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

Hello,

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

and on opencv

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 ?

edit retag close merge delete

Sort by ยป oldest newest most voted

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);
g=gx.mul(gx)+gy.mul(gy);
g.convertTo(g,CV_16U);
cvtColor(g,g,CV_BGR2GRAY);
Mat marqueur =g<20;
Mat cc;
int nb=connectedComponents(marqueur,cc,8,CV_32S);
watershed(img,cc);
Mat res;
Mat palette = Mat(256, 1, CV_8UC1,Scalar(255));
palette.at<uchar>(0,0)=0;

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


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

more

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

( 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

( 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.

( 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?

( 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 ?

( 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.

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

Ok it is for your school project.

Do you think given matlab results are good ?

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.

( 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.

( 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
- 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

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

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

( 2017-01-20 13:07:35 -0500 )edit

## Stats

Seen: 216 times

Last updated: Jan 20 '17