Search code examples
pythonimage-segmentationscikit-image

Python skimage flood fill won't work on a loaded binarized image


I'm trying to apply a flood_fill method to a certain image. Unfortunately, even though it works on an exemplary image, it doesn't work on mine, which is already binarized.

The code that works:

from skimage import data, filters
from skimage.segmentation import flood, flood_fill
import cv2 as cv


cameraman = data.camera()
flooded = flood_fill(cameraman, (200, 100), 255, tolerance=10)
cv.imshow("aaa",flooded)
cv.waitKey()

And the code that does not:

from skimage import data, filters
from skimage.segmentation import flood, flood_fill
import cv2 as cv
import numpy as np


img = cv.imread("Tubka_binar.png")
flooded = flood_fill(img, (200, 100), 100, tolerance = 10)

cv.imshow("aaa",flooded)
cv.waitKey()

And the errors I get:

Traceback (most recent call last):
  File "C:/Users/User/Documents/PW/MAGISTERSKIE/__PRACA/Python/Grubość Tuby.py", line 8, in <module>
    flooded = flood_fill(img, (200, 100), 100, tolerance = 10)
  File "C:\Users\User\Desktop\PROJEKT_PYTHONOWY\venv\lib\site-packages\skimage\morphology\_flood_fill.py", line 104, in flood_fill
    tolerance=tolerance)
  File "C:\Users\User\Desktop\PROJEKT_PYTHONOWY\venv\lib\site-packages\skimage\morphology\_flood_fill.py", line 235, in flood
    working_image.shape, order=order)
  File "<__array_function__ internals>", line 6, in ravel_multi_index
ValueError: parameter multi_index must be a sequence of length 3

Process finished with exit code 1

The image variables in both cases seem to be the same type. The image that I read in the second case is a binarized photo, that takes only two values: 0 and 255.

What is causing this? Best regards


Solution

  • It looks to me like your second image is not actually grayscale but rather saved (or loaded) as a 3-channel RGB image. If you print img.shape, I bet it’ll be something like (512, 512, 3). You can fix this by changing your reading code to:

    img = cv.imread("Tubka_binar.png")[..., 0]