質問するログイン新規登録

質問編集履歴

2

コード修正しました。

2019/11/19 04:31

投稿

reonald
reonald

スコア32

title CHANGED
File without changes
body CHANGED
@@ -8,7 +8,7 @@
8
8
  import glob
9
9
 
10
10
 
11
- files = glob.glob("./p1/./1/*")
11
+ files = glob.glob("./3/./1.2/*")
12
12
  for f in files:
13
13
  img = cv2.imread(f)
14
14
  cut = img[460:1270, 130:940]
@@ -48,7 +48,9 @@
48
48
  h, s, v = cv2.split(hsv)
49
49
  mean = v[v < 230].mean()
50
50
  if mean < 170:
51
- cv2.imwrite('./phyto/debug_%d.jpg' % i, im2)
51
+ save_path = './phyto/debug_%d.jpg' % i
52
+ print(save_path)
53
+ cv2.imwrite(save_path, im2)
52
54
  else :
53
55
  y, x = im2.shape[:2]
54
56
  a=max(y,x)
@@ -57,17 +59,38 @@
57
59
  if a < 100:
58
60
  continue
59
61
  elif 100 < a < 250:
60
- cv2.imwrite('./pu1/debug_%d.jpg' % i, im2)
62
+ save_path = './pu1/debug_%d.jpg' % i
63
+ print(save_path)
64
+ cv2.imwrite(save_path, im2)
61
65
  elif 250 < a < 400:
62
- cv2.imwrite('./pu2/debug_%d.jpg' % i, im2)
66
+ save_path = './pu2/debug_%d.jpg' % i
67
+ print(save_path)
68
+ cv2.imwrite(save_path, im2)
63
69
  elif 400 < a :
64
- cv2.imwrite('./pu3/debug_%d.jpg' % i, im2)
70
+ save_path = './pu3/debug_%d.jpg' % i
71
+ print(save_path)
72
+ cv2.imwrite(save_path, im2)
65
73
  コード
66
74
  ```
75
+
67
- となっているのですが,
76
+ となっているのですが,実行すると,
77
+ 133 149 149 大きさ:0.10081190798376183
78
+ ./pu1/debug_0.jpg
79
+ 111 95 111 大きさ:0.07510148849797023
80
+ ./pu1/debug_0.jpg
81
+ ./phyto/debug_1.jpg
82
+ 127 59 127 大きさ:0.08592692828146144
83
+ ./pu1/debug_2.jpg
84
+ 809 598 809 大きさ:0.547361299052774
85
+ ./pu3/debug_3.jpg
86
+ 118 202 202 大きさ:0.13667117726657646
87
+ ./pu1/debug_0.jpg
88
+ 93 104 104 大きさ:0.07036535859269283
89
+ ./pu1/debug_0.jpg
90
+ となり,
68
91
  保存するときにdebug0.jpgなどが上書きされてしまっております。
69
- 保存するとき名前に1つ目のforの処理の番号入れれば解決すると思うのですが,
92
+ おそらく,初めのforの処理をするときに,毎回debug_0.jpgという名前で保存するプログラムになってしまっていると思うのですが,
70
- ようなコードにすればよが分かりません
93
+ 初めforのでn番目の画像処理をするときdebug-n_0.jpgなどの名前で保存ることはできなでしょうか。
71
94
  コード修正宜しくお願いします。
72
95
 
73
96
  pythonのversionは3.7.3で,

1

コード修正しました。

2019/11/19 04:31

投稿

reonald
reonald

スコア32

title CHANGED
File without changes
body CHANGED
@@ -1,71 +1,74 @@
1
1
  現在のコードとして,
2
+ ```
2
- ```from pathlib import Path
3
+ from pathlib import Path
3
4
  import cv2
4
5
  import numpy as np
5
6
  import shutil
6
7
  import matplotlib.pyplot as plt
8
+ import glob
7
9
 
8
10
 
11
+ files = glob.glob("./p1/./1/*")
12
+ for f in files:
13
+ img = cv2.imread(f)
14
+ cut = img[460:1270, 130:940]
15
+ z=1*810/1478
9
16
 
17
+ gray = cv2.cvtColor(cut, cv2.COLOR_BGR2GRAY)
10
- img = cv2.imread('./p1/./1/S__433733663.jpg')
18
+ ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
11
- cut = img[460:1270, 130:940]
12
- z=1*810/1478
13
19
 
14
- gray = cv2.cvtColor(cut, cv2.COLOR_BGR2GRAY)
20
+ kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
21
+ binary = cv2.dilate(binary, kernel)
15
- ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
22
+ contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
16
23
 
17
- kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
18
- binary = cv2.dilate(binary, kernel)
19
- contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
20
24
 
25
+ rects = []
26
+ for cnt in contours:
27
+ if cv2.contourArea(cnt) < 4000:
28
+ continue
21
29
 
22
- rects = []
23
- for cnt in contours:
24
- if cv2.contourArea(cnt) < 4000:
30
+ rect = cv2.minAreaRect(cnt)
25
- continue
31
+ rects.append(rect)
26
32
 
27
- rect = cv2.minAreaRect(cnt)
28
- rects.append(rect)
33
+ def crop(cut, rect):
34
+ center, size, angle = rect
35
+ center = tuple(map(int, center))
36
+ size = tuple(map(int, size))
37
+ j, k = cut.shape[:2]
38
+ M = cv2.getRotationMatrix2D(center, angle, 1)
39
+ rotated = cv2.warpAffine(cut, M, (k, j))
29
40
 
30
- def crop(cut, rect):
31
- center, size, angle = rect
32
- center = tuple(map(int, center))
33
- size = tuple(map(int, size))
34
- j, k = cut.shape[:2]
35
- M = cv2.getRotationMatrix2D(center, angle, 1)
41
+ im2 = cv2.getRectSubPix(rotated, size, center)
36
- rotated = cv2.warpAffine(cut, M, (k, j))
42
+ return im2
37
43
 
38
- im2 = cv2.getRectSubPix(rotated, size, center)
39
- return im2
40
44
 
41
-
42
- for i, rect in enumerate(rects):
45
+ for i, rect in enumerate(rects):
43
- im2 = crop(cut, rect)
46
+ im2 = crop(cut, rect)
44
- hsv = cv2.cvtColor(im2, cv2.COLOR_BGR2HSV)
47
+ hsv = cv2.cvtColor(im2, cv2.COLOR_BGR2HSV)
45
- h, s, v = cv2.split(hsv)
48
+ h, s, v = cv2.split(hsv)
46
- mean = v[v < 230].mean()
49
+ mean = v[v < 230].mean()
47
- if mean < 170:
50
+ if mean < 170:
48
- cv2.imwrite('./phyto/debug_%d.jpg' % i, im2)
51
+ cv2.imwrite('./phyto/debug_%d.jpg' % i, im2)
49
- else :
52
+ else :
50
- y, x = im2.shape[:2]
53
+ y, x = im2.shape[:2]
51
- a=max(y,x)
54
+ a=max(y,x)
52
- b=z*a/810
55
+ b=z*a/810
53
- print('','',y,x,a,'大きさ:{0}'.format(b))
56
+ print('','',y,x,a,'大きさ:{0}'.format(b))
54
- if a < 100:
57
+ if a < 100:
55
- continue
58
+ continue
56
- elif 100 < a < 250:
59
+ elif 100 < a < 250:
57
- cv2.imwrite('./pu1/debug_%d.jpg' % i, im2)
60
+ cv2.imwrite('./pu1/debug_%d.jpg' % i, im2)
58
- elif 250 < a < 400:
61
+ elif 250 < a < 400:
59
- cv2.imwrite('./pu2/debug_%d.jpg' % i, im2)
62
+ cv2.imwrite('./pu2/debug_%d.jpg' % i, im2)
60
- elif 400 < a :
63
+ elif 400 < a :
61
- cv2.imwrite('./pu3/debug_%d.jpg' % i, im2)
64
+ cv2.imwrite('./pu3/debug_%d.jpg' % i, im2)
62
65
  コード
63
66
  ```
64
- となっており,これはp1ファイルの1とうファイルの中の画像を選んで,処理しているのですが,
67
+ となっているのですが,
65
- をp1の1のファイルの中の画像全に対て一括で処理したいとおもっております。
68
+ 保存するときにdebug0.jpgなどが上書きされてしっております。
66
- プログラムの内容しては,画像をリサイズし,二値化などした後,回転矩形を切り出し,hsvによって分類,その後,大分類し,ファイルへ保存なっておりま
69
+ 保存するときの名前1つ目のforの処理の番号を入れれば解決すると思うのでが,
70
+ どのようなコードにすればよいのかが分かりません。
71
+ コード修正宜しくお願いします。
67
72
 
68
- どなたかよろしくお願いします。
69
-
70
73
  pythonのversionは3.7.3で,
71
74
  opencvのversionは4.1.1です。