Reputation: 1
I am working on a project, which needs to detect many small red laser dots correctly using Opencv. In the end, I want to find all the red laser dots out correctly. Now I changed the rgb to hsv and set the range to detect red dots, then used the canny and findContours function in the opencv to detect the edge and find countours. However, the result isn't very good. some red dots weren't detected well. you could see the picture below. Could you give me some advice about it? laser lights
below is my code:
cap = cv2.VideoCapture(0)
# set red thresh
lower_red = np.array([0,0,255])
#156, 100, 40
upper_red = np.array([180,255,255])
while(1):
ret, frame0 = cap.read()
frame = cv2.flip(frame0,0)
frame = frame[50:360,280:380]
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower_red, upper_red)
edged = cv2.Canny(mask, 30, 200)
cv2.imshow('Canny Edges After Contouring', edged)
_, contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print("Number of Contours found = " + str(len(contours)))
# Draw all contours
# -1 signifies drawing all contours
# for c in contours:
# M = cv2.moments(c)
# cX = int(M["m10"] / M["m00"])
# cY = int(M["m01"] / M["m00"])
# cv2.drawContours(frame, c, -1, (0, 255, 0), 3)
# cv2.circle(frame,(cX,cY),2,(255,255,255),-1)
# cv2.putText(frame,"center",(cX - 20, cY - 20),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),2)
cv2.imshow('Capture',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Upvotes: 0
Views: 639
Reputation: 216
Have you tried to play with the individual channels of your image? you can get better contrast between the laser dots and the background if you use for example the green channel of your image.
import cv2 [enter image description here][1]
image = cv2.imread("cv2/images/laser.jpg")
b, g, r = cv2.split(image)
ret, newIm = cv2.threshold(g, 220, 255, cv2.THRESH_BINARY)
cv2.imshow('New Image', newIm)
cv2.waitKey()
Upvotes: 1