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

質問編集履歴

1

コードと3つのnpyファイル、ipynbファイルを添付してます。よろしくお願いします。

2020/11/26 11:57

投稿

amayan
amayan

スコア9

title CHANGED
File without changes
body CHANGED
@@ -8,4 +8,100 @@
8
8
  2. 列ごとに歯部分のピクセルを数え最多のものを最適とする。
9
9
 
10
10
  この2つだと実際に見てみると最適でないように感じました。
11
- 縦向きになったとどのように人が認知しているのか知りたいです。最適な角度を算出するアイデアをご教授願います。
11
+ 縦向きになったとどのように人が認知しているのか知りたいです。最適な角度を算出するアイデアをご教授願います。
12
+
13
+
14
+ [添付フォルダ(tooth_array.zip内に3つnpyファイル、ipynbファイル)](https://drive.google.com/file/d/1cdY877E7WrsXs9kR9c3p8qI-NqXD7B5X/view?usp=sharing)
15
+
16
+ ```Python
17
+ #coding:utf-8
18
+ import numpy as np
19
+ import matplotlib.pyplot as plt
20
+
21
+ # アフィン変換で画像配列の回転
22
+ def rotate_affine(src, theta):
23
+ # 元画像のサイズを取得
24
+ h, w = src.shape[0], src.shape[1]
25
+
26
+ # 出力画像用の配列生成(要素は全て0)
27
+ dst = np.zeros((h,w))
28
+
29
+ # degreeかrradianに変換
30
+ rd = np.radians(theta)
31
+
32
+ # アフィン変換
33
+ for y in range(0, h):
34
+ for x in range(0, w):
35
+ xi = int((x - int(w/2))*np.cos(rd) - (y - int(h/2))*np.sin(rd) + int(w/2))
36
+ yi = int((x - int(w/2))*np.sin(rd) + (y - int(h/2))*np.cos(rd) + int(h/2))
37
+
38
+ # 変換後の座標が範囲外でなければ出力画像配列に画素値を代入
39
+ if yi < h - 1 and xi < w - 1 and yi > 0 and xi > 0:
40
+ dst[y][x] = src[yi][xi]
41
+
42
+ return dst
43
+
44
+ def pattern1(src):
45
+ sum_cnt = 1000000
46
+ best_theta = 0
47
+ for theta in range(-90,90+1):
48
+ dst = rotate_affine(src,theta)
49
+ vertical_info = np.count_nonzero(dst ==255, axis=0)
50
+ side_info = np.count_nonzero(dst ==255, axis=1)
51
+ min_flag = False
52
+ for i,num in enumerate(vertical_info):
53
+ if num != 0 and min_flag == False:
54
+ min_flag = True
55
+ min_x = i
56
+ if num == 0 and min_flag == True:
57
+ max_x = i-1
58
+ min_flag = False
59
+
60
+ for i,num in enumerate(side_info):
61
+ if num != 0 and min_flag == False:
62
+ min_flag = True
63
+ min_y = i
64
+ if num == 0 and min_flag == True:
65
+ max_y = i-1
66
+ min_flag = False
67
+ tooth_co_info = [min_y,min_x,max_y,max_x]
68
+ ex_dst = dst[tooth_co_info[0]:tooth_co_info[2]+1,tooth_co_info[1]:tooth_co_info[3]+1]
69
+ print("theta,sum:{},{}".format(theta,np.count_nonzero(ex_dst ==0, axis=0).sum()))
70
+ if np.count_nonzero(ex_dst ==0, axis=0).sum() < sum_cnt:
71
+ sum_cnt = np.count_nonzero(ex_dst ==0, axis=0).sum()
72
+ best_theta = theta
73
+ print("best")
74
+ print(best_theta,sum_cnt)
75
+
76
+
77
+ def pattern2(src):
78
+ max_cnt = 0
79
+ best_theta = 0
80
+ for theta in range(-90,90+1):
81
+ dst = rotate_affine(src,theta)
82
+ print("theta,count:{},{}".format(theta,np.count_nonzero(dst ==255, axis=0).max()))
83
+ if np.count_nonzero(dst ==255, axis=0).max() > max_cnt:
84
+ max_cnt = np.count_nonzero(dst ==255, axis=0).max()
85
+ best_theta = theta
86
+ print("best")
87
+ print("theta,count:{},{}".format(theta,max_cnt))
88
+
89
+ if __name__ == '__main__':
90
+ mask = np.load('mask.npy')
91
+ bi_mask = np.where(mask==True,255,0)
92
+ h, w = bi_mask.shape[0], bi_mask.shape[1]
93
+ src = np.zeros((int((w**2 + h**2)**(1/2)), int((w**2 + h**2)**(1/2))))
94
+ dx,dy = int((w**2 + h**2)**(1/2)/2) - int(w/2), int((w**2 + h**2)**(1/2)/2) - int(h/2)
95
+
96
+ for y in range(0, h):#平行移動
97
+ for x in range(0, w):
98
+ src[y+dy][x+dx] = bi_mask[y][x]
99
+
100
+ theta = 0
101
+ dst = rotate_affine(src,theta)
102
+ plt.imshow(dst)
103
+ plt.show()
104
+
105
+ pattern1(src) #切り替え必要
106
+ # pattern2(src)
107
+ ```