Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV: error: (-215:Assertion failed) _src.type() == CV_8UC1 in function 'cv::equalizeHist'

Tags:

python

opencv

I'm trying with the code from link below to blur faces in images:

How to use opencv (python) to blur faces?

image = cv2.imread('45.jpg')
result_image = image.copy()

# Specify the trained cascade classifier
face_cascade_name = "‪C:/Users/User/Desktop/haarcascade_frontalface_alt.xml"

# Create a cascade classifier
face_cascade = cv2.CascadeClassifier()

# Load the specified classifier
face_cascade.load(face_cascade_name)

#Preprocess the image
grayimg = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
grayimg = cv2.equalizeHist(grayimg)

#Run the classifiers
faces = face_cascade.detectMultiScale(grayimg, 1.1, 2, 0|cv2.cv.CV_HAAR_SCALE_IMAGE, (30, 30))

print ("Faces detected")

But i got a Traceback error as follows. Please help. Thanks.

Traceback (most recent call last):

  File "<ipython-input-70-d20c79f10494>", line 15, in <module>
    grayimg = cv2.equalizeHist(grayimg)

error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\histogram.cpp:3334: error: (-215:Assertion failed) _src.type() == CV_8UC1 in function 'cv::equalizeHist'
like image 437
ah bon Avatar asked Dec 18 '18 09:12

ah bon


1 Answers

You need to convert to grey:

COLOR_BGR2GRAY

the error is telling you that your image is not an 8-bit grayscale image

change this line:

grayimg = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

to

grayimg = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

In the linked question you can see that the OP had used that for conversion

Regarding your latest error see related: Attribute error while using opencv for face recognition

basically it's moved to:

cv2.CASCADE_SCALE_IMAGE
like image 115
EdChum Avatar answered Nov 13 '22 02:11

EdChum