0

I'm a beginner in OpenCV and I'm trying to binarize an image.

I found this SO post which explains how to do it using Otsu's method:

Converting an OpenCV Image to Black and White

but when I do this function call

(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

full code:

images = cv2.imread(args["image"])
im_gray = cv2.cvtColor(images, cv2.IMREAD_GRAYSCALE)
thresh = 200
(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

I get:

File "read.py", line 22, in <module>
    (thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.error: OpenCV(4.6.0) /io/opencv/modules/imgproc/src/thresh.cpp:1555: error: (-2:Unspecified error) in function 'double cv::threshold(cv::InputArray, cv::OutputArray, double, double, int)'
> THRESH_OTSU mode:
>     'src_type == CV_8UC1 || src_type == CV_16UC1'
> where
>     'src_type' is 24 (CV_8UC4)

why is this happening and how can I fix it?

Thanks.

Octavian Niculescu
  • 1,177
  • 1
  • 3
  • 24

0 Answers0