Ask Your Question
0

segfault on cv::imshow("Image window", cv_ptr->image)

asked 2014-06-15 05:56:18 -0600

hvn gravatar image

updated 2014-06-16 14:43:30 -0600

Hi,

When I try to show an image with

cv_ptr->image.data = image_in_mat.data;
cv::namedWindow("Image window");
cv::imshow("Image window", cv_ptr->image);

I get a segfault on the last line while it compiles fine. The segfault tells it's a memory issue but why ?

Ok, I tried many things now and I have this piece of code and 2 points of failure (imshow and publish) I don't get:

try
{
    cv::imshow(OPENCV_WINDOW, cv_ptr->image);
}
catch (cv::Exception& cv_ex)
{
    ROS_ERROR("cv exception: %s", cv_ex.what());
    return;
}
cv::waitKey(0);
image_pub.publish(cv_ptr->toImageMsg());

Why are these causing segfaults ?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-06-16 21:54:11 -0600

unxnut gravatar image

Seems to me that you are not copying all the properties of image_in_mat into cv_ptr->image. Assuming that both of them are of type cv::Mat, you can try to do the following:

cv_ptr->image = image_in_mat;
cv::imshow ( "Image window", cv_ptr->image );

and see if that cause the seg fault. If that does not, your headers are not correct in the current implementation.

edit flag offensive delete link more

Comments

Thanks, that solved the segfaults and after switching image_in_mat and cv_ptr->image it works..

hvn gravatar imagehvn ( 2014-06-17 01:02:59 -0600 )edit

That implies that your header do not match the data. You may have to investigate the step size and other properties.

unxnut gravatar imageunxnut ( 2014-06-17 06:40:10 -0600 )edit

i have the same problem, what did you do exactly to fix it.

chetan3061 gravatar imagechetan3061 ( 2015-04-15 23:48:01 -0600 )edit

Can you show detail specific to your issue?

unxnut gravatar imageunxnut ( 2015-04-16 06:38:21 -0600 )edit

this is what i used and it gave me the segmentation fault

void callback(const sensor_msgs::ImageConstPtr& msg) {

Mat mat;

cv_bridge::CvImagePtr cvImagePtr; try {

    cvImagePtr = cv_bridge::toCvCopy(msg,sensor_msgs::image_encodings::BGR8);

     mat = cvImagePtr->image;

    //std::cout<<mat.rows<<"\n";


     } 

catch (cv_bridge::Exception &e) {

   ROS_ERROR("cv_bridge exception:  %s", e.what());

}

    imshow("windowName", cvImagePtr->image);

    cv::waitKey(3);

    destroyWindow("windowName");

}

chetan3061 gravatar imagechetan3061 ( 2015-04-16 15:52:59 -0600 )edit

Question Tools

Stats

Asked: 2014-06-15 05:56:18 -0600

Seen: 4,372 times

Last updated: Jun 16 '14