简体   繁体   中英

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

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('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 . But I saw you returned the frame in the color() where you should return the 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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