[英]How to correctly pass an image as an argument to a class in opencv python
I am a newbie to python and opencv and when I try to run this code I get an error in the colors method while converting BGR
to HSV
as 我是python和opencv的新手,当我尝试运行此代码时,在将
BGR
转换为HSV
时,在colors方法中出现错误
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
cv2.error: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\imgproc\src\color.cpp:7646:
error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cv::ipp_cvtColor
Even if I comment that part out and only run the part where it is simply returning the image as it is, it gives an error while displaying the image in cv2.imshow() as 即使我注释掉该部分并仅运行它只是按原样返回图像的部分,当在cv2.imshow()中显示图像时,也会产生错误
cv2.imshow('image',res)
cv2.error: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\highgui\src\window.cpp:281:
error: (-215) size.width>0 && size.height>0 in function cv::imshow
Please help me figure out if I'm missing out on something. 请帮助我弄清楚我是否缺少某些东西。
class basicop:
@staticmethod
def colors(color, frame):
if(color=='red'):
lower = np.array([0, 100, 100])
upper= np.array([10, 255, 255])
elif(color=='green'):
lower = np.array([86, 36, 99])
upper= np.array([86, 255, 255])
#CONVERT BGR TO HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(frame, lower, upper)
res = cv2.bitwise_and(frame, frame, mask= mask)
return frame
frame= cv2.imread('gree.jpg')
res=basicop.colors('green', frame)
cv2.imshow('image', res)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
elif k == ord('s'):
cv2.imwrite('sanj.jpg', res)
cv2.destroyAllWindows()
Error means that imread() can't read that image . 错误意味着imread()无法读取该图像。 But I saw you returned the frame in the color() where you should return the res .
但是我看到您在color()中返回了框架,您应该在其中返回res。
class basicop:
@staticmethod
def colors(color, frame):
if(color=='red'):
lower = np.array([0, 100, 100])
upper= np.array([100, 255, 255])
elif(color=='green'):
lower = np.array([50,50,50])
upper= np.array([70, 255, 255])
#CONVERT BGR TO HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(frame, lower, upper)
res = cv2.bitwise_and(frame, frame, mask= mask)
return res
frame= cv2.imread('img.jpg')
res=basicop.colors('green', frame)
cv2.imshow('image', res)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
elif k == ord('s'):
cv2.imwrite('snL.jpg', res)
cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.