ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Convert cv_bridge Image to cv::Mat image

asked 2016-02-03 11:07:17 -0600

user23fj239 gravatar image

I would like to have a pure OpenCV version cv::Mat of the Image on which I can do further modifications. I tried cv_ptr->dImg; but it outputs errors. So far I have

 void imageCb(const sensor_msgs::ImageConstPtr& msg)
   {
    cv_bridge::CvImagePtr cv_ptr;
    try
    {
      cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::TYPE_16UC1);
    }
    catch (cv_bridge::Exception& e)
    {
      ROS_ERROR("cv_bridge exception: %s", e.what());
      return;
    }

    // Draw an example circle on the video stream
    //if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
     // cv::circle(cv_ptr->image, cv::Point(px, py), 10, CV_RGB(255,0,0));
        cv::Mat dImg;
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2016-02-03 11:57:17 -0600

user23fj239 gravatar image

I guess I figured it out. Did I make any mistakes?

cv::Mat dImg =  cv_ptr->image;
     double min = 0;
     double max = 1000;
cv::Mat img_scaled_8u;
cv::Mat(cv_ptr->image-min).convertTo(img_scaled_8u, CV_8UC1, 255. / (max - min));
cv::cvtColor(img_scaled_8u, dImg, CV_GRAY2RGB);
//if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
//cv::circle(dImg, cv::Point(px, py), 10, CV_RGB(255,0,0));
// Update GUI Window
cv::imshow(OPENCV_WINDOW, dImg);
cv::waitKey(3);
edit flag offensive delete link more

Comments

I only needed the part of converting cv_bridge image to Mat. It worked for me - thanks!

Gushu gravatar image Gushu  ( 2018-05-24 07:13:38 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-02-03 11:07:17 -0600

Seen: 7,250 times

Last updated: Feb 03 '16