Ask Your Question

Watershed - How to set minima of gradient map as seeds ?

asked 2017-01-19 04:37:10 -0500

Nbb gravatar image

updated 2017-01-20 10:51:59 -0500


I have a gradient map of an image obtained via convolution with the x and y sobel masks. I'd like to set the minimum points as the seed for the watershed segmentation.

Am I doing it correctly / efficiently if I scan the entire image with a 3x3 window and set the center as the seed if it happens to be the minimum ? Is there an OpenCV function that does this ?

Matlab's watershed function automatically sets the seed points.

This is my watershed result on Matlab image description

and on opencv image description

how do i make the result look more like the one i obtain on matlab ? This was the result of the watershed that I was taught. It doesnt look like the one i obtained from opencv. Does anyone have a watershed code that i can use that will give me the same result like matlab ?

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-01-19 08:44:42 -0500

LBerger gravatar image

updated 2017-01-20 01:40:08 -0500

Use a mask :

Mat img=imread("f:lena.jpg",IMREAD_COLOR);
Mat gx,gy,g;
Sobel(img,gx, CV_16S, 1, 0, 3); 
Sobel(img,gy, CV_16S, 0, 1,3);
Mat marqueur =g<20;
Mat cc;
int nb=connectedComponents(marqueur,cc,8,CV_32S);
Mat res;
Mat palette = Mat(256, 1, CV_8UC1,Scalar(255));<uchar>(0,0)=0;

cc.convertTo(res, CV_8UC1);
LUT(res, palette, res);
imshow("res", res);

I think now you have to merge watershed results...

image description

edit flag offensive delete link more


why is marquer = g < 20; ? is it an arbitrary value ? ur code gives me weird result i added the picture in my post

Nbb gravatar imageNbb ( 2017-01-19 22:21:18 -0500 )edit

20 it is an abitrary value. You have to choose. thresh. Results are not weird it is only a LUT problem. It is difficult to show an image with type CV_32S using only OpenCV function.

Now about matlab you change your question. I haven't got matlab it is difficult to give an answer without any reference. I think with a good thresh value and erode Mat (marqueur) before watershed you can improve result

LBerger gravatar imageLBerger ( 2017-01-20 01:46:45 -0500 )edit

Well id like the result to look more like matlab because it is the starting point of my project. I need an oversegmentation of the image via watershed and the regions need to be of similar size.

Nbb gravatar imageNbb ( 2017-01-20 02:39:11 -0500 )edit

give a reference . I prefer my result than matlab results : less connected components. But you have to define a criteria for : what is a good segmentation?

LBerger gravatar imageLBerger ( 2017-01-20 02:51:20 -0500 )edit

I tried using a 3x3 window to scan the entire image and its really slow :( Is there no way I can achieve similar results to that of MATLAB ?

Nbb gravatar imageNbb ( 2017-01-20 02:56:36 -0500 )edit

its for a class project. i need to achieve something similar for the next stage. im supposed to get something similar to the image i posted above from the lecture notes.

Nbb gravatar imageNbb ( 2017-01-20 10:39:22 -0500 )edit

Ok it is for your school project.

Do you think given matlab results are good ?

Read result in this page

If you really want matlab results then you must have many seeds=connected components. Try do change thresh an d plot number of connected components function as thresh.

LBerger gravatar imageLBerger ( 2017-01-20 11:05:36 -0500 )edit

thanks for ur continued help lberger. Its not whether its good or bad. Based on my understanding, the watershed algorithm grows from seeds that are initialized from local minima. So for the images above, it should be normal to retrieve a lot of components because of small variations in pixel intensity.

Nbb gravatar imageNbb ( 2017-01-20 11:47:59 -0500 )edit

I got even more confused when i re-read the watershed function. It seems that you pass in the colored image. If I already have the seeds then I don't need to pass in anything else other than the markers right ?

Isn't the workflow of watershed as follows
- find gradient map
- locate local minima
- grow seed from minima

which of the 3 is opencv's watershed doing ? Im freaking confused now. In matlab, I pass the gradient map and it computes everything else for me

Nbb gravatar imageNbb ( 2017-01-20 11:54:46 -0500 )edit

In opencv you give a mask type CV_32S with seed label.

LBerger gravatar imageLBerger ( 2017-01-20 13:07:35 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-01-19 04:37:10 -0500

Seen: 190 times

Last updated: Jan 20 '17