In opencv how do I get a list of segemented regions

Viewed 394

I'm working on a project where I want to evaluate certain parameters on regions of a segemented image. So I have the following code

  col = cv2.imread("in.jpg",1)
  col=cv2.resize(col,(width,height),interpolation=cv2.INTER_CUBIC)
  res=cv2.pyrMeanShiftFiltering(col,20,45,3)

and would now like to somehow get a list of masks per region in res. So for example if res was now something like this

1 1 0 2 1 
1 0 0 2 1
0 0 2 2 1

I would like to get an output such as

1 1 0 0 0
1 0 0 0 0
0 0 0 0 0
,
0 0 1 0 0 
0 1 1 0 0
1 1 0 0 0
,
0 0 0 1 0 
0 0 0 1 0
0 0 1 1 0
,
0 0 0 0 1 
0 0 0 0 1
0 0 0 0 1

So that is a mask for each group of the same values that are connected. Maybe this could somehow involve the floodfill function? I can see that maybe by looping over every pixel and then flood filling and comparing to see if that set of pixels was already set might work but that seems like a very expensive way so is there something faster? Oh and here is an example image of res after the code has run here

1 Answers
Related