Skip to content

Commit 28c9a1d

Browse files
committed
Adding make mask image to bezier_cyl
Almost done doing mask fitting, just working on intersection over union
1 parent 832d700 commit 28c9a1d

File tree

2 files changed

+159
-320
lines changed

2 files changed

+159
-320
lines changed

Image_based/bezier_cyl_2d.py

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -310,16 +310,8 @@ def draw_rect_filled(im, rect, col=(50, 255, 255)):
310310
@param rect - the rect as a 4x2 np array
311311
@param col - the color to use
312312
"""
313-
xs = [p[0] for p in rect]
314-
ys = [p[1] for p in rect]
315-
diff_x = max(xs) - min(xs)
316-
diff_y = max(ys) - min(ys)
317-
for s in np.linspace(0.0, 1.0, int(np.ceil(diff_x))):
318-
for t in np.linspace(0, 1, int(np.ceil(diff_y))):
319-
xy = (1-s) * (1-t) * rect[0] + (1-s) * t * rect[1] + s * t * rect[2] + s * (1-t) * rect[3]
320-
xy[0], xy[1] = xy[1], xy[0]
321-
if 0 < int(xy[0]) < im.shape[0] and 0 < int(xy[1]) < im.shape[1]:
322-
im[int(xy[0]), int(xy[1])] = col
313+
points = np.int32(rect)
314+
cv2.fillPoly(im, pts=[points], color=col)
323315

324316
def draw_edge_rects(self, im, step_size=40, perc_width=0.3):
325317
""" Draw the edge rectangles
@@ -388,6 +380,38 @@ def draw_interior_rects_filled(self, im, b_solid=True, col_solid=(255, 255, 255)
388380
col = (128 + i * col_incr, 100 + (i % 2) * 100, 128 + i * col_incr)
389381
self.draw_rect_filled(im, r, col=col)
390382

383+
def draw_boundary_rects_filled(self, im, b_solid=True, col_solid=(255, 255, 255), step_size=40, perc_width=0.5):
384+
""" Draw the edge rectangles filled
385+
@param im - the image
386+
@param b_solid - use a solid color or alternate in order to see rects and order
387+
@param col_solid - the solid color to use.
388+
@param step_size how many pixels to move along the boundary
389+
@param perc_width How much of the radius to move in/out of the edge
390+
"""
391+
rects, _ = self.boundary_rects(step_size, perc_width)
392+
col_incr = 128 // len(rects)
393+
for i, r in enumerate(rects):
394+
if b_solid:
395+
col = col_solid
396+
else:
397+
col = (128 + i * col_incr, 100 + (i % 4) * 50, 128 + i * col_incr)
398+
self.draw_rect_filled(im, r, col=col)
399+
400+
def make_mask_image(self, im_mask, step_size=20, perc_fuzzy=0.2):
401+
""" Create a mask that is white in the middle, grey along the boundaries
402+
@param im_mask - the image
403+
@param step_size how many pixels to move along the boundary
404+
@param perc_fuzzy How much of the boundary to make fuzzy
405+
"""
406+
self.draw_interior_rects_filled(im_mask, b_solid=True,
407+
col_solid=(255, 255, 255),
408+
step_size=step_size,
409+
perc_width=1.0)
410+
self.draw_boundary_rects_filled(im_mask, b_solid=True,
411+
col_solid=(128, 128, 128),
412+
step_size=step_size,
413+
perc_width=perc_fuzzy)
414+
391415
def write_json(self, fname):
392416
"""Convert to array and write out
393417
@param fname file name to write to"""
@@ -436,7 +460,7 @@ def read_json(fname, bezier_crv=None):
436460
assert(not bezier_crv_vert.is_wire())
437461

438462
import matplotlib.pyplot as plt
439-
fig, axs = plt.subplots(2, 2)
463+
fig, axs = plt.subplots(3, 2)
440464
perc_width_interior = 0.5
441465
perc_width_edge = 0.2
442466
for i_row, crv in enumerate([bezier_crv_horiz, bezier_crv_vert]):
@@ -455,10 +479,15 @@ def read_json(fname, bezier_crv=None):
455479
axs[1, i_row].imshow(im_debug)
456480
axs[1, i_row].set_title(crv.orientation + f" filled {perc_width_interior}")
457481

458-
fname = "./Data/test_crv.json"
459-
crv.write_json(fname)
482+
im_debug = np.zeros((480, 640, 3), np.uint8)
483+
crv.make_mask_image(im_debug, perc_fuzzy=0.25)
484+
axs[2, i_row].imshow(im_debug)
485+
axs[2, i_row].set_title(crv.orientation + f" mask 0.25")
486+
487+
fname_test = "./Data/test_crv.json"
488+
crv.write_json(fname_test)
460489

461-
read_back_in_crv = BezierCyl2D.read_json(fname)
490+
read_back_in_crv = BezierCyl2D.read_json(fname_test)
462491
plt.tight_layout()
463492

464493
print("Done")

0 commit comments

Comments
 (0)