-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinal_colour_detection.py
40 lines (29 loc) · 1.09 KB
/
final_colour_detection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import cv2
import numpy as np
# Capture video from the Raspberry Pi camera
cap = cv2.VideoCapture(0)
while True:
# Read the current frame from the camera
ret, frame = cap.read()
# Convert the image from the BGR color space to the HSV color space
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Define the range of colors to detect (in this case, red)
lower_red = np.array([0, 100, 100])
upper_red = np.array([10, 255, 255])
# Threshold the image to get only the red colors
mask = cv2.inRange(hsv, lower_red, upper_red)
# Find the coordinates of all non-zero pixels in the mask
coords = np.column_stack(np.where(mask > 0))
# Print the coordinates of the red pixels
print(coords)
# Bitwise-AND the mask and the original image
res = cv2.bitwise_and(frame, frame, mask=mask)
# Show the images
cv2.imshow("Original", frame)
cv2.imshow("Red", res)
# Exit the loop if the 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the camera and close the OpenCV window
cap.release()
cv2.destroyAllWindows()