Skip to content

Commit

Permalink
replace block division with nested compo detection
Browse files Browse the repository at this point in the history
  • Loading branch information
MulongXie committed Jul 6, 2021
1 parent 074cae3 commit d35581e
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 68 deletions.
Binary file modified detect_compo/__pycache__/ip_region_proposal.cpython-35.pyc
Binary file not shown.
3 changes: 1 addition & 2 deletions detect_compo/ip_region_proposal.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ def nesting_inspection(org, grey, compos, ffl_block):
for i, compo in enumerate(compos):
if compo.height > 50:
replace = False
clip_org = compo.compo_clipping(org)
clip_grey = compo.compo_clipping(grey)
n_compos = blk.block_division(clip_grey, org, grad_thresh=ffl_block, show=False)
n_compos = det.nested_components_detection(clip_grey, org, grad_thresh=ffl_block, show=False)
Compo.cvt_compos_relative_pos(n_compos, compo.bbox.col_min, compo.bbox.row_min)

for n_compo in n_compos:
Expand Down
66 changes: 0 additions & 66 deletions detect_compo/lib_ip/Block.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,6 @@
C = Config()


def block_division(grey, org, grad_thresh,
show=False, write_path=None,
step_h=10, step_v=10,
line_thickness=C.THRESHOLD_LINE_THICKNESS,
min_rec_evenness=C.THRESHOLD_REC_MIN_EVENNESS,
max_dent_ratio=C.THRESHOLD_REC_MAX_DENT_RATIO,
min_block_height_ratio=C.THRESHOLD_BLOCK_MIN_HEIGHT):
'''
:param grey: grey-scale of original image
:return: corners: list of [(top_left, bottom_right)]
-> top_left: (column_min, row_min)
-> bottom_right: (column_max, row_max)
'''
blocks = []
mask = np.zeros((grey.shape[0]+2, grey.shape[1]+2), dtype=np.uint8)
broad = np.zeros((grey.shape[0], grey.shape[1], 3), dtype=np.uint8)
broad_all = broad.copy()

row, column = grey.shape[0], grey.shape[1]
for x in range(0, row, step_h):
for y in range(0, column, step_v):
if mask[x, y] == 0:
# region = flood_fill_bfs(grey, x, y, mask)

# flood fill algorithm to get background (layout block)
mask_copy = mask.copy()
ff = cv2.floodFill(grey, mask, (y, x), None, grad_thresh, grad_thresh, cv2.FLOODFILL_MASK_ONLY)
# ignore small regions
if ff[0] < 500: continue
mask_copy = mask - mask_copy
region = np.reshape(cv2.findNonZero(mask_copy[1:-1, 1:-1]), (-1, 2))
region = [(p[1], p[0]) for p in region]

block = Block(region, grey.shape)
# draw.draw_region(region, broad_all)
# if block.height < 40 and block.width < 40:
# continue
if block.height < 30:
continue

# print(block.area / (row * column))
if block.area / (row * column) > 0.9:
continue
elif block.area / (row * column) > 0.7:
block.redundant = True

# get the boundary of this region
# ignore lines
if block.compo_is_line(line_thickness):
continue
# ignore non-rectangle as blocks must be rectangular
if not block.compo_is_rectangle(min_rec_evenness, max_dent_ratio):
continue
# if block.height/row < min_block_height_ratio:
# continue
blocks.append(block)
# draw.draw_region(region, broad)
if show:
cv2.imshow('flood-fill all', broad_all)
cv2.imshow('block', broad)
cv2.waitKey()
if write_path is not None:
cv2.imwrite(write_path, broad)
return blocks


class Block(Component):
def __init__(self, region, image_shape):
super().__init__(region, image_shape)
Expand Down
1 change: 1 addition & 0 deletions detect_compo/lib_ip/Component.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(self, region, image_shape):

self.rect_ = None
self.line_ = None
self.redundant = False

def compo_update(self, id, org_shape):
self.id = id
Expand Down
Binary file modified detect_compo/lib_ip/__pycache__/Block.cpython-35.pyc
Binary file not shown.
Binary file modified detect_compo/lib_ip/__pycache__/Component.cpython-35.pyc
Binary file not shown.
Binary file modified detect_compo/lib_ip/__pycache__/ip_detection.cpython-35.pyc
Binary file not shown.
Binary file modified detect_compo/lib_ip/__pycache__/ip_draw.cpython-35.pyc
Binary file not shown.
65 changes: 65 additions & 0 deletions detect_compo/lib_ip/ip_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,68 @@ def component_detection(binary, min_obj_area,
return compos_rec, compos_nonrec
else:
return compos_all


def nested_components_detection(grey, org, grad_thresh,
show=False, write_path=None,
step_h=10, step_v=10,
line_thickness=C.THRESHOLD_LINE_THICKNESS,
min_rec_evenness=C.THRESHOLD_REC_MIN_EVENNESS,
max_dent_ratio=C.THRESHOLD_REC_MAX_DENT_RATIO):
'''
:param grey: grey-scale of original image
:return: corners: list of [(top_left, bottom_right)]
-> top_left: (column_min, row_min)
-> bottom_right: (column_max, row_max)
'''
compos = []
mask = np.zeros((grey.shape[0]+2, grey.shape[1]+2), dtype=np.uint8)
broad = np.zeros((grey.shape[0], grey.shape[1], 3), dtype=np.uint8)
broad_all = broad.copy()

row, column = grey.shape[0], grey.shape[1]
for x in range(0, row, step_h):
for y in range(0, column, step_v):
if mask[x, y] == 0:
# region = flood_fill_bfs(grey, x, y, mask)

# flood fill algorithm to get background (layout block)
mask_copy = mask.copy()
ff = cv2.floodFill(grey, mask, (y, x), None, grad_thresh, grad_thresh, cv2.FLOODFILL_MASK_ONLY)
# ignore small regions
if ff[0] < 500: continue
mask_copy = mask - mask_copy
region = np.reshape(cv2.findNonZero(mask_copy[1:-1, 1:-1]), (-1, 2))
region = [(p[1], p[0]) for p in region]

compo = Component(region, grey.shape)
# draw.draw_region(region, broad_all)
# if block.height < 40 and block.width < 40:
# continue
if compo.height < 30:
continue

# print(block.area / (row * column))
if compo.area / (row * column) > 0.9:
continue
elif compo.area / (row * column) > 0.7:
compo.redundant = True

# get the boundary of this region
# ignore lines
if compo.compo_is_line(line_thickness):
continue
# ignore non-rectangle as blocks must be rectangular
if not compo.compo_is_rectangle(min_rec_evenness, max_dent_ratio):
continue
# if block.height/row < min_block_height_ratio:
# continue
compos.append(compo)
# draw.draw_region(region, broad)
if show:
cv2.imshow('flood-fill all', broad_all)
cv2.imshow('block', broad)
cv2.waitKey()
if write_path is not None:
cv2.imwrite(write_path, broad)
return compos
2 changes: 2 additions & 0 deletions detect_compo/lib_ip/ip_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def draw_bounding_box(org, components, color=(0, 255, 0), line=2,
cv2.imshow(name, board)
if wait_key is not None:
cv2.waitKey(wait_key)
if wait_key == 0:
cv2.destroyAllWindows()
if write_path is not None:
# board = cv2.resize(board, (1080, 1920))
# board = board[100:-110]
Expand Down

0 comments on commit d35581e

Please sign in to comment.