python opencv HSV range finder creating trackbars

前端 未结 2 1425
傲寒
傲寒 2021-01-28 14:43

I want to find the HSV value of a LASER dot using opencv and python. I got the code http://opencv-srf.blogspot.com.au/2010/09/object-detection-using-color-seperation.html from

相关标签:
2条回答
  • 2021-01-28 15:22

    You can grab the trackbar values with cv2.getTrackbarPos(). Also note that sometimes it puts trackbars out of order, which is annoying, but at least they're labeled.

    However, I don't think that these trackbars will work very well for live video feed. There's a lot of freezing issues. You'll have to have a super low framerate (works for me with cv2.waitKey(500) if you're actually trying to display it). This is mostly due to the trackbars sucking, not the thresholding operation, which is not that slow.

    You need to add your trackbars after you create the named window. Then, for your while loop, try:

    while True:
        # grab the frame
        ret, frame = cap.read()
    
        # get trackbar positions
        ilowH = cv2.getTrackbarPos('lowH', 'image')
        ihighH = cv2.getTrackbarPos('highH', 'image')
        ilowS = cv2.getTrackbarPos('lowS', 'image')
        ihighS = cv2.getTrackbarPos('highS', 'image')
        ilowV = cv2.getTrackbarPos('lowV', 'image')
        ihighV = cv2.getTrackbarPos('highV', 'image')
    
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        lower_hsv = np.array([ilowH, ilowS, ilowV])
        higher_hsv = np.array([ihighH, ihighS, ihighV])
        mask = cv2.inRange(hsv, lower_hsv, higher_hsv)
    
        frame = cv2.bitwise_and(frame, frame, mask=mask)
    
        # show thresholded image
        cv2.imshow('image', frame)
        k = cv2.waitKey(1000) & 0xFF # large wait time to remove freezing
        if k == 113 or k == 27:
            break
    

    and finally end the file with a cv2.destroyAllWindows()

    As an aside, the maximum H value for HSV is 180, not 179.

    Shameless plug: I happened to just finish a project doing precisely this, but on images. You can grab it on GitHub here. There is an example; try running it and then modifying as you need. It will let you change the colorspace and threshold inside each different colorspace, and it will print the final thresholding values that you ended on. Additionally it will return the output image from the operation for you to use, too. Hopefully it is useful for you! Feel free to send any issues or suggestions through GitHub for the project.

    Here is an example of it running:

    And as output it gives you:

    Colorspace: HSV 
    Lower bound: [68.4, 0.0, 0.0] 
    Upper bound: [180.0, 255.0, 255.0]
    

    as well as the binary image. I am currently working on getting this into a web application as well, but that probably won't be finished for a few days.

    0 讨论(0)
  • 2021-01-28 15:29

    Use this code to find range of masking of real-time video! this might save you time. Below is a whole code, Check it and run it to have a test.

    import cv2
    import numpy as np
    
    camera = cv2.VideoCapture(0)
    
    def nothing(x):
        pass
    
    cv2.namedWindow('marking')
    
    cv2.createTrackbar('H Lower','marking',0,255,nothing)
    cv2.createTrackbar('H Higher','marking',255,255,nothing)
    cv2.createTrackbar('S Lower','marking',0,255,nothing)
    cv2.createTrackbar('S Higher','marking',255,255,nothing)
    cv2.createTrackbar('V Lower','marking',0,255,nothing)
    cv2.createTrackbar('V Higher','marking',255,255,nothing)
    
    
    while(1):
        _,img = camera.read()
        img = cv2.flip(img,1)
    
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    
        hL = cv2.getTrackbarPos('H Lower','marking')
        hH = cv2.getTrackbarPos('H Higher','marking')
        sL = cv2.getTrackbarPos('S Lower','marking')
        sH = cv2.getTrackbarPos('S Higher','marking')
        vL = cv2.getTrackbarPos('V Lower','marking')
        vH = cv2.getTrackbarPos('V Higher','marking')
    
        LowerRegion = np.array([hL,sL,vL],np.uint8)
        upperRegion = np.array([hH,sH,vH],np.uint8)
    
        redObject = cv2.inRange(hsv,LowerRegion,upperRegion)
    
        kernal = np.ones((1,1),"uint8")
    
        red = cv2.morphologyEx(redObject,cv2.MORPH_OPEN,kernal)
        red = cv2.dilate(red,kernal,iterations=1)
    
        res1=cv2.bitwise_and(img, img, mask = red)
    
    
        cv2.imshow("Masking ",res1)
    
        if cv2.waitKey(10) & 0xFF == ord('q'):
            camera.release()
            cv2.destroyAllWindows()
            break`
    

    Thanks! Hugs..

    0 讨论(0)
提交回复
热议问题