@@ -366,7 +366,10 @@ def get_neighbour_pixels(
366
366
return neighbour_pixels
367
367
368
368
369
- def detect_horizontal_and_vertical_lines (image : np .ndarray ) -> np .ndarray :
369
+ def detect_horizontal_and_vertical_lines (
370
+ image : np .ndarray ,
371
+ average_depiction_size : Tuple [int , int ]
372
+ ) -> np .ndarray :
370
373
"""
371
374
This function takes an image and returns a binary mask that labels the pixels that
372
375
are part of long horizontal or vertical lines. [Definition of long: 1/5 of the
@@ -382,19 +385,19 @@ def detect_horizontal_and_vertical_lines(image: np.ndarray) -> np.ndarray:
382
385
"""
383
386
binarised_im = ~ image * 255
384
387
binarised_im = binarised_im .astype ("uint8" )
388
+
389
+ structure_height , structure_width = average_depiction_size
385
390
386
- horizontal_kernel_size = int (binarised_im .shape [1 ] / 7 )
387
391
horizontal_kernel = cv2 .getStructuringElement (
388
- cv2 .MORPH_RECT , (horizontal_kernel_size , 1 )
392
+ cv2 .MORPH_RECT , (structure_width , 1 )
389
393
)
390
394
horizontal_mask = cv2 .morphologyEx (
391
395
binarised_im , cv2 .MORPH_OPEN , horizontal_kernel , iterations = 2
392
396
)
393
397
horizontal_mask = horizontal_mask == 255
394
398
395
- vertical_kernel_size = int (binarised_im .shape [0 ] / 7 )
396
399
vertical_kernel = cv2 .getStructuringElement (
397
- cv2 .MORPH_RECT , (1 , vertical_kernel_size )
400
+ cv2 .MORPH_RECT , (1 , structure_height )
398
401
)
399
402
vertical_mask = cv2 .morphologyEx (
400
403
binarised_im , cv2 .MORPH_OPEN , vertical_kernel , iterations = 2
@@ -472,13 +475,30 @@ def expansion_coordination(
472
475
473
476
474
477
def complete_structure_mask (
475
- image_array : np .array , mask_array : np .array , debug = False
478
+ image_array : np .array ,
479
+ mask_array : np .array ,
480
+ average_depiction_size : Tuple [int , int ],
481
+ debug = False
476
482
) -> np .array :
477
483
"""
478
- This funtion takes an image (array) and an array containing the masks (shape:
484
+ This funtion takes an image (np. array) and an array containing the masks (shape:
479
485
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
+ which is used to define the kernel size for the vertical and horizontal line
488
+ detection for the exclusion masks. The exclusion mask is used to exclude pixels
489
+ from the mask expansion to avoid including whole tables.
480
490
It detects objects on the contours of the mask and expands it until it frames the
481
- complete object in the image. It returns the expanded mask array"""
491
+ complete object in the image. It returns the expanded mask array
492
+
493
+ Args:
494
+ image_array (np.array): input image
495
+ 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
+ debug (bool, optional): More verbose if True. Defaults to False.
498
+
499
+ Returns:
500
+ np.array: expanded mask array
501
+ """
482
502
483
503
if mask_array .size != 0 :
484
504
# Binarization of input image
@@ -498,7 +518,8 @@ def complete_structure_mask(
498
518
split_mask_arrays = np .array (
499
519
[mask_array [:, :, index ] for index in range (mask_array .shape [2 ])]
500
520
)
501
- exclusion_mask = detect_horizontal_and_vertical_lines (blurred_image_array )
521
+ exclusion_mask = detect_horizontal_and_vertical_lines (blurred_image_array ,
522
+ average_depiction_size )
502
523
# Run expansion the expansion
503
524
image_repeat = itertools .repeat (blurred_image_array , mask_array .shape [2 ])
504
525
exclusion_mask_repeat = itertools .repeat (exclusion_mask , mask_array .shape [2 ])
0 commit comments