gpt4 book ai didi

python - 相机捕获蓝色时如何打印文本?

转载 作者:行者123 更新时间:2023-12-02 17:47:36 26 4
gpt4 key购买 nike

我正在使用opencv和python在脚本捕获特定颜色时打印文本的脚本。我尝试使用if语句,但失败。

这是我的代码:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(1):

# Take each frame
_, frame = cap.read()

# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

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

# 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)

if result.any() == True:
print 'I can see blue color'

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

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

cv2.destroyAllWindows()

最佳答案

我制定了一个适合我的环境的解决方案。我正在使用Python 2.7和OpenCV 2.4.6。您可能需要修改blue_threshold值以适合您的需求。

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
blue_threshold = 1000000 # This value you could change for what works best

while True:

# Take each frame
_, frame = cap.read()

# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

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

# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
count = mask.sum()

if count > blue_threshold:
print 'I can see blue color'


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

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

cv2.destroyAllWindows()

关于python - 相机捕获蓝色时如何打印文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31476793/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com