【问题标题】:Tracking yellow color object with OpenCV python使用 OpenCV python 跟踪黄色对象
【发布时间】:2019-12-07 07:59:35
【问题描述】:

如何使用 opencv 在 python 中跟踪黄色对象?而且,如果可能的话,我怎样才能得到对象的位置?

我已经尝试使用以下方法,但我无法弄清楚上下范围如何工作。

import numpy as np
import cv2


cap = cv2.VideoCapture(0)
while True:
    screen =  np.array(ImageGrab.grab())
    ret, img = cap.read()
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    #Help
    lower = np.array([])
    upper = np.array([])

    mask = cv2.inRange(hsv, lower, upper)

    cv2.imshow('screen', mask)



    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

它应该找到黄色物体并可能得到它们的位置。

【问题讨论】:

  • lowerupper 是范围阈值。您的mask 将只允许显示该范围内的颜色(即lower < hsv color < upper)。见this

标签: python image opencv image-processing colors


【解决方案1】:

下限值和上限值始终取决于您要选择的范围。对于特定颜色,没有硬性规定它应该在这个范围内。因为它可能会因为光照条件而有所不同。

关于您的跟踪,我建议您按照本教程进行操作。

https://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/

它解释了掩蔽、分割、跟踪。

【讨论】:

    【解决方案2】:

    您可以将图像转换为 HSV,然后使用颜色阈值。使用此示例图像

    具有下限/上限范围

    lower = np.array([22, 93, 0])
    upper = np.array([45, 255, 255])
    

    我们可以隔离黄色

    要获取对象的位置(我假设您需要一个边界框),您可以在生成的蒙版上找到轮廓

    import numpy as np
    import cv2
    
    image = cv2.imread('yellow.jpg')
    original = image.copy()
    image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    lower = np.array([22, 93, 0], dtype="uint8")
    upper = np.array([45, 255, 255], dtype="uint8")
    mask = cv2.inRange(image, lower, upper)
    
    cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    
    for c in cnts:
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(original, (x, y), (x + w, y + h), (36,255,12), 2)
    
    cv2.imshow('mask', mask)
    cv2.imshow('original', original)
    cv2.waitKey()
    

    您可以使用此脚本查找颜色阈值范围

    import cv2
    import sys
    import numpy as np
    
    def nothing(x):
        pass
    
    useCamera=False
    
    # Check if filename is passed
    if (len(sys.argv) <= 1) :
        print("'Usage: python hsvThresholder.py <ImageFilePath>' to ignore camera and use a local image.")
        useCamera = True
    
    # Create a window
    cv2.namedWindow('image')
    
    # create trackbars for color change
    cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv
    cv2.createTrackbar('SMin','image',0,255,nothing)
    cv2.createTrackbar('VMin','image',0,255,nothing)
    cv2.createTrackbar('HMax','image',0,179,nothing)
    cv2.createTrackbar('SMax','image',0,255,nothing)
    cv2.createTrackbar('VMax','image',0,255,nothing)
    
    # Set default value for MAX HSV trackbars.
    cv2.setTrackbarPos('HMax', 'image', 179)
    cv2.setTrackbarPos('SMax', 'image', 255)
    cv2.setTrackbarPos('VMax', 'image', 255)
    
    # Initialize to check if HSV min/max value changes
    hMin = sMin = vMin = hMax = sMax = vMax = 0
    phMin = psMin = pvMin = phMax = psMax = pvMax = 0
    
    # Output Image to display
    if useCamera:
        cap = cv2.VideoCapture(0)
        # Wait longer to prevent freeze for videos.
        waitTime = 330
    else:
        img = cv2.imread(sys.argv[1])
        output = img
        waitTime = 33
    
    while(1):
    
        if useCamera:
            # Capture frame-by-frame
            ret, img = cap.read()
            output = img
    
        # get current positions of all trackbars
        hMin = cv2.getTrackbarPos('HMin','image')
        sMin = cv2.getTrackbarPos('SMin','image')
        vMin = cv2.getTrackbarPos('VMin','image')
    
        hMax = cv2.getTrackbarPos('HMax','image')
        sMax = cv2.getTrackbarPos('SMax','image')
        vMax = cv2.getTrackbarPos('VMax','image')
    
        # Set minimum and max HSV values to display
        lower = np.array([hMin, sMin, vMin])
        upper = np.array([hMax, sMax, vMax])
    
        # Create HSV Image and threshold into a range.
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, lower, upper)
        output = cv2.bitwise_and(img,img, mask= mask)
    
        # Print if there is a change in HSV value
        if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
            print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
            phMin = hMin
            psMin = sMin
            pvMin = vMin
            phMax = hMax
            psMax = sMax
            pvMax = vMax
    
        # Display output image
        cv2.imshow('image',output)
    
        # Wait longer to prevent freeze for videos.
        if cv2.waitKey(waitTime) & 0xFF == ord('q'):
            break
    
    # Release resources
    if useCamera:
        cap.release()
    cv2.destroyAllWindows()
    

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 2017-04-25
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 2020-06-26
      • 2012-11-17
      相关资源
      最近更新 更多