简体   繁体   English

使用open cv在python中处理图像

[英]image processing in python using open cv

import cv2 
fc=cv2.CascadeClassifier("haarcascade_fontalface_default.xml")

img=cv2.imread('dp.jpg')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

faces= fc.detectMultiScale(gray,1.1,4)

for (x,y,w,h) in faces:
    cv2.rectangle(img, (x,y), (x+w,y+h),(0,255,0),2)
    
cv2.imshow('img',img)
cv2.waitkey()
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-3-9ba3354eb85a> in <module>
      3 
      4 img=cv2.imread('dp.jpg')
----> 5 gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
      6 
      7 faces= fc.detectMultiScale(gray,1.1,4)

error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-rwld3ghi\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

Below are the possible reasons for the error以下是错误的可能原因

  • Give full path for your haar cascade file为您的haar cascade文件提供完整路径

  • Give the full path for your image .为您的image完整路径。 Check whether cv2 as correctly read your image by using print(img) , if it returns None then the path specified by you is incorrect .使用print(img)检查cv2是否正确读取您的图像,如果它返回None则您指定的路径不正确 Correct your path and try printing the image, if an array shows up then you are well to go as your image is now read successfully更正您的路径并尝试打印图像,如果出现array ,那么您就可以开始了,因为您的图像现在已成功读取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM