質問編集履歴
4
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,21 +2,11 @@
|
|
2
2
|
|
3
3
|
こちらの質問を参考に、画像の輪郭を抽出させ、それぞれの面積を表示させるコードを書きました。
|
4
4
|
|
5
|
-
そのコードに、「面積が50以上で白色の場合、指定した色で塗りつぶす」というコードを追記したいのですが、うまくいきません。
|
6
|
-
|
7
|
-
https://teratail.com/questions/169498
|
5
|
+
そのコードに、「面積が100以上で白色(RGB(230,230,230以上)の場合、指定した色(RGB(16,16,16)で塗りつぶす」というコードを追記したですが、面積が100以上の部分以外も、白色(RGB(230,230,230以上)の部分は全て塗りつぶされてしまいます。https://teratail.com/questions/169498
|
8
6
|
|
9
7
|
お手数ですがコードの修正方法についてアドバイスをいただけますととても助かります。
|
10
8
|
|
11
9
|
どうぞよろしくお願いいたします。
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
### 発生している問題・エラーメッセージ
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
エラーは発生しないのですが、その代わり塗りつぶしもされていない状態です。
|
20
10
|
|
21
11
|
|
22
12
|
|
@@ -26,59 +16,37 @@
|
|
26
16
|
|
27
17
|
```Python
|
28
18
|
|
19
|
+
import glob
|
20
|
+
|
21
|
+
import os
|
22
|
+
|
23
|
+
from PIL import Image
|
24
|
+
|
29
25
|
import cv2
|
30
|
-
|
31
|
-
import numpy as np
|
32
26
|
|
33
27
|
import matplotlib.pyplot as plt
|
34
28
|
|
35
|
-
from
|
29
|
+
from google.colab.patches import cv2_imshow
|
36
30
|
|
37
31
|
|
38
32
|
|
39
|
-
# 画像を読み込む。
|
40
33
|
|
34
|
+
|
41
|
-
i
|
35
|
+
input_dir = 'NORMAL_resize_copy_100_1_0506' # 画像があるディレクトリ
|
36
|
+
|
37
|
+
output_dir = 'NORMAL_resize_pixel_100_1_0506' # 出力するディレクトリ
|
42
38
|
|
43
39
|
|
44
40
|
|
45
|
-
# 2値化
|
46
|
-
|
47
|
-
thresh, binary = cv2.threshold(
|
48
|
-
|
49
|
-
|
41
|
+
os.makedirs(output_dir, exist_ok=True) # 保存するディレクトリ
|
50
42
|
|
51
43
|
|
52
44
|
|
53
|
-
|
45
|
+
assert(os.path.exists(input_dir)), "directory '{}' not found".format(input_dir)
|
54
46
|
|
55
|
-
|
47
|
+
for path in glob.glob(input_dir + "/*.jpeg"):
|
56
48
|
|
57
|
-
binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=2)
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
# 輪郭抽出する。(OpenCV 3)
|
62
|
-
|
63
|
-
_, contours, hierarchy = cv2.findContours(
|
64
|
-
|
65
|
-
binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
# OpenCV4 の場合
|
70
|
-
|
71
|
-
#contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
def draw_contours(ax, img, contours):
|
76
|
-
|
77
|
-
ax.imshow(img, cmap='gray')
|
78
|
-
|
79
|
-
|
49
|
+
img = cv2.imread(path,0)
|
80
|
-
|
81
|
-
fill_color = [16, 16, 16]
|
82
50
|
|
83
51
|
for i, cnt in enumerate(contours):
|
84
52
|
|
@@ -100,59 +68,19 @@
|
|
100
68
|
|
101
69
|
print('contour: {}, area: {}'.format(i, area))
|
102
70
|
|
103
|
-
#
|
71
|
+
#白色の面積が100以上かつRGBが(230,230,230以上)の場合、RGB(16,16,16)で塗りつぶす
|
104
72
|
|
105
|
-
if area >
|
73
|
+
if area > 100:
|
106
74
|
|
107
|
-
img[
|
75
|
+
img[img >= 230] = 16
|
108
76
|
|
109
|
-
|
77
|
+
#plt.show()
|
110
78
|
|
111
|
-
im
|
79
|
+
name, ext = os.path.splitext(os.path.basename(path)) # 拡張子を除いたファイル名
|
112
80
|
|
113
|
-
|
81
|
+
save_path = os.path.join(output_dir, '{}_pixel.jpeg'.format(name)) # 保存するパス
|
114
82
|
|
115
|
-
img[:] = fill_color
|
116
|
-
|
117
|
-
elif area > 50 and img == (248, 248, 248):
|
118
|
-
|
119
|
-
img[:] = fill_color
|
120
|
-
|
121
|
-
elif area > 50 and img == (249, 249, 249):
|
122
|
-
|
123
|
-
img[:] = fill_color
|
124
|
-
|
125
|
-
elif area > 50 and img == (250, 250, 250):
|
126
|
-
|
127
|
-
img[:] = fill_color
|
128
|
-
|
129
|
-
elif area > 50 and img == (251, 251, 251):
|
130
|
-
|
131
|
-
img[:] = fill_color
|
132
|
-
|
133
|
-
elif area > 50 and img == (252, 252, 252):
|
134
|
-
|
135
|
-
img[:] = fill_color
|
136
|
-
|
137
|
-
elif area > 50 and img == (253, 253, 253):
|
138
|
-
|
139
|
-
img[:] = fill_color
|
140
|
-
|
141
|
-
elif area > 50 and img == (254, 254, 254):
|
142
|
-
|
143
|
-
img[:] = fill_color
|
144
|
-
|
145
|
-
elif area > 50 and img == (255, 255, 255):
|
146
|
-
|
147
|
-
img[:] = fill_color
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
fig, ax = plt.subplots(figsize=(6, 6))
|
152
|
-
|
153
|
-
|
83
|
+
cv2.imwrite(save_path, img)
|
154
|
-
|
155
|
-
plt.show()
|
156
84
|
|
157
85
|
|
158
86
|
|
3
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -104,47 +104,47 @@
|
|
104
104
|
|
105
105
|
if area > 50 and img == (245, 245, 245):
|
106
106
|
|
107
|
-
img[:
|
107
|
+
img[:] = fill_color
|
108
108
|
|
109
109
|
elif area > 50 and img == (246, 246, 246):
|
110
110
|
|
111
|
-
img[:
|
111
|
+
img[:] = fill_color
|
112
112
|
|
113
113
|
elif area > 50 and img == (247, 247, 247):
|
114
114
|
|
115
|
-
img[:
|
115
|
+
img[:] = fill_color
|
116
116
|
|
117
117
|
elif area > 50 and img == (248, 248, 248):
|
118
118
|
|
119
|
-
img[:
|
119
|
+
img[:] = fill_color
|
120
120
|
|
121
121
|
elif area > 50 and img == (249, 249, 249):
|
122
122
|
|
123
|
-
img[:
|
123
|
+
img[:] = fill_color
|
124
124
|
|
125
125
|
elif area > 50 and img == (250, 250, 250):
|
126
126
|
|
127
|
-
img[:
|
127
|
+
img[:] = fill_color
|
128
128
|
|
129
129
|
elif area > 50 and img == (251, 251, 251):
|
130
130
|
|
131
|
-
img[:
|
131
|
+
img[:] = fill_color
|
132
132
|
|
133
133
|
elif area > 50 and img == (252, 252, 252):
|
134
134
|
|
135
|
-
img[:
|
135
|
+
img[:] = fill_color
|
136
136
|
|
137
137
|
elif area > 50 and img == (253, 253, 253):
|
138
138
|
|
139
|
-
img[:
|
139
|
+
img[:] = fill_color
|
140
140
|
|
141
141
|
elif area > 50 and img == (254, 254, 254):
|
142
142
|
|
143
|
-
img[:
|
143
|
+
img[:] = fill_color
|
144
144
|
|
145
145
|
elif area > 50 and img == (255, 255, 255):
|
146
146
|
|
147
|
-
img[:
|
147
|
+
img[:] = fill_color
|
148
148
|
|
149
149
|
|
150
150
|
|
2
エラーメッセージ部分の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,41 +16,7 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
error Traceback (most recent call last)
|
22
|
-
|
23
|
-
<ipython-input-14-772dbf8c0e1c> in <module>()
|
24
|
-
|
25
|
-
43
|
26
|
-
|
27
|
-
44 fig, ax = plt.subplots(figsize=(6, 6))
|
28
|
-
|
29
|
-
---> 45 draw_contours(ax, img, contours)
|
30
|
-
|
31
|
-
46 plt.show()
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
<ipython-input-14-772dbf8c0e1c> in draw_contours(ax, img, contours)
|
36
|
-
|
37
|
-
38 area = cv2.contourArea(cnt)
|
38
|
-
|
39
|
-
39 print('contour: {}, area: {}'.format(i, area))
|
40
|
-
|
41
|
-
---> 40 if area > 50 and cv2.inRange(img, (245, 245, 245), (255, 255, 255)):
|
42
|
-
|
43
|
-
41 img[:,:,:] = fill_color
|
44
|
-
|
45
|
-
42
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
error: OpenCV(3.4.3) /io/opencv/modules/core/src/arithm.cpp:1778: error: (-209:Sizes of input arguments do not match) The lower boundary is neither an array of the same size and same type as src, nor a scalar in function 'inRange'
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
```
|
19
|
+
エラーは発生しないのですが、その代わり塗りつぶしもされていない状態です。
|
54
20
|
|
55
21
|
|
56
22
|
|
1
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -112,13 +112,11 @@
|
|
112
112
|
|
113
113
|
ax.axis('off')
|
114
114
|
|
115
|
-
white = cv2.inRange(img, (245, 245, 245), (255, 255, 255))# ???
|
116
|
-
|
117
|
-
fill_color = [16, 16, 16]
|
115
|
+
fill_color = [16, 16, 16]
|
118
116
|
|
119
117
|
for i, cnt in enumerate(contours):
|
120
118
|
|
121
|
-
cnt = np.squeeze(cnt, axis=1)
|
119
|
+
cnt = np.squeeze(cnt, axis=1) # (NumPoints, 1, 2) -> (NumPoints, 2)
|
122
120
|
|
123
121
|
# 輪郭の点同士を結ぶ線を描画する。
|
124
122
|
|
@@ -132,17 +130,63 @@
|
|
132
130
|
|
133
131
|
ax.text(cnt[0][0], cnt[0][1], i, color='orange', size='20')
|
134
132
|
|
135
|
-
# 輪郭の面積を表示する。
|
136
|
-
|
137
133
|
area = cv2.contourArea(cnt)
|
138
134
|
|
139
135
|
print('contour: {}, area: {}'.format(i, area))
|
140
136
|
|
141
|
-
# 白色の面積が50以上の場合、
|
137
|
+
# 白色の面積が50以上の場合、(16,16,16)の色で塗りつぶす
|
142
138
|
|
143
|
-
if area > 50 and img =
|
139
|
+
if area > 50 and img == (245, 245, 245):
|
144
140
|
|
145
|
-
img[:,:,:] = fill_color
|
141
|
+
img[:, :, :] == fill_color
|
142
|
+
|
143
|
+
elif area > 50 and img == (246, 246, 246):
|
144
|
+
|
145
|
+
img[:, :, :] == fill_color
|
146
|
+
|
147
|
+
elif area > 50 and img == (247, 247, 247):
|
148
|
+
|
149
|
+
img[:, :, :] == fill_color
|
150
|
+
|
151
|
+
elif area > 50 and img == (248, 248, 248):
|
152
|
+
|
153
|
+
img[:, :, :] == fill_color
|
154
|
+
|
155
|
+
elif area > 50 and img == (249, 249, 249):
|
156
|
+
|
157
|
+
img[:, :, :] == fill_color
|
158
|
+
|
159
|
+
elif area > 50 and img == (250, 250, 250):
|
160
|
+
|
161
|
+
img[:, :, :] == fill_color
|
162
|
+
|
163
|
+
elif area > 50 and img == (251, 251, 251):
|
164
|
+
|
165
|
+
img[:, :, :] == fill_color
|
166
|
+
|
167
|
+
elif area > 50 and img == (252, 252, 252):
|
168
|
+
|
169
|
+
img[:, :, :] == fill_color
|
170
|
+
|
171
|
+
elif area > 50 and img == (253, 253, 253):
|
172
|
+
|
173
|
+
img[:, :, :] == fill_color
|
174
|
+
|
175
|
+
elif area > 50 and img == (254, 254, 254):
|
176
|
+
|
177
|
+
img[:, :, :] == fill_color
|
178
|
+
|
179
|
+
elif area > 50 and img == (255, 255, 255):
|
180
|
+
|
181
|
+
img[:, :, :] == fill_color
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
fig, ax = plt.subplots(figsize=(6, 6))
|
186
|
+
|
187
|
+
draw_contours(ax, img, contours)
|
188
|
+
|
189
|
+
plt.show()
|
146
190
|
|
147
191
|
|
148
192
|
|