danforthcenter / plantcv

Plant phenotyping with image analysis
Mozilla Public License 2.0
661 stars 264 forks source link

Issue with thresholding images - image is neither greyscale or rbg? #1440

Closed jsab101 closed 9 months ago

jsab101 commented 9 months ago

I cannot successfully threshold a subset of my images. All images have been taken the same way, but my code fails for about 20% of the images.

I started with thresh = pcv.threshold.binary(gray_img = img, threshold = 200 , object_type="dark") bsa_fill = pcv.fill(bin_img = thresh, size = 24)

and got the error: RuntimeError: Image is not binary (at the pcv.fill step)

I thought that while the images look grayscale to the eye, they may not be so I tried selecting a colorspace to threshold: cs_plot = pcv.visualize.colorspaces(rgb_img=img, original_img=False)

and I got the error: RuntimeError: Input image is not RGB!

I then tried the otsu autocthreshold method thresh = pcv.threshold.otsu(gray_img=img, object_type="dark")

And got the error:

error: OpenCV(4.8.1) /Users/runner/miniforge3/conda-bld/libopencv_1698890312282/work/modules/imgproc/src/thresh.cpp:1555: error: (-2:Unspecified error) in function 'double cv::threshold(InputArray, OutputArray, double, double, int)'

THRESH_OTSU mode: 'src_type == CV_8UC1 || src_type == CV_16UC1' where 'src_type' is 16 (CV_8UC3)

I then tried to convert my image to 8-bit greyscale image = img.astype("uint8")

and got the error (with the otsu threshold): error: OpenCV(4.8.1) /Users/runner/miniforge3/conda-bld/libopencv_1698890312282/work/modules/imgproc/src/thresh.cpp:1555: error: (-2:Unspecified error) in function 'double cv::threshold(InputArray, OutputArray, double, double, int)'

THRESH_OTSU mode: 'src_type == CV_8UC1 || src_type == CV_16UC1' where 'src_type' is 16 (CV_8UC3)

and with the binary threshold:

RuntimeError: Image is not binary (at the pcv.fill step)

Example image - 14113976

Expected behavior I expect the image to be thresholded and thus be able to be further processed.

Local environment (please complete the following information):

Additional context Strangely this only occurs for a subset of my images when I am trying to run a multi-plant workflow. All the images I am having trouble with work when processed individually in a jupyter notebook.

HaleySchuhl commented 9 months ago

Hi @jsab101 thanks for opening this issue. I've downloaded the example image and can confirm that it appears to be grayscale image data. I'm wondering if not all images in the dataset are grayscale. Because I'm unable to replicate your error locally when the functions are run separately.

image

Can you try to add this to your readimage step and see if that resolves the error in 1/5 of the images?

img, path, filename= pcv.readimage("300060513-66541894-9c9e-4fc9-8344-c3ea2f2e4b79.jpg", mode="gray")
jsab101 commented 9 months ago

This worked! Thank you!

HaleySchuhl commented 9 months ago

Feel free to reopen this issue with any further questions. Thanks :D