3
3
import os .path
4
4
import numpy as np
5
5
from PIL import Image , ImageStat , ImageOps
6
- from scipy .ndimage import filters
7
6
from shapely .geometry import Polygon
8
7
from shapely .prepared import prep
9
8
@@ -128,7 +127,7 @@ def process(self):
128
127
else :
129
128
background = background .median [0 ]
130
129
if level == 'region' :
131
- background_image = Image .new ('L' , page_image .size , background )
130
+ background_image = Image .new (page_image . mode , page_image .size , background )
132
131
page_array = pil2array (page_image )
133
132
page_bin = np .array (page_array <= midrange (page_array ), np .uint8 )
134
133
# in absolute coordinates merely for comparison/intersection
@@ -137,13 +136,14 @@ def process(self):
137
136
# in relative coordinates for mask/cropping
138
137
polygons = [coordinates_of_segment (region , page_image , page_coords )
139
138
for region in regions ]
140
- masks = [pil2array (polygon_mask (page_image , polygon )).astype (np .uint8 )
141
- for polygon in polygons ]
142
- for i , mask in enumerate (masks [num_texts :], num_texts ):
139
+ for i , polygon in enumerate (polygons [num_texts :], num_texts ):
143
140
# for non-text regions, extend mask by 3 pixels in each direction
144
141
# to ensure they do not leak components accidentally
145
142
# (accounts for bad cropping of such regions in GT):
146
- masks [i ] = filters .maximum_filter (mask , 7 )
143
+ polygon = Polygon (polygon ).buffer (3 ).exterior .coords [:- 1 ] # keep open
144
+ polygons [i ] = polygon
145
+ masks = [pil2array (polygon_mask (page_image , polygon )).astype (np .uint8 )
146
+ for polygon in polygons ]
147
147
for i , region in enumerate (regions ):
148
148
if i >= num_texts :
149
149
break # keep non-text regions unchanged
@@ -172,7 +172,7 @@ def process(self):
172
172
continue
173
173
region_image , region_coords = self .workspace .image_from_segment (
174
174
region , page_image , page_coords , feature_selector = 'binarized' )
175
- background_image = Image .new ('L' , region_image .size , background )
175
+ background_image = Image .new (region_image . mode , region_image .size , background )
176
176
region_array = pil2array (region_image )
177
177
region_bin = np .array (region_array <= midrange (region_array ), np .uint8 )
178
178
# in absolute coordinates merely for comparison/intersection
0 commit comments