質問編集履歴
2
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -71,261 +71,3 @@
|
|
71
71
|
のようにすることで大きな矩形や小さな矩形を取り除くことはできますか。
|
72
72
|
|
73
73
|
ほかにいい方法があればアドバイスお願いします。
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
実際に使ったプログラムを載せます。いろんなサイトを参考にして書いたのでぐちゃぐちゃかもしれませんがよろしくおねがいします。
|
80
|
-
|
81
|
-
```ここに言語を入力
|
82
|
-
|
83
|
-
#coding: Shift_Jis
|
84
|
-
|
85
|
-
import cv2
|
86
|
-
|
87
|
-
import math
|
88
|
-
|
89
|
-
import numpy as np
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
# cv2.cv.CV_FOURCC
|
94
|
-
|
95
|
-
def cv_fourcc(c1, c2, c3, c4):
|
96
|
-
|
97
|
-
return (ord(c1) & 255) + ((ord(c2) & 255) << 8) + \
|
98
|
-
|
99
|
-
((ord(c3) & 255) << 16) + ((ord(c4) & 255) << 24)
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
if __name__ == '__main__':
|
106
|
-
|
107
|
-
#
|
108
|
-
|
109
|
-
ESC_KEY = 27 #
|
110
|
-
|
111
|
-
INTERVAL= 33 # 33
|
112
|
-
|
113
|
-
FRAME_RATE = 30 # fps
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
ORG_WINDOW_NAME = "org"
|
118
|
-
|
119
|
-
GAUSSIAN_WINDOW_NAME = "syori"
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
FILE_NAME = "1.avi"
|
124
|
-
|
125
|
-
FILE_NAME2 = "2.avi"
|
126
|
-
|
127
|
-
FILE_NAME3 = "3.avi"
|
128
|
-
|
129
|
-
FILE_NAME4 = "4.avi"
|
130
|
-
|
131
|
-
#FILE_NAME5 = "5.avi"
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
DEVICE_ID = 1
|
136
|
-
|
137
|
-
cap = cv2.VideoCapture(DEVICE_ID)
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
end_flag, c_frame = cap.read()
|
142
|
-
|
143
|
-
height, width, channels = c_frame.shape
|
144
|
-
|
145
|
-
image_size = height * width
|
146
|
-
|
147
|
-
rec = cv2.VideoWriter(FILE_NAME, \
|
148
|
-
|
149
|
-
cv_fourcc('X', 'V', 'I', 'D'), \
|
150
|
-
|
151
|
-
FRAME_RATE, \
|
152
|
-
|
153
|
-
(width, height), \
|
154
|
-
|
155
|
-
True)
|
156
|
-
|
157
|
-
rec2 = cv2.VideoWriter(FILE_NAME2, \
|
158
|
-
|
159
|
-
cv_fourcc('X', 'V', 'I', 'D'), \
|
160
|
-
|
161
|
-
FRAME_RATE, \
|
162
|
-
|
163
|
-
(width, height), \
|
164
|
-
|
165
|
-
True)
|
166
|
-
|
167
|
-
rec3 = cv2.VideoWriter(FILE_NAME3, \
|
168
|
-
|
169
|
-
cv_fourcc('X', 'V', 'I', 'D'), \
|
170
|
-
|
171
|
-
FRAME_RATE, \
|
172
|
-
|
173
|
-
(width, height), \
|
174
|
-
|
175
|
-
True)
|
176
|
-
|
177
|
-
rec4 = cv2.VideoWriter(FILE_NAME4, \
|
178
|
-
|
179
|
-
cv_fourcc('X', 'V', 'I', 'D'), \
|
180
|
-
|
181
|
-
FRAME_RATE, \
|
182
|
-
|
183
|
-
(width, height), \
|
184
|
-
|
185
|
-
True)
|
186
|
-
|
187
|
-
#rec5 = cv2.VideoWriter(FILE_NAME5, \
|
188
|
-
|
189
|
-
#cv_fourcc('X', 'V', 'I', 'D'), \
|
190
|
-
|
191
|
-
##FRAME_RATE, \
|
192
|
-
|
193
|
-
#(width, height), \
|
194
|
-
|
195
|
-
#True)
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
cv2.namedWindow(ORG_WINDOW_NAME)
|
200
|
-
|
201
|
-
cv2.namedWindow(GAUSSIAN_WINDOW_NAME)
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
while end_flag == True:
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
gray = cv2.cvtColor(c_frame,cv2.COLOR_BGR2GRAY)
|
210
|
-
|
211
|
-
ret,th1 = cv2.threshold(gray,200,255,cv2.THRESH_BINARY)
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
gauss = cv2.GaussianBlur(th1,(9,9),0)
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
edges = cv2.Canny(gauss,100,150)
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
img, contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
|
224
|
-
|
225
|
-
for i in range(0, len(contours)):
|
226
|
-
|
227
|
-
area = cv2.contourArea(contours[i])
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
if area < 1e2 or 1e5 < area: #1e2,1e5
|
232
|
-
|
233
|
-
continue
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
if len(contours[i]) > 0:
|
238
|
-
|
239
|
-
rect = contours[i]
|
240
|
-
|
241
|
-
x, y, w, h = cv2.boundingRect(rect)
|
242
|
-
|
243
|
-
if w >= 50 and w <= 1000: #300,450
|
244
|
-
|
245
|
-
if h >= 0 and h <= 500: #0,70
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
cv2.rectangle(c_frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
LSD = cv2.createLineSegmentDetector()
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
lines, width, prec, nfa = LSD.detect(edges)
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
color = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
for i in range(len(lines)):
|
266
|
-
|
267
|
-
for x1,y1,x2,y2 in lines[i] :
|
268
|
-
|
269
|
-
cv2.line(c_frame,(x1,y1),(x2,y2),(0,0,255),2)
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
disp_in_img = cv2.cvtColor(color, cv2.COLOR_BGR2RGB)
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
cv2.imshow(ORG_WINDOW_NAME, c_frame)
|
278
|
-
|
279
|
-
#cv2.imshow('res',gauss)
|
280
|
-
|
281
|
-
cv2.imshow('4', disp_in_img)
|
282
|
-
|
283
|
-
cv2.imshow('3', gray)
|
284
|
-
|
285
|
-
cv2.imshow("1", th1)
|
286
|
-
|
287
|
-
cv2.imshow("2", edges)
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
rec.write(gray)
|
292
|
-
|
293
|
-
rec2.write(c_frame)
|
294
|
-
|
295
|
-
rec3.write(th1)
|
296
|
-
|
297
|
-
rec4.write(edges)
|
298
|
-
|
299
|
-
#rec5.write(gauss)
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
key = cv2.waitKey(INTERVAL)
|
306
|
-
|
307
|
-
if key == ESC_KEY:
|
308
|
-
|
309
|
-
break
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
end_flag, c_frame = cap.read()
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
#
|
320
|
-
|
321
|
-
cv2.destroyAllWindows()
|
322
|
-
|
323
|
-
cap.release()
|
324
|
-
|
325
|
-
rec.release()
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
```
|
1
プログラム添付
test
CHANGED
File without changes
|
test
CHANGED
@@ -71,3 +71,261 @@
|
|
71
71
|
のようにすることで大きな矩形や小さな矩形を取り除くことはできますか。
|
72
72
|
|
73
73
|
ほかにいい方法があればアドバイスお願いします。
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
実際に使ったプログラムを載せます。いろんなサイトを参考にして書いたのでぐちゃぐちゃかもしれませんがよろしくおねがいします。
|
80
|
+
|
81
|
+
```ここに言語を入力
|
82
|
+
|
83
|
+
#coding: Shift_Jis
|
84
|
+
|
85
|
+
import cv2
|
86
|
+
|
87
|
+
import math
|
88
|
+
|
89
|
+
import numpy as np
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
# cv2.cv.CV_FOURCC
|
94
|
+
|
95
|
+
def cv_fourcc(c1, c2, c3, c4):
|
96
|
+
|
97
|
+
return (ord(c1) & 255) + ((ord(c2) & 255) << 8) + \
|
98
|
+
|
99
|
+
((ord(c3) & 255) << 16) + ((ord(c4) & 255) << 24)
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
if __name__ == '__main__':
|
106
|
+
|
107
|
+
#
|
108
|
+
|
109
|
+
ESC_KEY = 27 #
|
110
|
+
|
111
|
+
INTERVAL= 33 # 33
|
112
|
+
|
113
|
+
FRAME_RATE = 30 # fps
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
ORG_WINDOW_NAME = "org"
|
118
|
+
|
119
|
+
GAUSSIAN_WINDOW_NAME = "syori"
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
FILE_NAME = "1.avi"
|
124
|
+
|
125
|
+
FILE_NAME2 = "2.avi"
|
126
|
+
|
127
|
+
FILE_NAME3 = "3.avi"
|
128
|
+
|
129
|
+
FILE_NAME4 = "4.avi"
|
130
|
+
|
131
|
+
#FILE_NAME5 = "5.avi"
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
DEVICE_ID = 1
|
136
|
+
|
137
|
+
cap = cv2.VideoCapture(DEVICE_ID)
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
end_flag, c_frame = cap.read()
|
142
|
+
|
143
|
+
height, width, channels = c_frame.shape
|
144
|
+
|
145
|
+
image_size = height * width
|
146
|
+
|
147
|
+
rec = cv2.VideoWriter(FILE_NAME, \
|
148
|
+
|
149
|
+
cv_fourcc('X', 'V', 'I', 'D'), \
|
150
|
+
|
151
|
+
FRAME_RATE, \
|
152
|
+
|
153
|
+
(width, height), \
|
154
|
+
|
155
|
+
True)
|
156
|
+
|
157
|
+
rec2 = cv2.VideoWriter(FILE_NAME2, \
|
158
|
+
|
159
|
+
cv_fourcc('X', 'V', 'I', 'D'), \
|
160
|
+
|
161
|
+
FRAME_RATE, \
|
162
|
+
|
163
|
+
(width, height), \
|
164
|
+
|
165
|
+
True)
|
166
|
+
|
167
|
+
rec3 = cv2.VideoWriter(FILE_NAME3, \
|
168
|
+
|
169
|
+
cv_fourcc('X', 'V', 'I', 'D'), \
|
170
|
+
|
171
|
+
FRAME_RATE, \
|
172
|
+
|
173
|
+
(width, height), \
|
174
|
+
|
175
|
+
True)
|
176
|
+
|
177
|
+
rec4 = cv2.VideoWriter(FILE_NAME4, \
|
178
|
+
|
179
|
+
cv_fourcc('X', 'V', 'I', 'D'), \
|
180
|
+
|
181
|
+
FRAME_RATE, \
|
182
|
+
|
183
|
+
(width, height), \
|
184
|
+
|
185
|
+
True)
|
186
|
+
|
187
|
+
#rec5 = cv2.VideoWriter(FILE_NAME5, \
|
188
|
+
|
189
|
+
#cv_fourcc('X', 'V', 'I', 'D'), \
|
190
|
+
|
191
|
+
##FRAME_RATE, \
|
192
|
+
|
193
|
+
#(width, height), \
|
194
|
+
|
195
|
+
#True)
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
cv2.namedWindow(ORG_WINDOW_NAME)
|
200
|
+
|
201
|
+
cv2.namedWindow(GAUSSIAN_WINDOW_NAME)
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
while end_flag == True:
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
gray = cv2.cvtColor(c_frame,cv2.COLOR_BGR2GRAY)
|
210
|
+
|
211
|
+
ret,th1 = cv2.threshold(gray,200,255,cv2.THRESH_BINARY)
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
gauss = cv2.GaussianBlur(th1,(9,9),0)
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
edges = cv2.Canny(gauss,100,150)
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
img, contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
|
224
|
+
|
225
|
+
for i in range(0, len(contours)):
|
226
|
+
|
227
|
+
area = cv2.contourArea(contours[i])
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
if area < 1e2 or 1e5 < area: #1e2,1e5
|
232
|
+
|
233
|
+
continue
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
if len(contours[i]) > 0:
|
238
|
+
|
239
|
+
rect = contours[i]
|
240
|
+
|
241
|
+
x, y, w, h = cv2.boundingRect(rect)
|
242
|
+
|
243
|
+
if w >= 50 and w <= 1000: #300,450
|
244
|
+
|
245
|
+
if h >= 0 and h <= 500: #0,70
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
cv2.rectangle(c_frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
LSD = cv2.createLineSegmentDetector()
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
lines, width, prec, nfa = LSD.detect(edges)
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
color = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
for i in range(len(lines)):
|
266
|
+
|
267
|
+
for x1,y1,x2,y2 in lines[i] :
|
268
|
+
|
269
|
+
cv2.line(c_frame,(x1,y1),(x2,y2),(0,0,255),2)
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
disp_in_img = cv2.cvtColor(color, cv2.COLOR_BGR2RGB)
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
cv2.imshow(ORG_WINDOW_NAME, c_frame)
|
278
|
+
|
279
|
+
#cv2.imshow('res',gauss)
|
280
|
+
|
281
|
+
cv2.imshow('4', disp_in_img)
|
282
|
+
|
283
|
+
cv2.imshow('3', gray)
|
284
|
+
|
285
|
+
cv2.imshow("1", th1)
|
286
|
+
|
287
|
+
cv2.imshow("2", edges)
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
rec.write(gray)
|
292
|
+
|
293
|
+
rec2.write(c_frame)
|
294
|
+
|
295
|
+
rec3.write(th1)
|
296
|
+
|
297
|
+
rec4.write(edges)
|
298
|
+
|
299
|
+
#rec5.write(gauss)
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
key = cv2.waitKey(INTERVAL)
|
306
|
+
|
307
|
+
if key == ESC_KEY:
|
308
|
+
|
309
|
+
break
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
end_flag, c_frame = cap.read()
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
#
|
320
|
+
|
321
|
+
cv2.destroyAllWindows()
|
322
|
+
|
323
|
+
cap.release()
|
324
|
+
|
325
|
+
rec.release()
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
```
|