Ask Your Question
0

How to correctly publish and receive cv::Mat using cv_bridge? (C++)

asked 2017-02-06 01:51:13 -0500

anonymous user

Anonymous

updated 2017-02-06 03:14:45 -0500

Hey everyone,

I am having troubles converting a cv::Mat to a sensor_msgs/Image, publishing it and doing the reverse operation to regain the original cv::Mat. The imshow() function shows me the correct image(white-gray-black image with numbers ranging from 0 to 1) before sending, but shows a seemingly random and black-white(binary) image after receiving it. Normalizing the matrix with cv::normalize() before displaying it does not do the trick either.

So I would be happy if you could check out my code below or give me a working sample code from one of your projects :). There is the fear that the mistake lies outside the shown code, but I don't think you want to read all my nodes.

Extract publisher code:

cv::Mat out_mat;
// fill mat....
std::cout << "out_mat.type() = " << out_mat.type() << "\n"; // prints out_mat.type() = 5
cv::namedWindow("out_mat", CV_WINDOW_NORMAL);
cv::resizeWindow("out_mat", 500, 500);
cv::imshow("out_mat", out_mat); // shows correct image
cv::waitKey(1);
cv_bridge::CvImage cvi_mat;
cvi_mat.encoding = sensor_msgs::image_encodings::MONO8;
cvi_mat.image = out_mat;
cvi_mat.toImageMsg(container); // container of type sensor_msgs/Image
pub_peaks_.publish(container);

Extract subscriber code:

// convert image msg to cv mat
cv_bridge::CvImagePtr img;
img = cv_bridge::toCvCopy(container, sensor_msgs::image_encodings::MONO8); // container of type sensor_msgs/Image
cv::Mat mat_received = img->image;
cv::Mat mat;
mat_received.convertTo(mat,5);
std::cout << "mat type: " << mat.type() << "\n"; // prints mat_c type: 5
cv::namedWindow("Grid", CV_WINDOW_NORMAL);
cv::resizeWindow("Grid", 500, 500);
cv::imshow("Grid", mat); // shows false/random (binary black-white) image
cv::waitKey(1);

Every help is appreciated.

edit retag flag offensive close merge delete

Comments

Not sure but maybe is the problem is on the matrix type conversion (I suggest to use the CV definition instead the int value!). Did you try to show your mat_received image instead the converted one?

afranceson gravatar image afranceson  ( 2017-02-07 05:28:55 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-02-06 07:33:01 -0500

anonymous user

Anonymous

For anyone wondering: As I only needed to send the matrix I created a different rosmsg containing an array where I wrote the values of the matrix. After receiving the array, I convert it back to a matrix. Therefore I don't have to deal with encodings and stuff.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2017-02-06 01:51:13 -0500

Seen: 2,887 times

Last updated: Feb 06 '17