-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape.py
50 lines (42 loc) · 1.98 KB
/
shape.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
41
42
43
44
45
46
47
48
49
50
import cv2
def concat_and_count(set1, set2):
# 将set2拼接到set1中
set1.update(set2)
# 求交集并返回
return set1
def detect_and_draw_shapes(image, mask, color_value, color_name):
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 圆形检测部分很烂,选择更优策略,先执行圆形检测,再执行方形检测
# circles = cv2.HoughCircles(mask, cv2.HOUGH_GRADIENT, dp=1.2, minDist=100, param1=50, param2=30, minRadius=100, maxRadius=1500)
#
# if circles is not None:
# circles = np.round(circles[0, :]).astype("int")
# for (x, y, r) in circles:
# perceived_width = r * 2
# distance = calculate_distance(KNOWN_WIDTH, FOCAL_LENGTH, perceived_width)
# cv2.circle(img, (x, y), r, color_value, 4)
# text = f"{color_name} Circle, Distance: {distance:.2f} m"
# cv2.putText(img, text, (x - r, y - r - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# 继续其他形状的检测,例如长方形和正方形
for contour in contours:
area = cv2.contourArea(contour)
if area < 20000:
continue
epsilon = 0.04 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
shape_name = None
if len(approx) == 4:
x, y, w, h = cv2.boundingRect(approx)
aspect_ratio = float(w) / h
if 0.9 <= aspect_ratio <= 1.1:
shape_name = "Square"
else:
shape_name = "Rectangle"
if shape_name:
cv2.drawContours(image, [approx], 0, color_value, 2)
text = f"{color_name} {shape_name}"
cv2.putText(image, text, (approx[0][0][0], approx[0][0][1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(255, 255, 255), 2)
set_new=concat_and_count({color_name}, {shape_name})
print(set_new)
return image