mirror of
https://github.com/ciromattia/kcc
synced 2026-09-14 23:07:09 +00:00
fix cropping with edge imperfections (#1435)
* improve detection of edge imperfections for cropping * increase imperfections ratio * fil in edge too * fine tune the constants
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from PIL import ImageOps, ImageFilter, ImageFile
|
||||
from PIL.Image import Image
|
||||
import numpy as np
|
||||
from .common_crop import threshold_from_power, group_close_values
|
||||
|
||||
@@ -149,22 +150,39 @@ def get_bbox_crop_margin(img, power=1, background_color='white'):
|
||||
|
||||
return bw_img.getbbox()
|
||||
|
||||
def ignore_pixels_near_edge(bw_img):
|
||||
def ignore_pixels_near_edge(bw_img: Image):
|
||||
w, h = bw_img.size
|
||||
# going 0.02-0.03 was too much
|
||||
if int(0.02 * h) == int(0.025 * h):
|
||||
return
|
||||
if int(0.02 * w) == int(0.025 * w):
|
||||
return
|
||||
edge_bbox = [
|
||||
(0, 0, w, int(0.02 * h)),
|
||||
(0, int(0.98 * h), w, h),
|
||||
(0, 0, int(0.02 * w), h),
|
||||
(int(0.98 * w), 0, w, h)
|
||||
]
|
||||
for box in edge_bbox:
|
||||
edge = bw_img.crop(box)
|
||||
h = edge.histogram()
|
||||
if not edge.height or not edge.width:
|
||||
continue
|
||||
imperfections = h[255] / (edge.height * edge.width)
|
||||
if imperfections > 0 and imperfections < .02:
|
||||
bw_img.paste(im=0, box=box)
|
||||
|
||||
inner_bbox = [
|
||||
(int(0.02 * w), int(0.02 * h), int(0.98 * w), int(0.025 * h)), # top
|
||||
(int(0.02 * w), int(0.975 * h), int(0.98 * w), int(0.98 * h)), # lower
|
||||
(int(0.02 * w), int(0.02 * h), int(0.025 * w), int(0.98 * h)), # left
|
||||
(int(0.975 * w), int(0.02 * h), int(0.98 * w), int(0.98 * h)), # right
|
||||
]
|
||||
|
||||
for edge_box, inner_box in zip(edge_bbox, inner_bbox):
|
||||
inner_edge = bw_img.crop(inner_box)
|
||||
h = inner_edge.histogram()
|
||||
imperfections = h[255] / (inner_edge.height * inner_edge.width)
|
||||
|
||||
if imperfections > 0 and imperfections < .001:
|
||||
bw_img.paste(im=0, box=inner_box)
|
||||
if imperfections < .001:
|
||||
edge = bw_img.crop(edge_box)
|
||||
edge_h = edge.histogram()
|
||||
if edge_h[-1] != 0:
|
||||
bw_img.paste(im=0, box=edge_box)
|
||||
|
||||
|
||||
def box_intersect(box1, box2, max_dist):
|
||||
|
||||
Reference in New Issue
Block a user