質問編集履歴
6
コード修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -193,4 +193,82 @@
|
|
193
193
|
cv2.waitKey()
|
194
194
|
cv2.destroyAllWindows()
|
195
195
|
|
196
|
+
```
|
197
|
+
修正後
|
198
|
+
```python
|
199
|
+
import cv2
|
200
|
+
import math
|
201
|
+
import numpy as np
|
202
|
+
|
203
|
+
img_src = cv2.imread("test.jpg")
|
204
|
+
|
205
|
+
gauss = cv2.GaussianBlur(img_src, (11, 11), 0)
|
206
|
+
gray = cv2.cvtColor(gauss, cv2.COLOR_BGR2GRAY)
|
207
|
+
ret,th1 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
|
208
|
+
edges = cv2.Canny(th1, 50, 150)
|
209
|
+
|
210
|
+
img, contours, hierarchy = cv2.findContours(th1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
|
211
|
+
for i in range(0, len(contours)):
|
212
|
+
#for cnt in contours:
|
213
|
+
cnt = contours[i]
|
214
|
+
area = cv2.contourArea(cnt)
|
215
|
+
#print(area)
|
216
|
+
|
217
|
+
if 500 < area < 5000:
|
218
|
+
rect = cv2.minAreaRect(cnt)
|
219
|
+
box = cv2.boxPoints(rect)
|
220
|
+
|
221
|
+
(cx, cy), (width, height), angle = rect
|
222
|
+
diag = np.linalg.norm([width, height])
|
223
|
+
#print(width, height, diag)
|
224
|
+
# 161.91236877441406 16.144147872924805 162.715237986
|
225
|
+
|
226
|
+
#
|
227
|
+
A = width
|
228
|
+
B = height
|
229
|
+
C = diag
|
230
|
+
|
231
|
+
if A < B:
|
232
|
+
min = A
|
233
|
+
else:
|
234
|
+
min = B
|
235
|
+
if C < min:
|
236
|
+
min = C
|
237
|
+
print("min", min)
|
238
|
+
if 5 < min < 25:
|
239
|
+
#
|
240
|
+
cv2.drawContours(img_src, [box.astype(int)], -1, (0, 255, 0), 2)
|
241
|
+
|
242
|
+
def within(line, rect):
|
243
|
+
|
244
|
+
p1, p2 = tuple(line[:2]), tuple(line[2:])
|
245
|
+
#print("p1",p1)
|
246
|
+
#print("p2",p2)
|
247
|
+
#print("line",line)
|
248
|
+
return cv2.pointPolygonTest(box, p1, False) >= 0 and \
|
249
|
+
cv2.pointPolygonTest(box, p2, False) >= 0
|
250
|
+
|
251
|
+
LSD = cv2.createLineSegmentDetector()
|
252
|
+
lines, width, prec, nfa = LSD.detect(edges)
|
253
|
+
if lines is not None:
|
254
|
+
lines = np.squeeze(lines,axis=1)
|
255
|
+
#if lines.dims == 1:
|
256
|
+
# lines = np.expand_dims(lines, axis=0)
|
257
|
+
for line in lines:
|
258
|
+
p1, p2 = tuple(line[:2]), tuple(line[2:])
|
259
|
+
print("line",line)
|
260
|
+
|
261
|
+
if within(line, rect):
|
262
|
+
|
263
|
+
cv2.line(img_src, p1, p2, (0, 0, 255), 2)
|
264
|
+
#else:
|
265
|
+
|
266
|
+
# cv2.line(img_src, p1, p2, (255, 0, 0), 2)
|
267
|
+
|
268
|
+
|
269
|
+
|
270
|
+
cv2.imshow('src', img_src)
|
271
|
+
cv2.waitKey()
|
272
|
+
cv2.destroyAllWindows()
|
273
|
+
|
196
274
|
```
|
5
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -102,4 +102,95 @@
|
|
102
102
|
.
|
103
103
|
.
|
104
104
|
```
|
105
|
-
コードが見づらくて申し訳ありません
|
105
|
+
コードが見づらくて申し訳ありません
|
106
|
+
|
107
|
+
```python
|
108
|
+
import cv2
|
109
|
+
import math
|
110
|
+
import numpy as np
|
111
|
+
|
112
|
+
img_src = cv2.imread("./haku/sample/image1.ppm")
|
113
|
+
|
114
|
+
gauss = cv2.GaussianBlur(img_src,(11,11),0)#9,9
|
115
|
+
gray = cv2.cvtColor(gauss,cv2.COLOR_BGR2GRAY)
|
116
|
+
ret,th1 = cv2.threshold(gray,200,255,cv2.THRESH_BINARY)
|
117
|
+
edges = cv2.Canny(th1,50,150)#(100,150)
|
118
|
+
|
119
|
+
|
120
|
+
img, contours, hierarchy = cv2.findContours(th1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
|
121
|
+
if len(contours) > 0:
|
122
|
+
for i in range(0, len(contours)):
|
123
|
+
cnt = contours[i]
|
124
|
+
M = cv2.moments(cnt)
|
125
|
+
#print(M)
|
126
|
+
area = cv2.contourArea(cnt)
|
127
|
+
print(area)
|
128
|
+
if 500 < area < 5000:
|
129
|
+
rect = cv2.minAreaRect(contours[i])
|
130
|
+
box = cv2.boxPoints(rect)
|
131
|
+
box = np.int0(box)
|
132
|
+
x1,x2,x3,x4 = np.int0(box)
|
133
|
+
a = np.array(x1)
|
134
|
+
b = np.array(x2)
|
135
|
+
c = np.array(x3)
|
136
|
+
d = np.array(x4)
|
137
|
+
|
138
|
+
A = np.linalg.norm(a-b)
|
139
|
+
B = np.linalg.norm(a-c)
|
140
|
+
C = np.linalg.norm(a-d)
|
141
|
+
|
142
|
+
print("頂点",x1,x2,x3,x4)
|
143
|
+
#print("abcd", a,b,c,d)
|
144
|
+
#print("Aの長さ", A)
|
145
|
+
#print("Bの長さ",B)
|
146
|
+
#print("Cの長さ",C)
|
147
|
+
|
148
|
+
if A < B:
|
149
|
+
min = A
|
150
|
+
else:
|
151
|
+
min = B
|
152
|
+
if C < min:
|
153
|
+
min = C
|
154
|
+
print("最小",min)
|
155
|
+
if 5 < min < 25:
|
156
|
+
im = cv2.drawContours(img_src,[box],0,(0,255,0),2)
|
157
|
+
|
158
|
+
|
159
|
+
def within(line, rect):
|
160
|
+
"""line が rect に含まれるかどうか
|
161
|
+
"""
|
162
|
+
p1, p2 = tuple(line[:2]), tuple(line[2:])
|
163
|
+
# 線の始点と終点が長方形内に含まれるかどうか
|
164
|
+
return cv2.pointPolygonTest(box, p1, False) >= 0 and \
|
165
|
+
cv2.pointPolygonTest(box, p2, False) >= 0
|
166
|
+
|
167
|
+
LSD = cv2.createLineSegmentDetector()
|
168
|
+
lines, width, prec, nfa = LSD.detect(edges)
|
169
|
+
print(lines.shape)
|
170
|
+
lines = np.squeeze(lines)
|
171
|
+
|
172
|
+
if lines is not None:
|
173
|
+
for i in range(len(lines)):
|
174
|
+
|
175
|
+
#for x1,y1,x2,y2 in lines[i] :
|
176
|
+
# cv2.line(img_src,(x1,y1),(x2,y2),(0,0,255),2)
|
177
|
+
# X1 = (x1, y1)
|
178
|
+
# X2 = (x2, y2)
|
179
|
+
# print("X1", X1)
|
180
|
+
#print("X2", X2)
|
181
|
+
for line in lines:
|
182
|
+
p1, p2 = tuple(lines[:2]), tuple(line[2:])
|
183
|
+
if within(line, rect):
|
184
|
+
# 長方形に含まれる場合は赤色で描画
|
185
|
+
cv2.line(img_src, p1, p2, (0, 0, 255), 2)
|
186
|
+
else:
|
187
|
+
# 長方形に含まれない場合は赤色で描画
|
188
|
+
cv2.line(img_src, p1, p2, (255, 0, 0), 2)
|
189
|
+
|
190
|
+
|
191
|
+
#cv2.imwrite("teratail.jpg", img_src)
|
192
|
+
cv2.imshow('src', img_src)
|
193
|
+
cv2.waitKey()
|
194
|
+
cv2.destroyAllWindows()
|
195
|
+
|
196
|
+
```
|
4
コードの追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -60,7 +60,26 @@
|
|
60
60
|
b = np.array(x2)
|
61
61
|
c = np.array(x3)
|
62
62
|
d = np.array(x4)
|
63
|
+
|
64
|
+
A = np.linalg.norm(a-b)
|
65
|
+
B = np.linalg.norm(a-c)
|
66
|
+
C = np.linalg.norm(a-d)
|
67
|
+
|
68
|
+
print("頂点",x1,x2,x3,x4)
|
69
|
+
#print("abcd", a,b,c,d)
|
70
|
+
#print("Aの長さ", A)
|
71
|
+
#print("Bの長さ",B)
|
72
|
+
#print("Cの長さ",C)
|
73
|
+
|
74
|
+
if A < B:
|
75
|
+
min = A
|
76
|
+
else:
|
77
|
+
min = B
|
78
|
+
if C < min:
|
79
|
+
min = C
|
80
|
+
print("最小",min)
|
81
|
+
if 5 < min < 25:
|
63
|
-
|
82
|
+
im = cv2.drawContours(img_src,[box],0,(0,255,0),2)
|
64
83
|
|
65
84
|
#直線検出
|
66
85
|
LSD = cv2.createLineSegmentDetector()
|
3
画像の挿入
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
+

|
1
|
-
|
2
|
+

|
2
3
|
|
3
4
|
緑色の矩形内に赤い直線がある場合,白い線としたいと思っています
|
4
5
|
|
@@ -30,9 +31,17 @@
|
|
30
31
|
この場合条件文で比較するためのアドバイスをいただけたら幸いです
|
31
32
|
|
32
33
|
```python
|
33
|
-
|
34
|
+
import cv2
|
34
|
-
|
35
|
+
import math
|
35
|
-
|
36
|
+
import numpy as np
|
37
|
+
|
38
|
+
img_src = cv2.imread("./haku/sample/image1.ppm")
|
39
|
+
|
40
|
+
gauss = cv2.GaussianBlur(img_src,(11,11),0)#9,9
|
41
|
+
gray = cv2.cvtColor(gauss,cv2.COLOR_BGR2GRAY)
|
42
|
+
ret,th1 = cv2.threshold(gray,200,255,cv2.THRESH_BINARY)
|
43
|
+
edges = cv2.Canny(th1,50,150)#(100,150)
|
44
|
+
|
36
45
|
#矩形検出
|
37
46
|
img, contours, hierarchy = cv2.findContours(th1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
|
38
47
|
if len(contours) > 0:
|
2
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -27,4 +27,51 @@
|
|
27
27
|
|
28
28
|
というエラーがでてしまいます
|
29
29
|
これは条件文に対してX1がどの値かが明確ではないため起こる問題であっていますでしょうか
|
30
|
-
この場合条件文で比較するためのアドバイスをいただけたら幸いです
|
30
|
+
この場合条件文で比較するためのアドバイスをいただけたら幸いです
|
31
|
+
|
32
|
+
```python
|
33
|
+
.
|
34
|
+
.
|
35
|
+
.
|
36
|
+
#矩形検出
|
37
|
+
img, contours, hierarchy = cv2.findContours(th1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
|
38
|
+
if len(contours) > 0:
|
39
|
+
for i in range(0, len(contours)):
|
40
|
+
cnt = contours[i]
|
41
|
+
M = cv2.moments(cnt)
|
42
|
+
#print(M)
|
43
|
+
area = cv2.contourArea(cnt)
|
44
|
+
print(area)
|
45
|
+
if 500 < area < 5000:
|
46
|
+
rect = cv2.minAreaRect(contours[i])
|
47
|
+
box = cv2.boxPoints(rect)
|
48
|
+
box = np.int0(box)
|
49
|
+
x1,x2,x3,x4 = np.int0(box)
|
50
|
+
a = np.array(x1)
|
51
|
+
b = np.array(x2)
|
52
|
+
c = np.array(x3)
|
53
|
+
d = np.array(x4)
|
54
|
+
im = cv2.drawContours(img_src,[box],0,(0,255,0),2)
|
55
|
+
|
56
|
+
#直線検出
|
57
|
+
LSD = cv2.createLineSegmentDetector()
|
58
|
+
lines, width, prec, nfa = LSD.detect(edges)
|
59
|
+
if lines is not None:
|
60
|
+
for i in range(len(lines)):
|
61
|
+
|
62
|
+
for x1,y1,x2,y2 in lines[i] :
|
63
|
+
cv2.line(img_src,(x1,y1),(x2,y2),(0,0,255),2)
|
64
|
+
X1 = (x1, y1)
|
65
|
+
X2 = (x2, y2)
|
66
|
+
print("X1", X1)
|
67
|
+
print("X2", X2)
|
68
|
+
|
69
|
+
jug1 = cv2.pointPolygonTest(rect, X1, False)
|
70
|
+
jug2 = cv2.pointPolygonTest(rect, X2, False)
|
71
|
+
print(jug1)
|
72
|
+
print(jug2)
|
73
|
+
.
|
74
|
+
.
|
75
|
+
.
|
76
|
+
```
|
77
|
+
コードが見づらくて申し訳ありません
|
1
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -21,7 +21,7 @@
|
|
21
21
|
・
|
22
22
|
・
|
23
23
|
Traceback (most recent call last):
|
24
|
-
File "
|
24
|
+
File "○○○.py", line 101, in <module>
|
25
25
|
if a < X1 < b:
|
26
26
|
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
|
27
27
|
|