Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an error OpenCV(3.4.1) C:\projects\opencv-python\opencv\modules\imgproc\src\thresh.cpp:1406: error: (-215)

Tags:

python

opencv

I ran the below code and I am getting an error as

OpenCV(3.4.1) C:\projects\opencv-python\opencv\modules\imgproc\src\thresh.cpp:1406: error: (-215) src.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) in function cv::threshold

I am not clear on what this means and how to fix it

import numpy as numpy
from matplotlib import pyplot as matplot
import pandas as pandas
import math
from sklearn import preprocessing
from sklearn import svm
import cv2

blur = cv2.GaussianBlur(img,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
image = numpy.invert(th3)
matplot.imshow(image,'gray')
matplot.show()
like image 527
SidAvenger Avatar asked Jan 28 '26 20:01

SidAvenger


1 Answers

You will be able to resolve your error with following way.

First check whether your input image has only single channel. You can check it by running print img.shape. If the result is like (height, width, 3), then the image is not single channel. You can convert the image into a single channel one by:

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Then check whether the image type is not float. You can check it by running print img.dtype. If the result is related to float, you will need to change it also by:

img = img.astype('uint8')

And also the last thing, it is not actually an error in this case. But it can be an error in the future if you keep practicing this method of combining multiple flags. When you are using multiple flags, remember to combine then with not a plus sign, but with a | sign.

 ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)

Finally, you can you opencv functions to show the image. No need to depend on other libraries.

Final code is as follows:

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = img.astype('uint8')
blur = cv2.GaussianBlur(img,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
image = numpy.invert(th3)
cv2.show('image_out', image)
cv2.waitKey(0)
cv2.destroyAllWindows() 
like image 152
Ramesh-X Avatar answered Jan 30 '26 10:01

Ramesh-X



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!