質問編集履歴

1

ここまでにやっているコードを追記しました。

2021/10/10 01:24

投稿

totoro33
totoro33

スコア1

test CHANGED
File without changes
test CHANGED
@@ -46,15 +46,133 @@
46
46
 
47
47
  分割後の各点から垂線を引く方法と垂線と輪郭線との交点の座標を取得する方法がわからず試行錯誤しています。
48
48
 
49
+ ```ここに言語を入力 python
50
+
51
+ コード
52
+
53
+ #モジュールのインポート
54
+
55
+ import cv2
56
+
57
+ import matplotlib.pyplot as plt
58
+
59
+ %matplotlib inline
60
+
61
+ import numpy as np
62
+
63
+ import math
49
64
 
50
65
 
51
66
 
67
+ #最大輪郭を得る関数
68
+
69
+ def find_max_contour(img):
70
+
71
+ contours, _ = cv2.findContours(
72
+
73
+ img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE
74
+
75
+ )
76
+
77
+ max_contour = max(contours, key=lambda x:cv2.contourArea(x))
52
78
 
53
79
 
54
80
 
55
- ```ここに言語名を入力
81
+ return max_contour
56
82
 
83
+ # 画像の読み込み
84
+
85
+ img = cv2.imread("opening2.png", 0)
86
+
87
+ color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
88
+
89
+ lines = cv2.HoughLinesP(img, rho=1, theta=np.pi/360, threshold=80, minLineLength=80, maxLineGap=10)
90
+
91
+ # 複数の直線がある場合、y座標の和がもっとも大きいものを採用
92
+
93
+ # 中点(mid_x, mid_y)
94
+
95
+ y_sum_dict = {}
96
+
97
+ for i, pos in enumerate(lines.squeeze(1)):
98
+
99
+ y1, y2 = pos[1], pos[3]
100
+
101
+ y_sum_dict[i] = y1 + y2
102
+
103
+
104
+
105
+ index = 0
106
+
107
+ y_sum_max = 0
108
+
109
+ for i, y_sum in y_sum_dict.items():
110
+
111
+ if i == 0:
112
+
113
+ index = i
114
+
115
+ y_sum_max = y_sum
116
+
117
+ else:
118
+
119
+ if y_sum_max < y_sum:
120
+
121
+ y_sum_max =y_sum
122
+
123
+ index = i
124
+
125
+
126
+
127
+ pos = lines.squeeze(1)[index]
128
+
129
+ x1, y1, x2, y2 = pos[0], pos[1], pos[2], pos[3]
130
+
131
+ print(x1, y1, x2, y2)
132
+
133
+ mid_x, mid_y = int((x1+x2)/2), int((y1+y2)/2)
134
+
135
+ print(mid_x, mid_y)
136
+
137
+
138
+
139
+ # 全ての輪郭上の点に対してユークリッド距離を計算する方針
140
+
141
+ def get_distance(x1, y1, x2, y2):
142
+
143
+ d = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
144
+
57
- python
145
+ return d
146
+
147
+ # 中点から最大距離の点を見つける(api_x, api_y)
148
+
149
+ dist_list = []
150
+
151
+ for point in max_cnt.squeeze(1):
152
+
153
+ x, y = point[0], point[1]
154
+
155
+ dist = get_distance(mid_x, mid_y, x, y)
156
+
157
+ dist_list.append(dist)
158
+
159
+ max(dist_list)
160
+
161
+
162
+
163
+ max_dist_index = dist_list.index(max(dist_list))
164
+
165
+ apical = max_cnt.squeeze(1)[max_dist_index]
166
+
167
+
168
+
169
+ api_x, api_y = apical[0], apical[1]
170
+
171
+ # 20等分する
172
+
173
+ lins = np.linspace((api_x, api_y), (mid_x, mid_y), 21)
174
+
175
+ ```
58
176
 
59
177
 
60
178