回答編集履歴
1
せっかくなので…
test
CHANGED
@@ -20,4 +20,118 @@
|
|
20
20
|
|
21
21
|
---
|
22
22
|
|
23
|
+
漂う力技感。
|
24
|
+
|
25
|
+
![test.png](7481e5bd74f6ed573fa5b2ffe3e64be7.png)
|
26
|
+
|
27
|
+
![img_drawn.png](e4648eedebee0efe3dd0943c611177eb.png)
|
28
|
+
|
29
|
+
```Python3
|
30
|
+
|
31
|
+
import cv2
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
def detect_contour(img):
|
36
|
+
|
37
|
+
int_ocv = int(cv2.__version__[0])
|
38
|
+
|
39
|
+
if int_ocv == 4:
|
40
|
+
|
41
|
+
contours, hierachy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
42
|
+
|
43
|
+
else:
|
44
|
+
|
45
|
+
_, contours, hierachy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE )
|
46
|
+
|
47
|
+
return contours
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
img_raw = cv2.imread("test.png")
|
54
|
+
|
55
|
+
img_drawn = img_raw.copy()
|
56
|
+
|
57
|
+
img_hsv = cv2.cvtColor(img_raw,cv2.COLOR_BGR2HSV)
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
img_bin = cv2.inRange(img_hsv, (0,20,0), (10,255,255))
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
# cv2.imshow("img_bin",img_bin)
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
###
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
contours = detect_contour(img_bin)
|
74
|
+
|
75
|
+
for cnt in contours:
|
76
|
+
|
77
|
+
area = cv2.contourArea(cnt)
|
78
|
+
|
79
|
+
if area > 2000:
|
80
|
+
|
81
|
+
img_bin = cv2.drawContours(img_bin,[cnt],-1,(128), -1)
|
82
|
+
|
83
|
+
img_bin[img_bin==255] = 0
|
84
|
+
|
85
|
+
img_drawn[img_bin==128] = (0,0,255)
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
###
|
90
|
+
|
91
|
+
contours = detect_contour(img_bin)
|
92
|
+
|
93
|
+
for cnt in contours:
|
94
|
+
|
95
|
+
# area = cv2.contourArea(cnt)
|
96
|
+
|
97
|
+
x, y, w, h = cv2.boundingRect(cnt)
|
98
|
+
|
99
|
+
img_drawn = cv2.rectangle(img_drawn,(x,y),(x+w,y+h),(0,255,0),2)
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
# cv2.imshow("img_raw",img_raw)
|
104
|
+
|
105
|
+
# cv2.imshow("img_bin[fixed]",img_bin)
|
106
|
+
|
107
|
+
cv2.imshow("img_drawn",img_drawn)
|
108
|
+
|
109
|
+
cv2.waitKey(0)
|
110
|
+
|
111
|
+
cv2.imwrite("img_drawn.png",img_drawn)
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
cv2.waitKey(0)
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
```
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
---
|
136
|
+
|
23
137
|
さらに精度が必要であれば[機械学習](https://www.pro-s.co.jp/blog/system/opencv/6231)や[深層学習](https://qiita.com/harmegiddo/items/c3db5fd567fa4c6cc9fb)を検討ください。
|