YaredTaddese / leaf-image-segmentation

Leaf Image Segmentation
MIT License
48 stars 20 forks source link

Crash on flood fill #3

Open EmmanuelMess opened 3 years ago

EmmanuelMess commented 3 years ago
Traceback (most recent call last):
  File "leaf-image-segmentation/segment.py", line 162, in <module>
    segment_leaf(os.path.join(base_folder, file), filling_mode, smooth, args.marker_intensity)
  File "leaf-image-segmentation/segment.py", line 66, in segment_leaf
    select_largest_obj(bin_image, fill_mode=filling_mode,
  File "leaf-image-segmentation/background_marker.py", line 229, in select_largest_obj
    cv2.floodFill(img_floodfill, mask_, seedPoint=bkg_seed,
cv2.error: OpenCV(4.5.1) /tmp/pip-req-build-ms668fyv/opencv/modules/imgproc/src/floodfill.cpp:509: error: (-211:One of the arguments' values is out of range) Seed point is outside of image in function 'floodFill'

Image: imagen

EmmanuelMess commented 3 years ago

Adding:

def clamp(n, smallest, largest):
    return max(smallest, min(n, largest))

bkg_seed = (clamp(bkg_seed[0], 0, img_bin.shape[1] - 1), clamp(bkg_seed[1], 0, img_bin.shape[0] - 1))

to line 220 of background_marker.py fixes it.

lxfhfut commented 2 years ago

This issue is probably caused by the inconsistent order of row and col in numpy and cv2. One simple fix is replacing line 229 of background_marker.py cv2.floodFill(img_floodfill, mask_, seedPoint=bkg_seed, newVal=lab_val) with cv2.floodFill(img_floodfill, mask_, seedPoint=(bkg_seed[1], bkg_seed[0]), newVal=lab_val)