Siromanec commited on
Commit
eee8277
1 Parent(s): 32ea5b8

line stuff, now full hough, liberal edge selection

Browse files
Files changed (1) hide show
  1. handcrafted_solution.py +39 -16
handcrafted_solution.py CHANGED
@@ -116,39 +116,61 @@ def get_vertices_and_edges_from_segmentation(gest_seg_np, edge_th=50.0):
116
  cv2.circle(vertex_mask, np.round(i).astype(np.uint32), 15, (255,), 30, -1)
117
  vertex_mask = np.bitwise_not(vertex_mask)
118
 
119
- for edge_class in ['eave', 'ridge', 'rake', 'valley', 'flashing']:
120
- if (len(apex_pts) < 2):
 
 
 
 
 
 
 
 
121
  break
122
  edge_color = np.array(gestalt_color_mapping[edge_class])
123
 
124
  mask = cv2.inRange(gest_seg_np,
125
- edge_color - color_range,
126
- edge_color + color_range)
127
- mask = cv2.bitwise_and(mask, vertex_mask)
128
 
129
  mask = cv2.morphologyEx(mask,
130
- cv2.MORPH_DILATE, np.ones((11, 11)), iterations=2)
131
- if edge_class == "ridge":
132
- mask = cv2.morphologyEx(mask,
133
- cv2.MORPH_DILATE, np.ones((11, 11)), iterations=1)
 
134
 
135
  if np.any(mask):
136
 
137
  rho = 1 # distance resolution in pixels of the Hough grid
138
  theta = np.pi / 180 # angular resolution in radians of the Hough grid
139
- threshold = 15 # minimum number of votes (intersections in Hough grid cell)
140
- min_line_length = 40 # minimum number of pixels making up a line
141
  max_line_gap = 40 # maximum gap in pixels between connectable line segments
142
 
143
  # Run Hough on edge detected image
144
  # Output "lines" is an array containing endpoints of detected line segments
 
145
  lines = cv2.HoughLinesP(mask, rho, theta, threshold, np.array([]),
146
  min_line_length, max_line_gap)
147
 
148
  edges = []
149
 
150
  for line in lines if lines is not None else []:
151
- for x1, y1, x2, y2 in line:
 
 
 
 
 
 
 
 
 
 
 
 
152
  edges.append((x1, y1, x2, y2))
153
 
154
  edges = np.array(edges)
@@ -158,15 +180,16 @@ def get_vertices_and_edges_from_segmentation(gest_seg_np, edge_th=50.0):
158
  begin_distances = cdist(apex_pts, edges[:, :2], metric="sqeuclidean")
159
  end_distances = cdist(apex_pts, edges[:, 2:], metric="sqeuclidean")
160
 
161
- begin_closest_points = np.argmin(begin_distances,
162
- axis=0) # index of the closest point for each edge end point
163
  end_closest_points = np.argmin(end_distances, axis=0)
164
 
165
  begin_closest_point_distances = begin_distances[begin_closest_points, np.arange(len(begin_closest_points))]
166
  end_closest_point_distances = end_distances[end_closest_points, np.arange(len(end_closest_points))]
167
 
168
- begin_in_range_mask = begin_closest_point_distances <= edge_th
169
- end_in_range_mask = end_closest_point_distances <= edge_th
 
 
170
 
171
  # where both ends are in range
172
  in_range_connected_mask = np.logical_and(begin_in_range_mask, end_in_range_mask)
 
116
  cv2.circle(vertex_mask, np.round(i).astype(np.uint32), 15, (255,), 30, -1)
117
  vertex_mask = np.bitwise_not(vertex_mask)
118
 
119
+ scale = 4
120
+ vertex_size = np.zeros(apex_pts.shape[0])
121
+ for i, coords in enumerate(apex_pts):
122
+ coords = np.round(coords).astype(np.uint32)
123
+ radius = 25#np.clip(int(max_depth//2 + depth_np[coords[1], coords[0]]), 10, 30)#int(np.clip(max_depth - depth_np[coords[1], coords[0]], 10, 20))
124
+ vertex_size[i] = (scale*radius) ** 2 # because we are using squared distances
125
+ cv2.circle(vertex_mask, coords, radius - 5, (255, ), 2 * (radius - 5), -1)
126
+ # vertex_size *= scale
127
+ for edge_class in ['eave', 'ridge', 'rake', 'valley', 'flashing', 'step_flashing']:
128
+ if len(apex_pts) < 2:
129
  break
130
  edge_color = np.array(gestalt_color_mapping[edge_class])
131
 
132
  mask = cv2.inRange(gest_seg_np,
133
+ edge_color-color_range,
134
+ edge_color+color_range)
135
+ # mask = cv2.bitwise_and(mask, vertex_mask)
136
 
137
  mask = cv2.morphologyEx(mask,
138
+ cv2.MORPH_DILATE, np.ones((3, 3)), iterations=1)
139
+ # if edge_class == "ridge":
140
+ # mask = cv2.morphologyEx(mask,
141
+ # cv2.MORPH_DILATE, np.ones((11, 11)), iterations=1)
142
+
143
 
144
  if np.any(mask):
145
 
146
  rho = 1 # distance resolution in pixels of the Hough grid
147
  theta = np.pi / 180 # angular resolution in radians of the Hough grid
148
+ threshold = 20 # minimum number of votes (intersections in Hough grid cell)
149
+ min_line_length = 60 # minimum number of pixels making up a line
150
  max_line_gap = 40 # maximum gap in pixels between connectable line segments
151
 
152
  # Run Hough on edge detected image
153
  # Output "lines" is an array containing endpoints of detected line segments
154
+ cv2.GaussianBlur(mask, (11,11), 0, mask)
155
  lines = cv2.HoughLinesP(mask, rho, theta, threshold, np.array([]),
156
  min_line_length, max_line_gap)
157
 
158
  edges = []
159
 
160
  for line in lines if lines is not None else []:
161
+ for x1,y1,x2,y2 in line:
162
+ extend = 40
163
+ if x1 < x2:
164
+ x1, y1, x2, y2 = x2, y2, x1, y1
165
+ # cv2.line(mask_copy,(x1,y1),(x2,y2),(255,),10, cv2.LINE_AA)
166
+ direction = (np.array([x2 - x1, y2 - y1]))
167
+ direction = extend * direction / np.linalg.norm(direction)
168
+
169
+ # norm = extend * np.linalg.norm(np.array([y2 - y1, x1 - x2]))
170
+ x1,y1 = (-direction + (x1, y1)).astype(np.int32)
171
+ x2,y2 = (+ direction + (x2, y2)).astype(np.int32)
172
+
173
+
174
  edges.append((x1, y1, x2, y2))
175
 
176
  edges = np.array(edges)
 
180
  begin_distances = cdist(apex_pts, edges[:, :2], metric="sqeuclidean")
181
  end_distances = cdist(apex_pts, edges[:, 2:], metric="sqeuclidean")
182
 
183
+ begin_closest_points = np.argmin(begin_distances, axis=0) # index of the closest point for each edge end point
 
184
  end_closest_points = np.argmin(end_distances, axis=0)
185
 
186
  begin_closest_point_distances = begin_distances[begin_closest_points, np.arange(len(begin_closest_points))]
187
  end_closest_point_distances = end_distances[end_closest_points, np.arange(len(end_closest_points))]
188
 
189
+
190
+
191
+ begin_in_range_mask = begin_closest_point_distances < vertex_size[begin_closest_points]
192
+ end_in_range_mask = end_closest_point_distances < vertex_size[end_closest_points]
193
 
194
  # where both ends are in range
195
  in_range_connected_mask = np.logical_and(begin_in_range_mask, end_in_range_mask)