4

Is there a floodFill function for python/openCV that takes a list of seeds and starts changing the color of its neighbours? I know that simplecv as a function like that SimpleCV floodFill. OpenCV says it has two floodFill functions when that uses a mask and another one that doesn't, documentation, I'm not being able to use the opencv floodfill function without a mask and with a list of seeds. Any help?

This is what I'm trying to do so far:

A=array([[0,1,1,0],[0,0,0,0],[1,1,1,1],[1,1,1,1]],np.uint8)
mask = np.ones((A.shape[0]+2,A.shape[0]+2),np.uint8)
mask[1:-1,1:-1] = np.zeros((A.shape))
cv.floodFill(A, mask, (3,0), 0,0,0,  flags=4|cv.FLOODFILL_MASK_ONLY)
print mask

returned mask:

[[1 1 1 1 1 1]
 [1 1 0 0 1 1]
 [1 1 1 1 1 1]
 [1 0 0 0 0 1]
 [1 0 0 0 0 1]
 [1 1 1 1 1 1]]

Expected mask:

[[1 1 1 1 1 1]
 [1 0 0 0 0 1]
 [1 0 0 0 0 1]
 [1 1 1 1 1 1]
 [1 1 1 1 1 1]
 [1 1 1 1 1 1]]

Original Image:

[[0 1 1 0]
 [0 0 0 0]
 [1 1 1 1]
 [1 1 1 1]]
João Abrantes
  • 4,772
  • 4
  • 35
  • 71

1 Answers1

3

If you look closely at the documentation, that's one of the purpose of mask. You can call multiple times the function (2nd version) every time with a different seed, and at the end mask will contain the area that has been floodfilled. If a new seed belongs to an area already floodfilled, your function call will return immediately.

Use the FLOODFILL_MASK_ONLY flag, and then use this mask to paint your input image with the desidered filling color at the end with a setTo() (You'll have to use a subimage of Mask! Removing first and last row and column). Note that your floodfill might produce different results depending on the order you process your seed points if you set loDiff or upDiff to something different than the default value zero.

Take also a look at this.

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197
  • Thanks for the help. I tried to do as you said but I wasn't able to succeed. I updated my question with more details. – João Abrantes Feb 06 '15 at 16:22
  • 2
    @JoãoAbrantes Yep, you got swapped x and y in your seed point, (3,0) is last pixel of first raw, not last pixel of first column. Coordinates are (x,y), namely (col,raw) – Antonio Feb 06 '15 at 16:33
  • 1
    now I feel dumb.. ahah thank you very much! I still need to get use to this x,y coordinates. – João Abrantes Feb 06 '15 at 16:43