-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
71 lines (52 loc) · 2.22 KB
/
camera.py
File metadata and controls
71 lines (52 loc) · 2.22 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import cv2
import numpy as np
def imgshow(name,img):
cv2.imshow(name,img)
cv2.moveWindow(name,200,200)
cv2.waitKey(0)
def map_contours_to_grid(contours, grid_shape, image_shape):
grid = np.zeros(grid_shape, dtype=int)
rows, cols = grid_shape
img_height, img_width = image_shape
cell_height = img_height / rows
cell_width = img_width / cols
for contour in contours:
if cv2.contourArea(contour) > 400:
M = cv2.moments(contour)
if M["m00"] != 0:
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
col = int(cx / cell_width)
row = int(cy / cell_height)
if 0 <= row < rows and 0 <= col < cols:
grid[row, col] = 1
return grid
def CaptureBoard(cam):
result, image = cam.read()
if result:
cv2.imwrite("currentPhoto.png", image)
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_red = np.array([140, 80, 80])
upper_red = np.array([255, 255, 255])
mask_red = cv2.inRange(image_hsv, lower_red, upper_red)
contours_red, _ = cv2.findContours(mask_red, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
grid_red = map_contours_to_grid(contours_red, (6, 7), image.shape[:2])
lower_yellow = np.array([12, 160, 150])
upper_yellow = np.array([60, 255, 255])
mask_yellow = cv2.inRange(image_hsv, lower_yellow, upper_yellow)
contours_yellow, _ = cv2.findContours(mask_yellow, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
grid_yellow = map_contours_to_grid(contours_yellow, (6, 7), image.shape[:2])
combined_grid = grid_red + grid_yellow * 2
for contour in contours_red:
if cv2.contourArea(contour) > 400:
cv2.drawContours(image, [contour], -1, (0, 0, 255), 3)
for contour in contours_yellow:
if cv2.contourArea(contour) > 400:
cv2.drawContours(image, [contour], -1, (0, 255, 255), 3)
#cv2.imshow("Contours", image)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
return combined_grid
else:
print("No image detected. Please try again.")
return -1