Ask Your Question

Error in Watershed Distance Transform [closed]

asked 2018-11-26 08:59:32 -0500

nicolasrs gravatar image

updated 2018-11-26 10:09:57 -0500

I'm trying to execute image segmentation with distance transform and watershed algorithm. I've used GrabCut to eliminate the background. After several tests, I found the error in this part:

 // Perform the distance transform algorithm
Mat dist;
distanceTransform(bw, dist, DIST_L2, 3);
// Normalize the distance image for range = {0.0, 1.0}
// so we can visualize and threshold it
normalize(dist, dist, 0, 1.0, NORM_MINMAX);
imshow("Distance Transform Image", dist);

I'm using Codeblocks, Ubuntu 18.04 and OpenCV 4.0.0.

The little part of the code above is from this tutorial:

I get this error:

terminate called after throwing an instance of ‘cv::Exception'

what(): OPenCV(4.0.0) /home/nicolas/opencv-4.0.0/sources/modules/highgui/src/window_gtk.cpp:146: error: (-215:Assertion failed) == widget->original_image->data.ptr in function ‘cvlmageWidgetSetImage'
Aborted (core dumped)

Process returned 134 (0x86) execution time : 3.122 s Press ENTER ti continue.

Does anyone knows what's happening?

EDIT 1: Berak's answer solved the problem above, thanks berak! But after I've got another error in this part:

// Create the marker image for the watershed algorithm Mat markers = Mat::zeros(dist.size(), CV_8U);//CV_32SC1);

// Draw the foreground markers for (size_t i = 0; i < contours.size(); i++) drawContours(markers, contours, static_cast<int>(i), Scalar::all(static_cast<int>(i)+1), -1);

// Draw the background marker circle(markers, Point(5,5), 3, CV_RGB(0,255,0), -1); imshow("Markers", markers*10000);

I replaced CV_32SC1 with CV_8U and it worked, but I don't know if it's the right solution. After I got another error in this part:

// Perform the watershed algorithm watershed(src, markers); Mat mark = Mat::zeros(markers.size(), CV_8UC1); markers.convertTo(mark, CV_8UC1); bitwise_not(mark, mark); imshow("Markers_v2", mark); // uncomment this if you want to see how the mark waitKey(); // image looks like at that point

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by nicolasrs
close date 2018-11-26 11:08:55.816621


would you be so nice, and replace the error screenshot with a TEXT version, so it can be quoted / indexed properly ? thank you !

berak gravatar imageberak ( 2018-11-26 09:08:34 -0500 )edit

Yes, sure! It's ok the way I did it?

nicolasrs gravatar imagenicolasrs ( 2018-11-26 09:53:03 -0500 )edit

sure, thanks a lot !

berak gravatar imageberak ( 2018-11-26 09:55:06 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2018-11-26 09:15:26 -0500

berak gravatar image

this is most likely a bug introduced here

there is already a fix for Qt from nov. 16, and another pr here

you'll have to update your opencv codebase, and rebuild, but for today, try to convert the image back to CV_8U, before using imshow() like:

normalize(dist, dist, 0, 255.0, NORM_MINMAX, CV_8U);
imshow("Distance Transform Image", dist);
edit flag offensive delete link more


Thanks a lot, berak! This solved the problem, but now I found another error. I'll edit the question with it.

nicolasrs gravatar imagenicolasrs ( 2018-11-26 09:54:28 -0500 )edit

Question Tools

1 follower


Asked: 2018-11-26 08:59:32 -0500

Seen: 144 times

Last updated: Nov 26 '18