floodFill loDiff/hiDiff not working?

asked 2017-11-30 05:06:04 -0600

NewPerspective5 gravatar image

updated 2017-11-30 05:48:37 -0600

I have been trying to implement floodFill to segment a image based on color. However it seems like no matter the values I use for loDiff/hiDiff the results remain the same. I tried everything between 0, and 10 000 as values on loDiff and hiDiff but the results remain the same. I had imagined a very high hiDiff and loDiff would result in the entire image being marked by floodFill.

Results below:

Original:

Original image

Pre processed:

Pre-processed image

Mask after floodFill:

Mask after flood fill

I attached my code below. I am using OpenCV version 3.2.0.

#Load file
image = cv2.imread(inputfile)

#Pre process image
blob_image = image.copy()
blob_image = cv2.medianBlur(blob_image, 55) #Set high for demonstration purpose

#Watershed to segment by color
h, w = blob_image.shape[:2]
seedPoint = (0, 0)#(int(h/2), int(w/2))
mask = np.zeros((h+2, w+2), np.uint8)
newVal = 10
rect = 0
diff = 250
maskVal = 255
flags = 4 | ( maskVal << 8 ) | cv2.FLOODFILL_MASK_ONLY
newmask = cv2.floodFill(image = blob_image, mask = mask, seedPoint = seedPoint,
                        newVal = newVal, loDiff = diff, upDiff = diff, flags = flags)
#Visualize results
cv2.imshow("Original", image)
cv2.imshow("Blurred", blob_image)
cv2.imshow("Mask", mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

1

Solved the problem, apparently for colored images loDiff and hiDiff needs to have one scalar for each channel (given as a list) instead of just one scalar.

NewPerspective5 gravatar imageNewPerspective5 ( 2017-11-30 09:17:09 -0600 )edit

If you see this reply, would you mind copying your comment into an answer so this can be marked as solved? As-is, it's going to sit in the "unanswered" list forever.

ssokolow gravatar imagessokolow ( 2020-03-26 00:45:09 -0600 )edit