相机无法检测 python 中的多种颜色
Camera unable to detect multiple colours in python
我正在尝试让我的相机检测红色和绿色。
现在它可以成功检测到其中一种颜色,但无法检测到两种颜色。
ball_color=('red','green')
如果我将其更改为 ball_color=('red') 或 ball_color=('green') 它可以工作,但是当我输入 ball_color=('red','green') 时出现错误。
我在底部添加了错误代码。非常感谢!
import cv2
import numpy as np
ball_color=('red','green')
color_dist = {'red': {'Lower': np.array([0, 60, 60]), 'Upper': np.array([10, 255, 255])},
'green': {'Lower': np.array([35, 43, 35]), 'Upper': np.array([90, 255, 255])},
}
cap = cv2.VideoCapture(0)
cv2.namedWindow('camera', cv2.WINDOW_AUTOSIZE)
while cap.isOpened():
ret, frame = cap.read()
if ret:
if frame is not None:
gs_frame = cv2.GaussianBlur(frame, (5, 5), 0) # 高斯模糊
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV) # 转化成HSV图像
erode_hsv = cv2.erode(hsv, None, iterations=2) # 腐蚀 粗的变细
inRange_hsv = cv2.inRange(erode_hsv, color_dist[ball_color]['Lower'], color_dist[ball_color]['Upper'])
cnts = cv2.findContours(inRange_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
c = max(cnts, key=cv2.contourArea)
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
cv2.drawContours(frame, [np.int0(box)], -1, (0, 255, 255), 2)
cv2.imshow('camera', frame)
cv2.waitKey(1)
cap.release()
cv2.waitKey(0)
cv2.destroyAllWindows()
错误代码如下:
Traceback (most recent call last):
File "/Users/fangyaoting/PycharmProjects/pythonProject/顏色辨識.py", line 21, in <module>
inRange_hsv = cv2.inRange(erode_hsv, color_dist[ball_color]['Lower'], color_dist[ball_color]['Upper'])
KeyError: ('red', 'green')
我认为可能是灵魂:
import cv2
import numpy as np
ball_color=('red','green')
color_dist = {'red': {'Lower': np.array([-10, 100, 100]), 'Upper': np.array([10, 255, 255])},
'green': {'Lower': np.array([35, 43, 35]), 'Upper': np.array([90, 255, 255])},
}
cap = cv2.VideoCapture(0)
cv2.namedWindow('camera', cv2.WINDOW_AUTOSIZE)
while cap.isOpened():
ret, frame = cap.read()
if ret:
if frame is not None:
gs_frame = cv2.GaussianBlur(frame, (5, 5), 0) # 高斯模糊
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV) # 转化成HSV图像
erode_hsv = cv2.erode(hsv, None, iterations=2) # 腐蚀 粗的变细
b_shape = hsv.shape[0], hsv.shape[1], 1
inRange_hsv = np.zeros(b_shape, hsv.dtype)
for i in ball_color:
inRange_hsv_tmp = cv2.inRange(erode_hsv, color_dist[i]['Lower'], color_dist[i]['Upper'])
inRange_hsv = cv2.bitwise_or(inRange_hsv, inRange_hsv_tmp)
cnts = cv2.findContours(inRange_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if len(cnts) > 0:
c = max(cnts, key=cv2.contourArea)
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
cv2.drawContours(frame, [np.int0(box)], -1, (0, 255, 255), 2)
cv2.imshow('camera', frame)
cv2.waitKey(1)
else:
print("無畫面")
else:
print("無法讀取鏡頭!")
cap.release()
cv2.waitKey(0)
cv2.destroyAllWindows()
您尝试将字符串元组作为参数传递给 cv2.inRange
,它可以使用长度为 1 的元组,但不能使用更多参数。所以你应该迭代它们并分别对每种颜色使用 cv2.inRange
然后对它们使用 cv2.bitwise_or
到 'concat' 二值图像。
关于cnts
:有时找不到任何轮廓,所以你应该使用if
语句来控制它。
编辑:在这一行 inRange_hsv = cv2.bitwise_or(inRange_hsv, inRange_hsv_tmp)
我正在尝试让我的相机检测红色和绿色。 现在它可以成功检测到其中一种颜色,但无法检测到两种颜色。
ball_color=('red','green')
如果我将其更改为 ball_color=('red') 或 ball_color=('green') 它可以工作,但是当我输入 ball_color=('red','green') 时出现错误。
我在底部添加了错误代码。非常感谢!
import cv2
import numpy as np
ball_color=('red','green')
color_dist = {'red': {'Lower': np.array([0, 60, 60]), 'Upper': np.array([10, 255, 255])},
'green': {'Lower': np.array([35, 43, 35]), 'Upper': np.array([90, 255, 255])},
}
cap = cv2.VideoCapture(0)
cv2.namedWindow('camera', cv2.WINDOW_AUTOSIZE)
while cap.isOpened():
ret, frame = cap.read()
if ret:
if frame is not None:
gs_frame = cv2.GaussianBlur(frame, (5, 5), 0) # 高斯模糊
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV) # 转化成HSV图像
erode_hsv = cv2.erode(hsv, None, iterations=2) # 腐蚀 粗的变细
inRange_hsv = cv2.inRange(erode_hsv, color_dist[ball_color]['Lower'], color_dist[ball_color]['Upper'])
cnts = cv2.findContours(inRange_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
c = max(cnts, key=cv2.contourArea)
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
cv2.drawContours(frame, [np.int0(box)], -1, (0, 255, 255), 2)
cv2.imshow('camera', frame)
cv2.waitKey(1)
cap.release()
cv2.waitKey(0)
cv2.destroyAllWindows()
错误代码如下:
Traceback (most recent call last):
File "/Users/fangyaoting/PycharmProjects/pythonProject/顏色辨識.py", line 21, in <module>
inRange_hsv = cv2.inRange(erode_hsv, color_dist[ball_color]['Lower'], color_dist[ball_color]['Upper'])
KeyError: ('red', 'green')
我认为可能是灵魂:
import cv2
import numpy as np
ball_color=('red','green')
color_dist = {'red': {'Lower': np.array([-10, 100, 100]), 'Upper': np.array([10, 255, 255])},
'green': {'Lower': np.array([35, 43, 35]), 'Upper': np.array([90, 255, 255])},
}
cap = cv2.VideoCapture(0)
cv2.namedWindow('camera', cv2.WINDOW_AUTOSIZE)
while cap.isOpened():
ret, frame = cap.read()
if ret:
if frame is not None:
gs_frame = cv2.GaussianBlur(frame, (5, 5), 0) # 高斯模糊
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV) # 转化成HSV图像
erode_hsv = cv2.erode(hsv, None, iterations=2) # 腐蚀 粗的变细
b_shape = hsv.shape[0], hsv.shape[1], 1
inRange_hsv = np.zeros(b_shape, hsv.dtype)
for i in ball_color:
inRange_hsv_tmp = cv2.inRange(erode_hsv, color_dist[i]['Lower'], color_dist[i]['Upper'])
inRange_hsv = cv2.bitwise_or(inRange_hsv, inRange_hsv_tmp)
cnts = cv2.findContours(inRange_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if len(cnts) > 0:
c = max(cnts, key=cv2.contourArea)
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
cv2.drawContours(frame, [np.int0(box)], -1, (0, 255, 255), 2)
cv2.imshow('camera', frame)
cv2.waitKey(1)
else:
print("無畫面")
else:
print("無法讀取鏡頭!")
cap.release()
cv2.waitKey(0)
cv2.destroyAllWindows()
您尝试将字符串元组作为参数传递给 cv2.inRange
,它可以使用长度为 1 的元组,但不能使用更多参数。所以你应该迭代它们并分别对每种颜色使用 cv2.inRange
然后对它们使用 cv2.bitwise_or
到 'concat' 二值图像。
关于cnts
:有时找不到任何轮廓,所以你应该使用if
语句来控制它。
编辑:在这一行 inRange_hsv = cv2.bitwise_or(inRange_hsv, inRange_hsv_tmp)