Skip to content

Commit 227c452

Browse files
committed
fix: max instead of mean structure size for line kernels
1 parent 2f2fe8e commit 227c452

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

decimer_segmentation/complete_structure.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def get_neighbour_pixels(
368368

369369
def detect_horizontal_and_vertical_lines(
370370
image: np.ndarray,
371-
average_depiction_size: Tuple[int, int]
371+
max_depiction_size: Tuple[int, int]
372372
) -> np.ndarray:
373373
"""
374374
This function takes an image and returns a binary mask that labels the pixels that
@@ -378,15 +378,16 @@ def detect_horizontal_and_vertical_lines(
378378
Args:
379379
image (np.ndarray): binarised image (np.array; type bool) as it is returned by
380380
binary_erosion() in complete_structure_mask()
381+
max_depiction_size (Tuple[int, int]): height, width; used as thresholds
381382
382383
Returns:
383384
np.ndarray: Exclusion mask that contains indices of pixels that are part of
384385
horizontal or vertical lines
385386
"""
386387
binarised_im = ~image * 255
387388
binarised_im = binarised_im.astype("uint8")
388-
389-
structure_height, structure_width = average_depiction_size
389+
390+
structure_height, structure_width = max_depiction_size
390391

391392
horizontal_kernel = cv2.getStructuringElement(
392393
cv2.MORPH_RECT, (structure_width, 1)
@@ -477,23 +478,23 @@ def expansion_coordination(
477478
def complete_structure_mask(
478479
image_array: np.array,
479480
mask_array: np.array,
480-
average_depiction_size: Tuple[int, int],
481+
max_depiction_size: Tuple[int, int],
481482
debug=False
482483
) -> np.array:
483484
"""
484485
This funtion takes an image (np.array) and an array containing the masks (shape:
485486
x,y,n where n is the amount of masks and x and y are the pixel coordinates).
486-
Additionally, it takes the average depiction size of the structures in the image
487+
Additionally, it takes the maximal depiction size of the structures in the image
487488
which is used to define the kernel size for the vertical and horizontal line
488489
detection for the exclusion masks. The exclusion mask is used to exclude pixels
489-
from the mask expansion to avoid including whole tables.
490+
from the mask expansion to avoid including whole tables.
490491
It detects objects on the contours of the mask and expands it until it frames the
491492
complete object in the image. It returns the expanded mask array
492493
493494
Args:
494495
image_array (np.array): input image
495496
mask_array (np.array): shape: y, x, n where n is the amount of masks
496-
average_depiction_size (Tuple[int, int]): height, width
497+
max_depiction_size (Tuple[int, int]): height, width
497498
debug (bool, optional): More verbose if True. Defaults to False.
498499
499500
Returns:
@@ -519,7 +520,7 @@ def complete_structure_mask(
519520
[mask_array[:, :, index] for index in range(mask_array.shape[2])]
520521
)
521522
exclusion_mask = detect_horizontal_and_vertical_lines(blurred_image_array,
522-
average_depiction_size)
523+
max_depiction_size)
523524
# Run expansion the expansion
524525
image_repeat = itertools.repeat(blurred_image_array, mask_array.shape[2])
525526
exclusion_mask_repeat = itertools.repeat(exclusion_mask, mask_array.shape[2])

decimer_segmentation/decimer_segmentation.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ def segment_chemical_structures(
117117
return segments
118118

119119

120-
def determine_average_depiction_size(
120+
def determine_depiction_size_with_buffer(
121121
bboxes: List[Tuple[int, int, int, int]]
122122
) -> Tuple[int, int]:
123123
"""
124-
This function takes a list of bounding boxes and returns the average
124+
This function takes a list of bounding boxes and returns 1.2 * the maximal
125125
depiction size (height, width) of the depicted chemical structures.
126126
127127
Args:
@@ -138,9 +138,9 @@ def determine_average_depiction_size(
138138
width = bbox[3] - bbox[1]
139139
heights.append(height)
140140
widths.append(width)
141-
average_height = int(np.mean(heights))
142-
average_width = int(np.mean(widths))
143-
return int(average_height), int(average_width)
141+
height = int(1.2 * np.max(heights))
142+
width = int(1.2 * np.max(widths))
143+
return height, width
144144

145145

146146
def sort_segments_bboxes(
@@ -227,11 +227,11 @@ def get_expanded_masks(image: np.array) -> np.array:
227227
"""
228228
# Structure detection with MRCNN
229229
masks, bboxes, _ = get_mrcnn_results(image)
230-
size = determine_average_depiction_size(bboxes)
230+
size = determine_depiction_size_with_buffer(bboxes)
231231
# Mask expansion
232232
expanded_masks = complete_structure_mask(image_array=image,
233233
mask_array=masks,
234-
average_depiction_size=size,)
234+
max_depiction_size=size,)
235235
return expanded_masks
236236

237237

tests/test_decimer_segmentation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import numpy as np
22
from decimer_segmentation.decimer_segmentation import *
33

4-
def test_determine_average_depiction_size():
4+
def test_determine_depiction_size_with_buffer():
55
# Determine the average depiction size of the structures in a given list of structures
66
bboxes = [
77
[0, 0, 6, 6],
88
[0, 0, 8, 8],
99
[0, 0, 10, 10],
1010
]
11-
expected_result = (8, 8)
12-
actual_result = determine_average_depiction_size(bboxes)
11+
expected_result = (12, 12)
12+
actual_result = determine_depiction_size_with_buffer(bboxes)
1313
assert expected_result == actual_result

tests/test_mask_expansion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def test_detect_horizontal_and_vertical_lines():
153153
[[False] * 20] * 20
154154
)
155155
expected_result[9] = np.array([True] * 20)
156-
actual_result = detect_horizontal_and_vertical_lines(~test_image)
156+
actual_result = detect_horizontal_and_vertical_lines(~test_image, (2, 2))
157157
np.testing.assert_array_equal(expected_result, actual_result)
158158

159159

0 commit comments

Comments
 (0)