-
Notifications
You must be signed in to change notification settings - Fork 0
/
crop.py
832 lines (721 loc) · 26.3 KB
/
crop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
from typing import Any, Optional, Tuple, List, Dict, Union
import types
import numpy as np
import cv2
from parameters import ErodeParameters, CannyParameters, HoughLinesParameters
from page.find_images import FindImageParameters
import page.find_images
import cv2ext
import compute
class FoundDataTry1Parameters:
class Impl(types.SimpleNamespace):
erode: ErodeParameters = ErodeParameters((9, 9), 1)
threshold: int = 240
pourcentage_ecart_rectangle: float = 10.0
def __init__(self) -> None:
self.__param = FoundDataTry1Parameters.Impl()
@property
def erode(self) -> ErodeParameters:
return self.__param.erode
@property
def threshold(self) -> int:
return self.__param.threshold
@threshold.setter
def threshold(self, val: int) -> None:
self.__param.threshold = val
@property
def pourcentage_ecart_rectangle(
self,
) -> float:
return self.__param.pourcentage_ecart_rectangle
@pourcentage_ecart_rectangle.setter
def pourcentage_ecart_rectangle(self, val: float) -> None:
self.__param.pourcentage_ecart_rectangle = val
class FoundDataTry2Parameters:
class Impl(types.SimpleNamespace):
blur_size: Tuple[int, int] = (10, 10)
threshold_gray: int = 200
kernel_morpho_size: Tuple[int, int] = (10, 10)
canny_gray: CannyParameters = CannyParameters(25, 255, 5)
hough_lines_gray: HoughLinesParameters = HoughLinesParameters(
1, np.pi / (180 * 20), 30, 100, 30
)
threshold_histogram: int = 15
canny_histogram: CannyParameters = CannyParameters(25, 255, 5)
hough_lines_histogram: HoughLinesParameters = HoughLinesParameters(
1, np.pi / (180 * 20), 30, 100, 30
)
find_images: FindImageParameters = FindImageParameters(
5,
(10, 10),
(10, 10),
(10, 10),
0.01,
)
def __init__(self) -> None:
self.__param = FoundDataTry2Parameters.Impl()
@property
def blur_size(self) -> Tuple[int, int]:
return self.__param.blur_size
@blur_size.setter
def blur_size(self, val: Tuple[int, int]) -> None:
self.__param.blur_size = val
@property
def threshold_gray(self) -> int:
return self.__param.threshold_gray
@threshold_gray.setter
def threshold_gray(self, val: int) -> None:
self.__param.threshold_gray = val
@property
def kernel_morpho_size(self) -> Tuple[int, int]:
return self.__param.kernel_morpho_size
@kernel_morpho_size.setter
def kernel_morpho_size(self, val: Tuple[int, int]) -> None:
self.__param.kernel_morpho_size = val
@property
def canny_gray(self) -> CannyParameters:
return self.__param.canny_gray
@property
def hough_lines_gray(
self,
) -> HoughLinesParameters:
return self.__param.hough_lines_gray
@property
def threshold_histogram(self) -> int:
return self.__param.threshold_histogram
@threshold_histogram.setter
def threshold_histogram(self, val: int) -> None:
self.__param.threshold_histogram = val
@property
def canny_histogram(self) -> CannyParameters:
return self.__param.canny_histogram
@property
def hough_lines_histogram(
self,
) -> HoughLinesParameters:
return self.__param.hough_lines_histogram
@property
def find_images(self) -> FindImageParameters:
return self.__param.find_images
class CropAroundDataInPageParameters:
class Impl(types.SimpleNamespace):
found_data_try1: FoundDataTry1Parameters = FoundDataTry1Parameters()
found_data_try2: FoundDataTry2Parameters = FoundDataTry2Parameters()
dilate_size: Tuple[int, int] = (5, 5)
threshold2: int = 200
contour_area_min: float = 0.01 * 0.01
contour_area_max: float = 1.0
border: int = 10
skip_rectangle_closed_to_line: float = 1.0
closed_to_edge: float = 0.02
def __init__(self) -> None:
self.__param = CropAroundDataInPageParameters.Impl()
@property
def found_data_try1(
self,
) -> FoundDataTry1Parameters:
return self.__param.found_data_try1
@property
def found_data_try2(
self,
) -> FoundDataTry2Parameters:
return self.__param.found_data_try2
@property
def dilate_size(self) -> Tuple[int, int]:
return self.__param.dilate_size
@dilate_size.setter
def dilate_size(self, val: Tuple[int, int]) -> None:
self.__param.dilate_size = val
@property
def threshold2(self) -> int:
return self.__param.threshold2
@threshold2.setter
def threshold2(self, val: int) -> None:
self.__param.threshold2 = val
@property
def contour_area_min(self) -> float:
return self.__param.contour_area_min
@contour_area_min.setter
def contour_area_min(self, val: float) -> None:
self.__param.contour_area_min = val
@property
def contour_area_max(self) -> float:
return self.__param.contour_area_max
@contour_area_max.setter
def contour_area_max(self, val: float) -> None:
self.__param.contour_area_max = val
@property
def border(self) -> int:
return self.__param.border
@border.setter
def border(self, val: int) -> None:
self.__param.border = val
@property
def skip_rectangle_closed_to_line(self) -> float:
return self.__param.skip_rectangle_closed_to_line
@skip_rectangle_closed_to_line.setter
def skip_rectangle_closed_to_line(self, val: float) -> None:
self.__param.skip_rectangle_closed_to_line = val
@property
def closed_to_edge(self) -> float:
return self.__param.closed_to_edge
@closed_to_edge.setter
def closed_to_edge(self, val: float) -> None:
self.__param.closed_to_edge = val
def init_default_values(
self,
key: str,
value: Union[int, float, Tuple[int, int]],
) -> None:
if key.startswith("Erode"):
self.found_data_try1.erode.init_default_values(
key[len("Erode") :], value
)
elif key == "Threshold1" and isinstance(value, int):
self.found_data_try1.threshold = value
elif key == "PourcentageEcartRectangle" and isinstance(value, float):
self.found_data_try1.pourcentage_ecart_rectangle = value
elif key == "DilateSize" and isinstance(value, tuple):
self.dilate_size = value
elif key == "Threshold2" and isinstance(value, int):
self.threshold2 = value
elif key == "ContourAreaMin" and isinstance(value, float):
self.contour_area_min = value
elif key == "ContourAreaMax" and isinstance(value, float):
self.contour_area_max = value
elif key == "Border" and isinstance(value, int):
self.border = value
else:
raise Exception("Invalid property.", key)
def found_data_try1(
image: Any,
n_page: int,
param: FoundDataTry1Parameters,
enable_debug: Optional[str] = None,
) -> Optional[Any]:
gray = cv2ext.convertion_en_niveau_de_gris(image)
eroded = cv2.erode(
gray,
cv2.getStructuringElement(cv2.MORPH_ELLIPSE, param.erode.size),
iterations=param.erode.iterations,
)
cv2ext.write_image_if(eroded, enable_debug, "_" + str(n_page) + "_2.png")
_, threshold = cv2.threshold(
eroded,
param.threshold,
255,
cv2.THRESH_BINARY,
)
cv2ext.write_image_if(
threshold, enable_debug, "_" + str(n_page) + "_3.png"
)
# On récupère le contour le plus grand.
contours, _ = cv2.findContours(
threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
contour_max = max(contours, key=cv2.contourArea)
if enable_debug is not None:
image2222 = cv2.drawContours(
cv2ext.convertion_en_couleur(image), contours, -1, (0, 0, 255), 3
)
image2222 = cv2.drawContours(
image2222, [contour_max], 0, (0, 255, 0), 3
)
cv2ext.secure_write(
enable_debug + "_" + str(n_page) + "_4.png", image2222
)
# On garde le rectangle le plus grand.
rect = cv2ext.get_polygon_from_contour(contour_max, 4)
if enable_debug is not None:
image22223 = cv2.drawContours(image2222, [rect], -1, (255, 0, 0), 3)
cv2ext.secure_write(
enable_debug + "_" + str(n_page) + "_5.png", image22223
)
# Si on n'a pas de rectangle, on essaie de trouver le contour de la
# page avec les traits horizontaux et verticaux.
if not compute.is_contour_rectangle(
rect, param.pourcentage_ecart_rectangle
):
return None
return rect
def found_data_try2_find_edges(
image: Any,
n_page: int,
param: FoundDataTry2Parameters,
enable_debug: Optional[str] = None,
) -> List[Any]:
blurimg = cv2ext.force_image_to_be_grayscale(image, param.blur_size, True)
liste_lines = []
for i in range(2):
if i == 0:
threshold_param_i = param.threshold_gray
canny_param_i = param.canny_gray
hough_lines_param_i = param.hough_lines_gray
morpho_mode1 = cv2.MORPH_OPEN
morpho_mode2 = cv2.MORPH_CLOSE
blurimg2 = blurimg
else:
threshold_param_i = param.threshold_histogram
canny_param_i = param.canny_histogram
hough_lines_param_i = param.hough_lines_histogram
morpho_mode1 = cv2.MORPH_CLOSE
morpho_mode2 = cv2.MORPH_OPEN
blurimg_bc = cv2ext.apply_brightness_contrast(blurimg, -96, 64)
cv2ext.write_image_if(
blurimg_bc,
enable_debug,
"_" + str(n_page) + "_" + str(i) + "_5_.png",
)
blurimg2 = cv2.equalizeHist(blurimg_bc)
cv2ext.write_image_if(
blurimg2,
enable_debug,
"_" + str(n_page) + "_" + str(i) + "_5a.png",
)
_, threshold = cv2.threshold(
blurimg2,
threshold_param_i,
255,
cv2.THRESH_BINARY,
)
cv2ext.write_image_if(
threshold,
enable_debug,
"_" + str(n_page) + "_" + str(i) + "_5b.png",
)
morpho1 = cv2.morphologyEx(
threshold,
morpho_mode1,
cv2.getStructuringElement(
cv2.MORPH_ELLIPSE, param.kernel_morpho_size
),
)
cv2ext.write_image_if(
morpho1,
enable_debug,
"_" + str(n_page) + "_" + str(i) + "_5b1.png",
)
morpho2 = cv2.morphologyEx(
morpho1,
morpho_mode2,
cv2.getStructuringElement(
cv2.MORPH_ELLIPSE, param.kernel_morpho_size
),
)
cv2ext.write_image_if(
morpho2,
enable_debug,
"_" + str(n_page) + "_" + str(i) + "_5b2.png",
)
canny = cv2.Canny(
morpho2,
canny_param_i.minimum,
canny_param_i.maximum,
apertureSize=canny_param_i.aperture_size,
)
cv2ext.write_image_if(
canny, enable_debug, "_" + str(n_page) + "_" + str(i) + "_5c.png"
)
lines_i = cv2.HoughLinesP(
canny,
hough_lines_param_i.delta_rho,
hough_lines_param_i.delta_tetha,
hough_lines_param_i.threshold,
minLineLength=hough_lines_param_i.min_line_length,
maxLineGap=hough_lines_param_i.max_line_gap,
)
liste_lines.extend(lines_i)
if enable_debug is not None:
image_with_lines = cv2ext.convertion_en_couleur(image)
for line in lines_i:
for point1_x, point1_y, point2_x, point2_y in line:
cv2.line(
image_with_lines,
(point1_x, point1_y),
(point2_x, point2_y),
(0, 0, 255),
1,
)
cv2ext.secure_write(
enable_debug + "_" + str(n_page) + "_" + str(i) + "_5d.png",
image_with_lines,
)
height, width = cv2ext.get_hw(image)
liste_lines.append(np.array([[0, 0, 0, height - 1]], dtype=int))
liste_lines.append(np.array([[0, 0, width - 1, 0]], dtype=int))
liste_lines.append(
np.array([[width - 1, 0, width - 1, height - 1]], dtype=int)
)
liste_lines.append(
np.array([[0, height - 1, width - 1, height - 1]], dtype=int)
)
return liste_lines
def found_data_try2_filter_edges(
liste_lines: List[Any], images_mask: Any
) -> Tuple[
List[Tuple[Tuple[int, int], Tuple[int, int]]],
List[Tuple[Tuple[int, int], Tuple[int, int]]],
]:
"""Edges must be vertical or horizontal and must be not cross images."""
lines_vertical_angle = []
lines_horizontal_angle = []
delta_angle = 3
for line in liste_lines:
point1_x, point1_y, point2_x, point2_y = line[0]
angle = compute.get_angle_0_180(
(point1_x, point1_y), (point2_x, point2_y)
)
if 90 - delta_angle <= angle <= 90 + delta_angle:
angle, posx = compute.get_angle_0_180_posx_safe(
(point1_x, point1_y), (point2_x, point2_y)
)
image_line = np.zeros(images_mask.shape, np.uint8)
cv2.line(
image_line,
(posx, 0),
compute.get_bottom_point_from_alpha_posx(
angle, posx, images_mask.shape[0]
),
(255, 255, 255),
1,
)
image_line = cv2.bitwise_and(images_mask, image_line)
if cv2.countNonZero(image_line) == 0:
lines_vertical_angle.append(
((point1_x, point1_y), (point2_x, point2_y))
)
if angle <= delta_angle or angle > 180 - delta_angle:
angle, posy = compute.get_angle_0_180_posy_safe(
(point1_x, point1_y), (point2_x, point2_y)
)
image_line = np.zeros(images_mask.shape, np.uint8)
cv2.line(
image_line,
(0, posy),
compute.get_right_point_from_alpha_posy(
angle, posy, images_mask.shape[1]
),
(255, 255, 255),
1,
)
image_line = cv2.bitwise_and(images_mask, image_line)
if cv2.countNonZero(image_line) == 0:
lines_horizontal_angle.append(
((point1_x, point1_y), (point2_x, point2_y))
)
return lines_vertical_angle, lines_horizontal_angle
def found_data_try2_remove_duplicated_edges(
lines_vertical_angle: List[Tuple[Tuple[int, int], Tuple[int, int]]],
lines_horizontal_angle: List[Tuple[Tuple[int, int], Tuple[int, int]]],
) -> Tuple[
List[Tuple[Tuple[int, int], Tuple[int, int]]],
List[Tuple[Tuple[int, int], Tuple[int, int]]],
]:
histogram_vertical: Dict[int, int] = dict()
histogram_horizontal: Dict[int, int] = dict()
histogram_vertical_points: Dict[
int, Tuple[Tuple[int, int], Tuple[int, int]]
] = dict()
histogram_horizontal_points: Dict[
int, Tuple[Tuple[int, int], Tuple[int, int]]
] = dict()
for line in lines_vertical_angle:
pt1, pt2 = line
_, posx = compute.get_angle_0_180_posx_safe(pt1, pt2)
histogram_vertical[posx] = histogram_vertical.get(posx, 0) + 1
histogram_vertical_points[posx] = line
for line in lines_horizontal_angle:
pt1, pt2 = line
_, posy = compute.get_angle_0_180_posy_safe(pt1, pt2)
histogram_horizontal[posy] = histogram_horizontal.get(posy, 0) + 1
histogram_horizontal_points[posy] = line
histogram_vertical_arr = np.zeros(max(histogram_vertical.keys()) + 1)
histogram_horizontal_arr = np.zeros(max(histogram_horizontal.keys()) + 1)
for key, value in histogram_vertical.items():
histogram_vertical_arr[key] = value
for key, value in histogram_horizontal.items():
histogram_horizontal_arr[key] = value
v_smooth = cv2.GaussianBlur(
histogram_vertical_arr, (9, 9), 9, 9, cv2.BORDER_REPLICATE
)
h_smooth = cv2.GaussianBlur(
histogram_horizontal_arr, (9, 9), 9, 9, cv2.BORDER_REPLICATE
)
lines_vertical_angle_keep: List[
Tuple[Tuple[int, int], Tuple[int, int]]
] = []
lines_horizontal_angle_keep: List[
Tuple[Tuple[int, int], Tuple[int, int]]
] = []
lines_vertical_angle_keep = compute.get_top_histogram(
v_smooth, histogram_vertical_points
)
lines_horizontal_angle_keep = compute.get_top_histogram(
h_smooth, histogram_horizontal_points
)
return lines_vertical_angle_keep, lines_horizontal_angle_keep
def convert_line_to_contour(
line0: Tuple[Tuple[int, int], Tuple[int, int]],
line1: Tuple[Tuple[int, int], Tuple[int, int]],
line2: Tuple[Tuple[int, int], Tuple[int, int]],
line3: Tuple[Tuple[int, int], Tuple[int, int]],
) -> Any:
point1_x, point1_y = compute.line_intersection(line0, line2)
point2_x, point2_y = compute.line_intersection(line0, line3)
point3_x, point3_y = compute.line_intersection(line1, line2)
point4_x, point4_y = compute.line_intersection(line1, line3)
xmoy = (point1_x + point2_x + point3_x + point4_x) // 4
ymoy = (point1_y + point2_y + point3_y + point4_y) // 4
list_of_points = [
[point1_x, point1_y],
[point2_x, point2_y],
[point3_x, point3_y],
[point4_x, point4_y],
]
list_of_points.sort(
key=lambda x: compute.get_angle__180_180((xmoy, ymoy), (x[0], x[1]))
)
return np.asarray(list_of_points)
def found_data_try2_is_contour_around_images(
zone: Tuple[int, int, int, int],
lines_vertical_angle: List[Tuple[Tuple[int, int], Tuple[int, int]]],
lines_horizontal_angle: List[Tuple[Tuple[int, int], Tuple[int, int]]],
images_mask: Any,
) -> Optional[Any]:
cnti = convert_line_to_contour(
lines_vertical_angle[zone[0]],
lines_vertical_angle[zone[1]],
lines_horizontal_angle[zone[2]],
lines_horizontal_angle[zone[3]],
)
mask = np.zeros(images_mask.shape, np.uint8)
mask = cv2.drawContours(mask, [cnti], -1, (255, 255, 255), -1)
mask = cv2.bitwise_and(images_mask, mask)
difference = cv2.subtract(images_mask, mask)
if cv2.countNonZero(difference) == 0:
return cnti
return None
def found_data_try2_find_smallest_rectangular_with_all_images_inside(
lines_vertical_angle: List[Tuple[Tuple[int, int], Tuple[int, int]]],
lines_horizontal_angle: List[Tuple[Tuple[int, int], Tuple[int, int]]],
images_mask: Any,
) -> Any:
# Keep the smallest rectangle that have inside all images.
flag_v_min: List[bool] = []
for v_i in range(len(lines_vertical_angle)):
v_i_min = v_i
v_i_max = len(lines_vertical_angle) - 1
h_i_min = 0
h_i_max = len(lines_horizontal_angle) - 1
cnti = found_data_try2_is_contour_around_images(
(v_i_min, v_i_max, h_i_min, h_i_max),
lines_vertical_angle,
lines_horizontal_angle,
images_mask,
)
flag_v_min.append(cnti is not None)
flag_v_max: List[bool] = []
for v_i in range(len(lines_vertical_angle) - 1, -1, -1):
v_i_min = 0
v_i_max = v_i
h_i_min = 0
h_i_max = len(lines_horizontal_angle) - 1
cnti = found_data_try2_is_contour_around_images(
(v_i_min, v_i_max, h_i_min, h_i_max),
lines_vertical_angle,
lines_horizontal_angle,
images_mask,
)
flag_v_max.insert(0, cnti is not None)
flag_h_min: List[bool] = []
for h_i in range(len(lines_horizontal_angle)):
v_i_min = 0
v_i_max = len(lines_vertical_angle) - 1
h_i_min = h_i
h_i_max = len(lines_horizontal_angle) - 1
cnti = found_data_try2_is_contour_around_images(
(v_i_min, v_i_max, h_i_min, h_i_max),
lines_vertical_angle,
lines_horizontal_angle,
images_mask,
)
flag_h_min.append(cnti is not None)
flag_h_max: List[bool] = []
for h_i in range(len(lines_horizontal_angle) - 1, -1, -1):
v_i_min = 0
v_i_max = len(lines_vertical_angle) - 1
h_i_min = 0
h_i_max = h_i
cnti = found_data_try2_is_contour_around_images(
(v_i_min, v_i_max, h_i_min, h_i_max),
lines_vertical_angle,
lines_horizontal_angle,
images_mask,
)
flag_h_max.insert(0, cnti is not None)
return cv2ext.bounding_rectangle(
cv2ext.get_hw(images_mask),
(lines_vertical_angle, lines_horizontal_angle),
(flag_v_min, flag_v_max, flag_h_min, flag_h_max),
)
def found_data_try2(
image: Any,
n_page: int,
param: FoundDataTry2Parameters,
page_angle: float,
enable_debug: Optional[str] = None,
) -> Any:
liste_lines = found_data_try2_find_edges(
image, n_page, param, enable_debug
)
images_mask = page.find_images.find_images(
image,
param.find_images,
page_angle,
compute.optional_concat(enable_debug, "_A_crop_" + str(n_page)),
)
if np.all(images_mask == 0):
raise Exception("Failed to found images.")
(
lines_vertical_angle,
lines_horizontal_angle,
) = found_data_try2_filter_edges(liste_lines, images_mask)
(
lines_vertical_angle,
lines_horizontal_angle,
) = found_data_try2_remove_duplicated_edges(
lines_vertical_angle, lines_horizontal_angle
)
lines_vertical_angle.sort(
key=lambda x: compute.get_angle_0_180_posx_safe(x[0], x[1])[1]
)
lines_horizontal_angle.sort(
key=lambda x: compute.get_angle_0_180_posy_safe(x[0], x[1])[1]
)
return found_data_try2_find_smallest_rectangular_with_all_images_inside(
lines_vertical_angle, lines_horizontal_angle, images_mask
)
def crop_around_page(
image: Any,
n_page: int,
parameters: CropAroundDataInPageParameters,
page_angle: float,
enable_debug: Optional[str] = None,
) -> Tuple[int, int, int, int]:
cv2ext.write_image_if(image, enable_debug, "_" + str(n_page) + "_1.png")
rect = page.crop.found_data_try1(
image, n_page, parameters.found_data_try1, enable_debug
)
if rect is None:
rect = page.crop.found_data_try2(
image, n_page, parameters.found_data_try2, page_angle, enable_debug
)
if enable_debug is not None:
image_cnt = cv2ext.convertion_en_couleur(image)
cv2.drawContours(image_cnt, [rect], 0, (0, 0, 255), 3)
cv2ext.secure_write(
enable_debug + "_" + str(n_page) + "_1_6.png", image_cnt
)
x_crop1 = [rect[0, 0, 0], rect[1, 0, 0], rect[2, 0, 0], rect[3, 0, 0]]
y_crop1 = [rect[0, 0, 1], rect[1, 0, 1], rect[2, 0, 1], rect[3, 0, 1]]
x_crop1.sort()
y_crop1.sort()
return (x_crop1[1], x_crop1[2], y_crop1[1], y_crop1[2])
def crop_around_data(
page_gauche_0: Any,
n_page: int,
parameters: CropAroundDataInPageParameters,
enable_debug: Optional[str] = None,
) -> Optional[Any]:
# On enlève les bordures noirs sur le bord des pages.
imgh, imgw = cv2ext.get_hw(page_gauche_0)
min_x, min_y = imgw, imgh
max_x = max_y = 0
gray = cv2ext.convertion_en_niveau_de_gris(page_gauche_0)
dilated = cv2ext.erode_and_dilate(
gray, parameters.dilate_size, parameters.dilate_size[0]
)
cv2ext.write_image_if(dilated, enable_debug, "_" + str(n_page) + "_7.png")
_, threshold = cv2.threshold(
dilated,
parameters.threshold2,
255,
cv2.THRESH_BINARY,
)
cv2ext.write_image_if(
threshold, enable_debug, "_" + str(n_page) + "_8.png"
)
threshold2 = cv2.copyMakeBorder(
threshold,
1,
1,
1,
1,
cv2.BORDER_CONSTANT,
value=[255],
)
contours, hierarchy = cv2.findContours(
threshold2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
cv2ext.remove_border_in_contours(contours, 1, threshold)
if enable_debug is not None:
image2222 = cv2ext.convertion_en_couleur(page_gauche_0)
image2222 = cv2.rectangle(
image2222,
(
int(parameters.closed_to_edge * imgw),
int(parameters.closed_to_edge * imgh),
),
(
int((1 - parameters.closed_to_edge) * imgw),
int((1 - parameters.closed_to_edge) * imgh),
),
(255, 0, 0),
1
)
ncontour_good_size = False
first_cnt_all = int(cv2.contourArea(contours[0])) == (imgh - 1) * (
imgw - 1
)
def is_border(contour: Any) -> bool:
rectangle = cv2.boundingRect(contour)
ratio = rectangle[3] / rectangle[2]
if ratio >= parameters.skip_rectangle_closed_to_line and (
rectangle[0] + rectangle[2] < parameters.closed_to_edge * imgw
or rectangle[0] > (1 - parameters.closed_to_edge) * imgw
):
return True
if ratio <= 1 / parameters.skip_rectangle_closed_to_line and (
rectangle[1] + rectangle[3] < parameters.closed_to_edge * imgh
or rectangle[1] > (1 - parameters.closed_to_edge) * imgh
):
return True
return False
contours_listered = filter(
lambda x: parameters.contour_area_min * imgh * imgw
< cv2.contourArea(x[0])
< parameters.contour_area_max * imgh * imgw
and (
(x[1][3] == -1 and not first_cnt_all)
or (x[1][3] == 0 and first_cnt_all)
)
and not is_border(x[0]),
zip(contours, hierarchy[0]),
)
for cnt, _ in contours_listered:
(point_x, point_y, width, height) = cv2.boundingRect(cnt)
if enable_debug is not None:
cv2.drawContours(image2222, [cnt], -1, (0, 0, 255), 3)
min_x = min(point_x, min_x)
max_x = max(point_x + width, max_x)
min_y = min(point_y, min_y)
max_y = max(point_y + height, max_y)
ncontour_good_size = True
if enable_debug is not None:
cv2ext.secure_write(
enable_debug + "_" + str(n_page) + "_9.png",
image2222,
)
if not ncontour_good_size:
return None
return (min_x, max_x, min_y, max_y)