質問編集履歴
1
コードと3つのnpyファイル、ipynbファイルを添付してます。よろしくお願いします。
test
CHANGED
File without changes
|
test
CHANGED
@@ -19,3 +19,195 @@
|
|
19
19
|
この2つだと実際に見てみると最適でないように感じました。
|
20
20
|
|
21
21
|
縦向きになったとどのように人が認知しているのか知りたいです。最適な角度を算出するアイデアをご教授願います。
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
[添付フォルダ(tooth_array.zip内に3つnpyファイル、ipynbファイル)](https://drive.google.com/file/d/1cdY877E7WrsXs9kR9c3p8qI-NqXD7B5X/view?usp=sharing)
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
```Python
|
32
|
+
|
33
|
+
#coding:utf-8
|
34
|
+
|
35
|
+
import numpy as np
|
36
|
+
|
37
|
+
import matplotlib.pyplot as plt
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
# アフィン変換で画像配列の回転
|
42
|
+
|
43
|
+
def rotate_affine(src, theta):
|
44
|
+
|
45
|
+
# 元画像のサイズを取得
|
46
|
+
|
47
|
+
h, w = src.shape[0], src.shape[1]
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
# 出力画像用の配列生成(要素は全て0)
|
52
|
+
|
53
|
+
dst = np.zeros((h,w))
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
# degreeかrradianに変換
|
58
|
+
|
59
|
+
rd = np.radians(theta)
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
# アフィン変換
|
64
|
+
|
65
|
+
for y in range(0, h):
|
66
|
+
|
67
|
+
for x in range(0, w):
|
68
|
+
|
69
|
+
xi = int((x - int(w/2))*np.cos(rd) - (y - int(h/2))*np.sin(rd) + int(w/2))
|
70
|
+
|
71
|
+
yi = int((x - int(w/2))*np.sin(rd) + (y - int(h/2))*np.cos(rd) + int(h/2))
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
# 変換後の座標が範囲外でなければ出力画像配列に画素値を代入
|
76
|
+
|
77
|
+
if yi < h - 1 and xi < w - 1 and yi > 0 and xi > 0:
|
78
|
+
|
79
|
+
dst[y][x] = src[yi][xi]
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
return dst
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
def pattern1(src):
|
88
|
+
|
89
|
+
sum_cnt = 1000000
|
90
|
+
|
91
|
+
best_theta = 0
|
92
|
+
|
93
|
+
for theta in range(-90,90+1):
|
94
|
+
|
95
|
+
dst = rotate_affine(src,theta)
|
96
|
+
|
97
|
+
vertical_info = np.count_nonzero(dst ==255, axis=0)
|
98
|
+
|
99
|
+
side_info = np.count_nonzero(dst ==255, axis=1)
|
100
|
+
|
101
|
+
min_flag = False
|
102
|
+
|
103
|
+
for i,num in enumerate(vertical_info):
|
104
|
+
|
105
|
+
if num != 0 and min_flag == False:
|
106
|
+
|
107
|
+
min_flag = True
|
108
|
+
|
109
|
+
min_x = i
|
110
|
+
|
111
|
+
if num == 0 and min_flag == True:
|
112
|
+
|
113
|
+
max_x = i-1
|
114
|
+
|
115
|
+
min_flag = False
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
for i,num in enumerate(side_info):
|
120
|
+
|
121
|
+
if num != 0 and min_flag == False:
|
122
|
+
|
123
|
+
min_flag = True
|
124
|
+
|
125
|
+
min_y = i
|
126
|
+
|
127
|
+
if num == 0 and min_flag == True:
|
128
|
+
|
129
|
+
max_y = i-1
|
130
|
+
|
131
|
+
min_flag = False
|
132
|
+
|
133
|
+
tooth_co_info = [min_y,min_x,max_y,max_x]
|
134
|
+
|
135
|
+
ex_dst = dst[tooth_co_info[0]:tooth_co_info[2]+1,tooth_co_info[1]:tooth_co_info[3]+1]
|
136
|
+
|
137
|
+
print("theta,sum:{},{}".format(theta,np.count_nonzero(ex_dst ==0, axis=0).sum()))
|
138
|
+
|
139
|
+
if np.count_nonzero(ex_dst ==0, axis=0).sum() < sum_cnt:
|
140
|
+
|
141
|
+
sum_cnt = np.count_nonzero(ex_dst ==0, axis=0).sum()
|
142
|
+
|
143
|
+
best_theta = theta
|
144
|
+
|
145
|
+
print("best")
|
146
|
+
|
147
|
+
print(best_theta,sum_cnt)
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
def pattern2(src):
|
154
|
+
|
155
|
+
max_cnt = 0
|
156
|
+
|
157
|
+
best_theta = 0
|
158
|
+
|
159
|
+
for theta in range(-90,90+1):
|
160
|
+
|
161
|
+
dst = rotate_affine(src,theta)
|
162
|
+
|
163
|
+
print("theta,count:{},{}".format(theta,np.count_nonzero(dst ==255, axis=0).max()))
|
164
|
+
|
165
|
+
if np.count_nonzero(dst ==255, axis=0).max() > max_cnt:
|
166
|
+
|
167
|
+
max_cnt = np.count_nonzero(dst ==255, axis=0).max()
|
168
|
+
|
169
|
+
best_theta = theta
|
170
|
+
|
171
|
+
print("best")
|
172
|
+
|
173
|
+
print("theta,count:{},{}".format(theta,max_cnt))
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
if __name__ == '__main__':
|
178
|
+
|
179
|
+
mask = np.load('mask.npy')
|
180
|
+
|
181
|
+
bi_mask = np.where(mask==True,255,0)
|
182
|
+
|
183
|
+
h, w = bi_mask.shape[0], bi_mask.shape[1]
|
184
|
+
|
185
|
+
src = np.zeros((int((w**2 + h**2)**(1/2)), int((w**2 + h**2)**(1/2))))
|
186
|
+
|
187
|
+
dx,dy = int((w**2 + h**2)**(1/2)/2) - int(w/2), int((w**2 + h**2)**(1/2)/2) - int(h/2)
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
for y in range(0, h):#平行移動
|
192
|
+
|
193
|
+
for x in range(0, w):
|
194
|
+
|
195
|
+
src[y+dy][x+dx] = bi_mask[y][x]
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
theta = 0
|
200
|
+
|
201
|
+
dst = rotate_affine(src,theta)
|
202
|
+
|
203
|
+
plt.imshow(dst)
|
204
|
+
|
205
|
+
plt.show()
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
pattern1(src) #切り替え必要
|
210
|
+
|
211
|
+
# pattern2(src)
|
212
|
+
|
213
|
+
```
|