Siromanec commited on
Commit
bef6092
1 Parent(s): 2cbdf26

added cleaner vertex mask

Browse files
Files changed (1) hide show
  1. handcrafted_solution.py +15 -10
handcrafted_solution.py CHANGED
@@ -105,15 +105,20 @@ def get_vertices_and_edges_from_segmentation(gest_seg_np, edge_th=50.0):
105
 
106
  gest_seg_np = clean_image(gest_seg_np)
107
  apex_centroids, eave_end_point_centroids, apex_mask, eave_end_point_mask = get_vertices(gest_seg_np)
108
- apex_mask = cv2.morphologyEx(apex_mask,
109
- cv2.MORPH_DILATE, np.ones((11, 11)), iterations=4)
110
- eave_end_point_mask = cv2.morphologyEx(eave_end_point_mask,
111
- cv2.MORPH_DILATE, np.ones((5, 5)), iterations=4)
112
- vertex_mask = cv2.bitwise_not(cv2.bitwise_or(apex_mask, eave_end_point_mask))
 
 
113
 
114
  apex_pts = np.concatenate([apex_centroids, eave_end_point_centroids])
115
 
 
116
  for edge_class in ['eave', 'ridge', 'rake', 'valley', 'flashing']:
 
 
117
  edge_color = np.array(gestalt_color_mapping[edge_class])
118
 
119
  mask = cv2.inRange(gest_seg_np,
@@ -125,10 +130,10 @@ def get_vertices_and_edges_from_segmentation(gest_seg_np, edge_th=50.0):
125
 
126
  mask = cv2.morphologyEx(mask,
127
  cv2.MORPH_DILATE, np.ones((11, 11)), iterations=3)
128
- line_img = np.zeros_like(gest_seg_np)
129
  output = cv2.connectedComponentsWithStats(mask, 8, cv2.CV_32S)
130
  (numLabels, labels, stats, centroids) = output
131
- stats, centroids = stats[1:], centroids[1:]
132
  edges = []
133
  for i in range(1, numLabels):
134
  y, x = np.where(labels == i)
@@ -139,9 +144,9 @@ def get_vertices_and_edges_from_segmentation(gest_seg_np, edge_th=50.0):
139
  x_right = x[xright_idx]
140
  y_right = y[xright_idx]
141
  edges.append((x_left, y_left, x_right, y_right))
142
- cv2.line(line_img, (x_left, y_left), (x_right, y_right), (255, 255, 255), 2)
143
  edges = np.array(edges)
144
- if (len(apex_pts) < 2) or len(edges) < 1:
145
  continue
146
  pts_to_edges_dist = np.minimum(cdist(apex_pts, edges[:, :2]), cdist(apex_pts, edges[:, 2:]))
147
  connectivity_mask = pts_to_edges_dist <= edge_th
@@ -262,7 +267,7 @@ def predict(entry, visualize=False) -> Tuple[np.ndarray, List[int]]:
262
  gest_seg_np = np.array(gest_seg).astype(np.uint8)
263
  # Metric3D
264
  depth_np = np.array(depth) / 2.5 # 2.5 is the scale estimation coefficient
265
- vertices, connections = get_vertices_and_edges_from_segmentation(gest_seg_np, edge_th=90.)
266
  if (len(vertices) < 2) or (len(connections) < 1):
267
  print(f'Not enough vertices or connections in image {i}')
268
  vert_edge_per_image[i] = np.empty((0, 2)), [], np.empty((0, 3))
 
105
 
106
  gest_seg_np = clean_image(gest_seg_np)
107
  apex_centroids, eave_end_point_centroids, apex_mask, eave_end_point_mask = get_vertices(gest_seg_np)
108
+
109
+ vertex_mask = np.zeros_like(apex_mask)
110
+ for i in apex_centroids:
111
+ cv2.circle(vertex_mask, np.round(i).astype(np.uint32), 20, (255, ), 40, -1)
112
+ for i in eave_end_point_centroids:
113
+ cv2.circle(vertex_mask, np.round(i).astype(np.uint32), 15, (255, ), 30, -1)
114
+ vertex_mask = np.bitwise_not(vertex_mask)
115
 
116
  apex_pts = np.concatenate([apex_centroids, eave_end_point_centroids])
117
 
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,
 
130
 
131
  mask = cv2.morphologyEx(mask,
132
  cv2.MORPH_DILATE, np.ones((11, 11)), iterations=3)
133
+ # line_img = np.zeros_like(gest_seg_np)
134
  output = cv2.connectedComponentsWithStats(mask, 8, cv2.CV_32S)
135
  (numLabels, labels, stats, centroids) = output
136
+ # stats, centroids = stats[1:], centroids[1:]
137
  edges = []
138
  for i in range(1, numLabels):
139
  y, x = np.where(labels == i)
 
144
  x_right = x[xright_idx]
145
  y_right = y[xright_idx]
146
  edges.append((x_left, y_left, x_right, y_right))
147
+ # cv2.line(line_img, (x_left, y_left), (x_right, y_right), (255, 255, 255), 2)
148
  edges = np.array(edges)
149
+ if len(edges) < 1:
150
  continue
151
  pts_to_edges_dist = np.minimum(cdist(apex_pts, edges[:, :2]), cdist(apex_pts, edges[:, 2:]))
152
  connectivity_mask = pts_to_edges_dist <= edge_th
 
267
  gest_seg_np = np.array(gest_seg).astype(np.uint8)
268
  # Metric3D
269
  depth_np = np.array(depth) / 2.5 # 2.5 is the scale estimation coefficient
270
+ vertices, connections = get_vertices_and_edges_from_segmentation(gest_seg_np, edge_th=70.)
271
  if (len(vertices) < 2) or (len(connections) < 1):
272
  print(f'Not enough vertices or connections in image {i}')
273
  vert_edge_per_image[i] = np.empty((0, 2)), [], np.empty((0, 3))