Ask Your Question
0

Error in Watershed Distance Transform [closed]

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

nicoz gravatar image

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

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: https://docs.opencv.org/4.0.0/d2/dbd/...

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) dst.data == 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 nicoz
close date 2018-11-26 11:08:55.816621

Comments

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 -0600 )edit
1

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

nicoz gravatar imagenicoz ( 2018-11-26 09:53:03 -0600 )edit

sure, thanks a lot !

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

1 answer

Sort by » oldest newest most voted
1

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

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

Comments

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

nicoz gravatar imagenicoz ( 2018-11-26 09:54:28 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 1,882 times

Last updated: Nov 26 '18