how can I write the image.h in my own data type

asked 2020-10-11 06:32:04 -0500

wangxy gravatar image

I want to publish an image and some other info , to ensure data sync. So I create mydata.msg like this

sensor_msgs/Image my_img
float64 x
float64 y
float64 z

How can I set the data of my_img?

my_package::my_data my_img_info;
my_img_info.my_img.data = ?

maybe I should write data to my_img.data, but I dont know how to write it. so are there any tutorials that can help me? Or other methods can ensure that images and data are one-to-one? Thanks.

edit retag flag offensive close merge delete

Comments

1

Since data is a one-dimensional array of uint8, for example, if we had the 2-dimensional image data original_img[height][width], we could copy it with the following

for(i = 0; i < my_img_info.my_img.height; i++) {
    for(j = 0; j < my_img_info.my_img.width; j++) {
        my_img_info.my_img[i*my_img_info.my_img.width + j] = original_img[i][j];
    }
}
miura gravatar image miura  ( 2020-10-12 08:50:31 -0500 )edit

Thanks for your answer I have another question,if I want to read image with subscriber´╝îShould I also use a two-dimensional loop to store image as a cv::Mat?

wangxy gravatar image wangxy  ( 2020-10-15 23:34:10 -0500 )edit

I'm glad I was able to help you. If you have a new question, you should create a new question page. If it is something that can be answered, we will respond to it.

miura gravatar image miura  ( 2020-10-16 06:48:28 -0500 )edit