add/remove apex
Browse files- handcrafted_solution.py +34 -2
handcrafted_solution.py
CHANGED
|
@@ -6,6 +6,7 @@ import numpy as np
|
|
| 6 |
from collections import defaultdict
|
| 7 |
import cv2
|
| 8 |
from typing import Tuple, List
|
|
|
|
| 9 |
from scipy.spatial.distance import cdist
|
| 10 |
|
| 11 |
from hoho.read_write_colmap import read_cameras_binary, read_images_binary, read_points3D_binary
|
|
@@ -125,6 +126,7 @@ def get_vertices_and_edges_from_two_segmentations(ade_seg_np, gest_seg_np, edge_
|
|
| 125 |
'''Get the vertices and edges from the gestalt segmentation mask of the house'''
|
| 126 |
vertices = []
|
| 127 |
connections = []
|
|
|
|
| 128 |
# combined map from ade
|
| 129 |
print(gest_seg_np.shape, ade_seg_np.shape)
|
| 130 |
ade_color0 = np.array([0,0,0])
|
|
@@ -138,10 +140,15 @@ def get_vertices_and_edges_from_two_segmentations(ade_seg_np, gest_seg_np, edge_
|
|
| 138 |
ade_mask = cv2.bitwise_or(ade_mask3, ade_mask2)
|
| 139 |
ade_mask = cv2.bitwise_or(ade_mask1, ade_mask)
|
| 140 |
print(ade_mask.any())
|
|
|
|
|
|
|
|
|
|
| 141 |
# Apex
|
| 142 |
apex_color = np.array(gestalt_color_mapping['apex'])
|
| 143 |
-
|
| 144 |
-
apex_mask = cv2.
|
|
|
|
|
|
|
| 145 |
if apex_mask.sum() > 0:
|
| 146 |
output = cv2.connectedComponentsWithStats(apex_mask, 8, cv2.CV_32S)
|
| 147 |
(numLabels, labels, stats, centroids) = output
|
|
@@ -150,7 +157,32 @@ def get_vertices_and_edges_from_two_segmentations(ade_seg_np, gest_seg_np, edge_
|
|
| 150 |
for i in range(numLabels-1):
|
| 151 |
vert = {"xy": centroids[i], "type": "apex"}
|
| 152 |
vertices.append(vert)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
eave_end_color = np.array(gestalt_color_mapping['eave_end_point'])
|
| 155 |
eave_end_mask = cv2.inRange(gest_seg_np, eave_end_color-0.5, eave_end_color+0.5)
|
| 156 |
eave_end_mask = cv2.bitwise_and(eave_end_mask, ade_mask)
|
|
|
|
| 6 |
from collections import defaultdict
|
| 7 |
import cv2
|
| 8 |
from typing import Tuple, List
|
| 9 |
+
from scipy.sparse import random
|
| 10 |
from scipy.spatial.distance import cdist
|
| 11 |
|
| 12 |
from hoho.read_write_colmap import read_cameras_binary, read_images_binary, read_points3D_binary
|
|
|
|
| 126 |
'''Get the vertices and edges from the gestalt segmentation mask of the house'''
|
| 127 |
vertices = []
|
| 128 |
connections = []
|
| 129 |
+
print(f'gest_seg_np[0,0]= {gest_seg_np[0,0]}')
|
| 130 |
# combined map from ade
|
| 131 |
print(gest_seg_np.shape, ade_seg_np.shape)
|
| 132 |
ade_color0 = np.array([0,0,0])
|
|
|
|
| 140 |
ade_mask = cv2.bitwise_or(ade_mask3, ade_mask2)
|
| 141 |
ade_mask = cv2.bitwise_or(ade_mask1, ade_mask)
|
| 142 |
print(ade_mask.any())
|
| 143 |
+
apex_map = np.zeros(ade_seg_np.shape)
|
| 144 |
+
apex_map_on_ade = ade_seg_np
|
| 145 |
+
apex_map_on_gest = gest_seg_np
|
| 146 |
# Apex
|
| 147 |
apex_color = np.array(gestalt_color_mapping['apex'])
|
| 148 |
+
print(f'apex_color= {apex_color}')
|
| 149 |
+
apex_mask = cv2.inRange(gest_seg_np, apex_color-0.5, apex_color+0.5)
|
| 150 |
+
# apex_mask = cv2.inRange(gest_seg_np, apex_color-10, apex_color+10) # include more pts
|
| 151 |
+
# apex_mask = cv2.bitwise_and(apex_mask, ade_mask) # remove pts
|
| 152 |
if apex_mask.sum() > 0:
|
| 153 |
output = cv2.connectedComponentsWithStats(apex_mask, 8, cv2.CV_32S)
|
| 154 |
(numLabels, labels, stats, centroids) = output
|
|
|
|
| 157 |
for i in range(numLabels-1):
|
| 158 |
vert = {"xy": centroids[i], "type": "apex"}
|
| 159 |
vertices.append(vert)
|
| 160 |
+
|
| 161 |
+
print(f'centroids[i]={centroids[i]}')
|
| 162 |
+
uu = int(centroids[i][1])
|
| 163 |
+
vv = int(centroids[i][0])
|
| 164 |
+
# plot a cross
|
| 165 |
+
apex_map_on_ade[uu, vv] = (255,255,255)
|
| 166 |
+
shift=[(1,0),(-1,0),(0,1),(0,-1), (2,0),(-2,0),(0,2),(0,-2), (3,0),(-3,0),(0,3),(0,-3)]
|
| 167 |
+
h,w,_ = apex_map_on_ade.shape
|
| 168 |
+
for ss in shift:
|
| 169 |
+
if uu+ss[0] >= 0 and uu+ss[0] < h and vv+ss[1] >= 0 and vv+ss[1] < w:
|
| 170 |
+
apex_map[uu+ss[0], vv+ss[1]] = (255,255,255)
|
| 171 |
+
apex_map_on_ade[uu+ss[0], vv+ss[1]] = (255,255,255)
|
| 172 |
+
apex_map_on_gest[uu+ss[0], vv+ss[1]] = (255,255,255)
|
| 173 |
|
| 174 |
+
# imsave apex
|
| 175 |
+
import random
|
| 176 |
+
rid = random.random()
|
| 177 |
+
filename_apex_ade = f'apex_map_on_ade_{rid}.jpg'
|
| 178 |
+
cv2.imwrite(filename_apex_ade, apex_map_on_ade)
|
| 179 |
+
filename_apex_gest = f'apex_map_on_gest_{rid}.jpg'
|
| 180 |
+
cv2.imwrite(filename_apex_gest, apex_map_on_gest)
|
| 181 |
+
filename_apex_map = f'apex_map_{rid}.jpg'
|
| 182 |
+
cv2.imwrite(filename_apex_map, apex_map)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
|
| 186 |
eave_end_color = np.array(gestalt_color_mapping['eave_end_point'])
|
| 187 |
eave_end_mask = cv2.inRange(gest_seg_np, eave_end_color-0.5, eave_end_color+0.5)
|
| 188 |
eave_end_mask = cv2.bitwise_and(eave_end_mask, ade_mask)
|