此代码有效,但当网络摄像头面向有光的区域时,它也会将其识别为激光指示器。我想要的帮助是消除那些噪音,只识别激光指示器。enter image description here这张照片显示了它在面具中的样子,但是没有光线条件。到目前为止,这是我的代码:
cap = cv2.VideoCapture(0)
while True:
ret, main = cap.read()
hsv = cv2.cvtColor(main, cv2.COLOR_BGR2HSV)
gray = cv2.cvtColor(main, cv2.COLOR_BGR2GRAY)
lower_laserdot = np.array([0, 0, 255])
upper_laserdot = np.array([255, 255, 255])
threshold = cv2.inRange(hsv, lower_laserdot, upper_laserdot)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 700, param1
= 50, param2 = 30, minRadius = 0, maxRadius = 0)
threshcopy = threshold.copy()
_, find, _ = cv2.findContours(threshcopy, cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)
maxarea = 1
bestcnt = 1
for cnt in find:
area = cv2.contourArea(cnt)
if area > maxarea:
maxarea = area
bestcnt = cnt
M = cv2.moments(bestcnt)
x, y = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
for(cx,cy,r) in circles:
if x > 0 or y > 0:
print('A laser dot has been detected')
cv2.imshow('mask', threshold)
rs = cv2.resize(main, (1365, 730))
cv2.imshow('main', rs)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
答案 0 :(得分:1)
假设它是红点,仅过滤红色色调(例如Hue <25和Hue> 230)。您还可以增加图像的对比度,因为激光很可能是图片中最高强度的颜色。