Choosing the correct upper and lower HSV boundaries for color detection with`cv::inRange` (OpenCV)

前端 未结 6 1973
旧巷少年郎
旧巷少年郎 2020-11-22 11:57

I have an image of a coffee can with an orange lid position of which I want to find. Here is it \"image\".

gcol

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 12:46

    I Created this simple program to get HSV Codes in realtime

    import cv2
    import numpy as np
    
    
    cap = cv2.VideoCapture(0)
    
    def nothing(x):
        pass
    # Creating a window for later use
    cv2.namedWindow('result')
    
    # Starting with 100's to prevent error while masking
    h,s,v = 100,100,100
    
    # Creating track bar
    cv2.createTrackbar('h', 'result',0,179,nothing)
    cv2.createTrackbar('s', 'result',0,255,nothing)
    cv2.createTrackbar('v', 'result',0,255,nothing)
    
    while(1):
    
        _, frame = cap.read()
    
        #converting to HSV
        hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
    
        # get info from track bar and appy to result
        h = cv2.getTrackbarPos('h','result')
        s = cv2.getTrackbarPos('s','result')
        v = cv2.getTrackbarPos('v','result')
    
        # Normal masking algorithm
        lower_blue = np.array([h,s,v])
        upper_blue = np.array([180,255,255])
    
        mask = cv2.inRange(hsv,lower_blue, upper_blue)
    
        result = cv2.bitwise_and(frame,frame,mask = mask)
    
        cv2.imshow('result',result)
    
        k = cv2.waitKey(5) & 0xFF
        if k == 27:
            break
    
    cap.release()
    
    cv2.destroyAllWindows()
    

提交回复
热议问题