使用python opencv跟踪白色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22588146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:13:59  来源:igfitidea点击:

Tracking white color using python opencv

pythonopencvcomputer-visionhsvcolor-tracking

提问by user3429616

I would like to track white color using webcam and python opencv. I already have the code to track blue color.

我想使用网络摄像头和 python opencv 跟踪白色。我已经有了跟踪蓝色的代码。

_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# define range of blue color in HSV
lower_blue = np.array([110,100,100])
upper_blue = np.array([130,255,255])

#How to define this range for white color


# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)

cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)

what valuesshould I give as lower bound and upper bound to track white color!!?? I tried changing values and I got other colors but no luck with the white color!!!

我应该给出什么作为跟踪白色的下限和上限!??我尝试改变值,我得到了其他颜色,但白色没有运气!!!

is that HSV values or BGR values specified as lower and upper bounds???

是 HSV 值还是 BGR 值指定为下限和上限???

PS : I must get the last result as a binary imagefor further processing!!

PS:我必须将最后的结果作为二进制图像进行进一步处理!!

Please help me !!!

请帮我 !!!

采纳答案by Vipul

I wrote this for tracking white color :

我写这个是为了跟踪白色:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(1):

    _, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of white color in HSV
    # change it according to your need !
    lower_white = np.array([0,0,0], dtype=np.uint8)
    upper_white = np.array([0,0,255], dtype=np.uint8)

    # Threshold the HSV image to get only white colors
    mask = cv2.inRange(hsv, lower_white, upper_white)
    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(frame,frame, mask= mask)

    cv2.imshow('frame',frame)
    cv2.imshow('mask',mask)
    cv2.imshow('res',res)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

I tried to track the white screen of my phone and got this :

我试图跟踪我手机的白屏并得到这个:

enter image description here

在此处输入图片说明

You can try changing the HSV values You might also try HSL color space as Legat said, it would be more accurate

您可以尝试更改 HSV 值您也可以像 Legat 所说的那样尝试 HSL 色彩空间,这样会更准确

回答by Legat

Let's take a look at HSV color space:

让我们来看看 HSV 色彩空间:

enter image description here

在此处输入图片说明

You need white, which is close to the center and rather high. Start with

你需要白色,它靠近中心并且相当高。从...开始

sensitivity = 15
lower_white = np.array([0,0,255-sensitivity])
upper_white = np.array([255,sensitivity,255])

and then adjust the threshold to your needs.

然后根据您的需要调整阈值。

You might also consider using HSLcolor space, which stands for Hue, Saturation, Lightness. Then you would only have to look at lightness for detecting white and recognizing other colors would stay easy. Both HSV and HSL keep similar colors close. Also HSL would probably prove more accurate for detecting white - here is why:

您也可以考虑使用 HSL色彩空间,它代表 Hue、Saturation、Lightness。然后你只需要看亮度来检测白色,识别其他颜色就很容易了。HSV 和 HSL 都保持相似的颜色接近。此外,HSL 可能会更准确地检测白色 - 这就是原因:

enter image description here

在此处输入图片说明

回答by nathancy

Here's an HSV color thresholder script to determine the lower and upper bounds using sliders

这是一个 HSV 颜色阈值脚本,用于使用滑块确定上下限

enter image description here

在此处输入图片说明

Results

结果

Using this sample image

使用此示例图像

With these lower/upper thresholds

有了这些下限/上限阈值

lower_white = np.array([0,0,168])
upper_white = np.array([172,111,255])

We get isolated white pixels (left) and the binary mask (right)

我们得到孤立的白色像素(左)和二值掩码(右)

Here's the script, remember to change the input image path

这里是脚本,记得改输入图片路径

import cv2
import sys
import numpy as np

def nothing(x):
    pass

# Load in image
image = cv2.imread('1.jpg')

# 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
wait_time = 33

while(1):

    # 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(image, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, lower, upper)
    output = cv2.bitwise_and(image,image, 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(wait_time) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()