Ask Your Question

need explaination in watershed program

asked 2014-08-19 01:13:42 -0500

jamesnzt gravatar image

updated 2014-08-19 04:23:24 -0500

In the Watershed based segmentation program , I need to know how the following part works

vector<Vec3b> colorTab;
for( i = 0; i < compCount; i++ )
int b = theRNG().uniform(0, 255);
int g = theRNG().uniform(0, 255);
int r = theRNG().uniform(0, 255);
colorTab.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));


// paint the watershed image
for( i = 0; i < markers.rows; i++ )
for( j = 0; j < markers.cols; j++ )
int index =<int>(i,j);
if( index == -1 )<Vec3b>(i,j) = Vec3b(255,255,255);
else if( index <= 0 || index > compCount )<Vec3b>(i,j) = Vec3b(0,0,0);
else<Vec3b>(i,j) = colorTab[index - 1];
wshed = wshed*0.5 + imgGray*0.5;

can any one explain the function of each line in the above code blocks?

edit retag flag offensive close merge delete


Instead of painting each region by random colour is it possible to segment individual region in this code? here "index" variable represent the number of regions segmented in the image. for each index value (region) a particular colon is painted by the second code block. how can i segment each block can you have any idea sir?

jamesnzt gravatar imagejamesnzt ( 2014-08-20 04:19:01 -0500 )edit

But @StevenPuttemans i think this method will create 1 in all the "index" no of regions. Consider i have 14 regions in which some are sharing common boundary. When i set 1 for regions denoted by index variable the regions which are sharing common boundary will merge and produce 1 in the mask.

jamesnzt gravatar imagejamesnzt ( 2014-08-20 04:31:51 -0500 )edit

Got it Thanks

jamesnzt gravatar imagejamesnzt ( 2014-08-20 04:38:55 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-08-20 04:41:49 -0500

Let me combine the remarks in an answer so that we can close this one down.


Without knowing exactly what the code of watershed does (i do know the principle, just haven't done it yet in openCV) I can say that the first part is basically creating different colors for filling each component. For doing this the random number generator functionality of OpenCV is used. For the amount of components a BGR color scalar is pushed back to the colorTab vector.

As to the second code snippet, this basically fills up the retrieved regions of the components and fills them with the retrieved color for that component. It is then placed as an overlay image on top of the grayscale image.


By creating a mask based on the indexes. Basically what you do is creating a 3D matrix construction. Each channel of your image will correspond to one of the indexes. Then loop over the image x and y positions and for each element check which index they have. Then set at that channel, inside the 3D matrix structure, the value on 1 instead of zeros. This will create all the masks to seperate the different regions.


No they will if you keep 1 indices strictly to the seperate masks. You could then check for overlapping boundaries, but afaik each pixel can only have a single index and not multiple, so there should be NO overlap of regions, not even a single pixel border.

Please if this fixed your problem, do accept this as the solution to the problem.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2014-08-19 01:13:42 -0500

Seen: 128 times

Last updated: Aug 20 '14