質問編集履歴

3

文字修正

2018/09/13 07:30

投稿

uriuri
uriuri

スコア47

test CHANGED
File without changes
test CHANGED
@@ -332,7 +332,7 @@
332
332
 
333
333
  Traceback (most recent call last):
334
334
 
335
- File "test.py", line 35, in <module>
335
+ File "kamera.py", line 35, in <module>
336
336
 
337
337
  height, width, channels = c_frame.shape
338
338
 

2

エラーの追記

2018/09/13 07:30

投稿

uriuri
uriuri

スコア47

test CHANGED
File without changes
test CHANGED
@@ -330,6 +330,14 @@
330
330
 
331
331
  このようなエラーがでてしまいます
332
332
 
333
+ Traceback (most recent call last):
334
+
335
+ File "test.py", line 35, in <module>
336
+
337
+ height, width, channels = c_frame.shape
338
+
333
339
  AttributeError: 'NoneType' object has no attribute 'shape'
334
340
 
341
+
342
+
335
343
  カメラが画像を読み込めていないことが原因だとおもうのですが、以前動いたものがうごかくなるということはどういったことなのでしょうか

1

添付ミス

2018/09/13 07:22

投稿

uriuri
uriuri

スコア47

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
 
4
4
 
5
+ 以前動いたプログラムを一部書き換え試しました
6
+
5
- 以前動いたプログラムエラーをはくようになってしまいました
7
+ 以前動いたプログラムエラーをはくようになってしまいました
6
8
 
7
9
  ```python
8
10
 
@@ -38,25 +40,25 @@
38
40
 
39
41
 
40
42
 
41
- ORG_WINDOW_NAME = "org"
43
+ ORG_WINDOW_NAME = "ORG"
42
-
44
+
43
- DST_WINDOW_NAME = "syori"
45
+ DST_WINDOW_NAME = "DST"
44
-
45
-
46
-
46
+
47
+
48
+
47
- FILE_NAME1 = "kagei0.avi"
49
+ FILE_NAME1 = "1.avi"
48
-
50
+
49
- FILE_NAME2 = "kagei1.avi"
51
+ FILE_NAME2 = "2.avi"
50
-
52
+
51
- FILE_NAME3 = "kagei2.avi"
53
+ FILE_NAME3 = "3.avi"
52
-
54
+
53
- FILE_NAME4 = "kaghi3.avi"
55
+ FILE_NAME4 = "4.avi"
54
-
56
+
55
- FILE_NAME5 = "kagei4.avi"
57
+ FILE_NAME5 = "5.avi"
56
-
57
-
58
-
58
+
59
+
60
+
59
- DEVICE_ID = 0
61
+ DEVICE_ID = 1
60
62
 
61
63
 
62
64
 
@@ -138,253 +140,179 @@
138
140
 
139
141
  while end_flag == True:
140
142
 
141
-
142
-
143
- gauss = cv2.GaussianBlur(c_frame,(11,11),0)#9,9
144
-
145
- gray = cv2.cvtColor(c_frame,cv2.COLOR_BGR2GRAY)
146
-
147
- #
148
-
149
- ret,th1 = cv2.threshold(gray,200,255,cv2.THRESH_BINARY)
150
-
151
- #
152
-
153
- #th1 = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
154
-
155
- #cv2.THRESH_BINARY,25,10) #11,2
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
- #
164
-
165
- # gauss = cv2.GaussianBlur(th1,(9,9),0)
166
-
167
-
168
-
169
- #
170
-
171
- edges = cv2.Canny(th1,50,150)#(100,150)
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
- img, contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
182
-
183
- for i in range(0, len(contours)):
184
-
185
- cnt = contours[i]
186
-
187
- M = cv2.moments(cnt)
188
-
189
- #print(M)
190
-
191
- area = cv2.contourArea(cnt)
192
-
193
- print(area)
194
-
195
- if 500 < area < 5000:
196
-
197
- # continue
198
-
199
- #if len(contours[i]) > 0:
200
-
201
- #rect = contours[i]
202
-
203
- #x, y, w, h = cv2.boundingRect(rect)
204
-
205
- #cv2.rectangle(img_src, (x, y), (x + w, y + h), (255, 0, 0), 2)
206
-
207
- rect = cv2.minAreaRect(contours[i])
208
-
209
- box = cv2.boxPoints(rect)
210
-
211
- #print(box)
212
-
213
- box = np.int0(box)
214
-
215
- x1,x2,x3,x4 = np.int0(box)
216
-
217
- a = np.array(x1)
218
-
219
- b = np.array(x2)
220
-
221
- c = np.array(x3)
222
-
223
- d = np.array(x4)
224
-
225
- #d = math.sqrt((x4-x1)** 2)
226
-
227
- A = np.linalg.norm(a-b)
228
-
229
- B = np.linalg.norm(a-c)
230
-
231
- C = np.linalg.norm(a-d)
232
-
233
-
234
-
235
- print("頂点",x1,x2,x3,x4)
236
-
237
- print("Aの長さ", A)
238
-
239
- print("Bの長さ",B)
240
-
241
- print("Cの長さ",C)
242
-
243
-
244
-
245
- if A < B:
246
-
247
- min = A
248
-
249
- else:
250
-
251
- min = B
252
-
253
-
254
-
255
- if C < min:
256
-
257
- min = C
258
-
259
-
260
-
261
- print("幅",min)
262
-
263
- if 30 > min > 8:
264
-
265
- im = cv2.drawContours(c_frame,[box],0,(0,255,0),2)
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
- #for c in contours:
274
-
275
- #epsilon = 0.01 * cv2.arcLength(c, True)
276
-
277
- #approx = cv2.approxPolyDP(c, epsilon, True)
278
-
279
-
280
-
281
-
282
-
283
- #cv2.drawContours(c_frame, approx, -1, (255, 255, 255), 3)
284
-
285
- #for i, contour in enumerate(contours):
286
-
287
- #area = cv2.contourArea(contour)
288
-
289
- #if area < 1000: #30
290
-
291
- #continue
143
+ gauss = cv2.GaussianBlur(c_frame, (11, 11), 0)
144
+
145
+ gray = cv2.cvtColor(gauss, cv2.COLOR_BGR2GRAY)
146
+
147
+ ret,th1 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
148
+
149
+ edges = cv2.Canny(th1, 50, 150)
150
+
151
+ img, contours, hierarchy = cv2.findContours(th1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
152
+
153
+ for i in range(0, len(contours)):
154
+
155
+ #for cnt in contours:
156
+
157
+ cnt = contours[i]
158
+
159
+ area = cv2.contourArea(cnt)
160
+
161
+ print(area)
162
+
163
+
164
+
165
+ if 500 < area < 5000:
166
+
167
+ rect = cv2.minAreaRect(cnt)
168
+
169
+ box = cv2.boxPoints(rect)
170
+
171
+
172
+
173
+ (cx, cy), (width, height), angle = rect
174
+
175
+ diag = np.linalg.norm([width, height])
176
+
177
+ print(width, height, diag)
178
+
179
+ # 161.91236877441406 16.144147872924805 162.715237986
180
+
181
+
182
+
183
+ # 長方形の幅、高さ、対角線の長さは cv2.minAreaRect() の返り値
184
+
185
+ # を見ればわかるので計算しなくてもよい。
186
+
187
+ #x1, x2, x3, x4 = box
188
+
189
+ #A = np.linalg.norm(x1 - x2)
190
+
191
+ #B = np.linalg.norm(x1 - x3)
192
+
193
+ #C = np.linalg.norm(x1 - x4)
194
+
195
+ #print("頂点",x1, x2, x3, x4)
196
+
197
+ #print("Aの長さ", A) # Aの長さ 16.1441
198
+
199
+ #print("Bの長さ",B) # Bの長さ 162.715
200
+
201
+ #print("Cの長さ",C) # Cの長さ 161.912
202
+
203
+ A = width
204
+
205
+ B = height
206
+
207
+ C = diag
208
+
209
+
210
+
211
+ if A < B:
212
+
213
+ min = A
214
+
215
+ else:
216
+
217
+ min = B
218
+
219
+ if C < min:
220
+
221
+ min = C
222
+
223
+ print("最小", min)
224
+
225
+ if 5 < min < 25:
226
+
227
+ # drawContours() の contours 引数は int 型でないといけない。
228
+
229
+ cv2.drawContours(c_frame, [box.astype(int)], -1, (0, 255, 0), 2)
230
+
231
+
232
+
233
+
234
+
235
+ def within(line, rect):
236
+
237
+
238
+
239
+ p1, p2 = tuple(line[:2]), tuple(line[2:])
240
+
241
+ # 始点と終点が方形内に含まれるかどうか
242
+
243
+ return cv2.pointPolygonTest(box, p1, False) >= 0 and \
244
+
245
+ cv2.pointPolygonTest(box, p2, False) >= 0
246
+
247
+
248
+
249
+ LSD = cv2.createLineSegmentDetector()
250
+
251
+ lines, width, prec, nfa = LSD.detect(edges)
252
+
253
+ lines = np.squeeze(lines)
254
+
255
+
256
+
257
+ for line in lines:
258
+
259
+ p1, p2 = tuple(line[:2]), tuple(line[2:])
260
+
261
+
262
+
263
+ if within(line, rect):
264
+
265
+ # 長方形に含まれる場合は赤色で描画
266
+
267
+ cv2.line(c_frame, p1, p2, (0, 0, 255), 2)
268
+
269
+ else:
270
+
271
+ # 長方形に含まれない場合は赤色で描画
272
+
273
+ cv2.line(c_frame, p1, p2, (255, 0, 0), 2)
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)
292
288
 
293
289
  #
294
290
 
295
- #if image_size * 0.39 < area: #0.99
296
-
297
- #continue
298
-
299
-
300
-
301
- #x,y,w,h = cv2.boundingRect(edges)
302
-
303
- #print(x)
304
-
305
- #print(y)
291
+ #rec.write(gray)
292
+
306
-
293
+ # rec2.write(c_frame)
294
+
295
+ #rec3.write(th1)
296
+
307
- #print(x + w)
297
+ #rec4.write(edges)
308
-
298
+
309
- #print(y + h)
299
+ #rec5.write(gauss)
310
-
311
-
312
-
313
- #c_frame = cv2.rectangle(c_frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
314
-
315
- LSD = cv2.createLineSegmentDetector()
316
-
317
-
318
-
319
- #
320
-
321
- lines, width, prec, nfa = LSD.detect(edges)
322
-
323
-
324
-
325
- #
326
-
327
- color = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)
328
-
329
-
330
-
331
- if lines is not None:
332
-
333
- for i in range(len(lines)):
334
-
335
- for x1,y1,x2,y2 in lines[i] :
336
-
337
- cv2.line(color,(x1,y1),(x2,y2),(0,0,255),2)
338
-
339
-
340
-
341
- #
342
-
343
- disp_in_img = cv2.cvtColor(color, cv2.COLOR_BGR2RGB)
344
300
 
345
301
 
346
302
 
347
303
  #
348
304
 
349
- cv2.imshow(ORG_WINDOW_NAME, c_frame)
350
-
351
- cv2.imshow('res',gauss)
352
-
353
- cv2.imshow('4', disp_in_img)
354
-
355
- cv2.imshow('3', gray)
356
-
357
- cv2.imshow("1", th1)
305
+ key = cv2.waitKey(INTERVAL)
358
-
306
+
359
- cv2.imshow("2", edges)
307
+ if key == ESC_KEY:
308
+
309
+ break
310
+
311
+
360
312
 
361
313
  #
362
314
 
363
- #rec.write(gray)
364
-
365
- rec2.write(c_frame)
366
-
367
- #rec3.write(th1)
368
-
369
- #rec4.write(edges)
370
-
371
- #rec5.write(gauss)
372
-
373
-
374
-
375
- #
376
-
377
- key = cv2.waitKey(INTERVAL)
378
-
379
- if key == ESC_KEY:
380
-
381
- break
382
-
383
-
384
-
385
- #
386
-
387
- end_flag, c_frame = cap.read()
315
+ end_flag, c_frame = cap.read()
388
316
 
389
317
 
390
318