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

早过忘川 提交于 2021-02-05 02:20:49

问题


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'

回答1:


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


来源:https://stackoverflow.com/questions/53829896/opencv-error-215assertion-failed-src-type-cv-8uc1-in-function-cve

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!