537 lines
21 KiB
Python
537 lines
21 KiB
Python
# coding=utf-8
|
|
"""
|
|
Traitement d'image de l'extension « Board photo », sans dependance a inkex.
|
|
|
|
Detection des reperes de calibration, redressement de la photo et
|
|
vectorisation de la planche. Dependances : numpy et Pillow, tous deux
|
|
fournis avec le Python d'Inkscape 1.x (pas d'OpenCV ni de scipy : les
|
|
composantes connexes sont calculees ici, par plages de pixels).
|
|
|
|
Memes conventions que board_photo_core : millimetres, repere SVG (y vers le
|
|
bas), coordonnees image continues (centre du pixel (r, c) en (c+0.5, r+0.5)).
|
|
"""
|
|
|
|
import io
|
|
import itertools
|
|
import math
|
|
|
|
import numpy as np
|
|
from PIL import Image, ImageFilter, ImageOps
|
|
|
|
import board_photo_core as core
|
|
|
|
# Photo reduite a ce cote maximal pour la recherche des reperes (rapide, et
|
|
# le centre est ensuite affine en pleine resolution).
|
|
SEARCH_SIDE = 1600
|
|
|
|
# Passes du seuil adaptatif : (diviseur du cote de l'image donnant le rayon
|
|
# du voisinage, ecart de gris). La premiere convient aux reperes bien visibles ;
|
|
# les suivantes, plus locales, aux petits reperes (photo basse resolution ou
|
|
# prise de loin) colles a des pieces sombres qui assombrissent le voisinage.
|
|
ADAPTIVE_PASSES = ((12, 20), (32, 20), (32, 10), (64, 20), (64, 10))
|
|
|
|
# Moment d'inertie normalise (Ixx + Iyy) / aire^2 : 1/6 pour un carre, quelle
|
|
# que soit sa taille ; une perspective moderee le change peu.
|
|
SQUARE_MOMENT = 1.0 / 6.0
|
|
|
|
# Ecart minimal (niveaux de gris) entre planche et fond pour vectoriser.
|
|
MIN_CONTRAST = 25
|
|
|
|
|
|
class MarkerError(Exception):
|
|
"""Reperes introuvables ; `reason` est un code court, `found` le nombre vu."""
|
|
|
|
def __init__(self, reason, found=0):
|
|
Exception.__init__(self, reason)
|
|
self.reason = reason
|
|
self.found = found
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Chargement
|
|
# --------------------------------------------------------------------------
|
|
|
|
def load_photo(source):
|
|
"""Ouvre une photo (chemin ou octets), redressee selon son EXIF, en RVB."""
|
|
if isinstance(source, (bytes, bytearray)):
|
|
source = io.BytesIO(source)
|
|
image = Image.open(source)
|
|
image = ImageOps.exif_transpose(image)
|
|
return image.convert("RGB")
|
|
|
|
|
|
def rotate_photo(image, angle):
|
|
"""Photo tournee de `angle` degres (multiple de 90) dans le sens horaire.
|
|
|
|
PIL tourne dans le sens trigonometrique : d'ou l'angle negatif. Pour un
|
|
quart de tour, rotate() passe par transpose (sans perte ni
|
|
interpolation) et `expand` echange largeur et hauteur.
|
|
"""
|
|
angle = int(angle) % 360
|
|
if angle % 90:
|
|
raise ValueError("rotation must be a multiple of 90 degrees: {}".format(angle))
|
|
if not angle:
|
|
return image
|
|
return image.rotate(-angle, expand=True)
|
|
|
|
|
|
def gray_array(image):
|
|
"""Niveaux de gris (0-255) en tableau numpy uint8."""
|
|
return np.asarray(image.convert("L"), dtype=np.uint8)
|
|
|
|
|
|
def encode_image(image, fmt="JPEG", quality=90):
|
|
"""Octets d'une image PIL (JPEG par defaut), pour l'incorporer au SVG."""
|
|
buffer = io.BytesIO()
|
|
if fmt == "JPEG":
|
|
image.convert("RGB").save(buffer, "JPEG", quality=int(quality), optimize=True)
|
|
else:
|
|
image.save(buffer, fmt)
|
|
return buffer.getvalue()
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Composantes connexes (par plages de pixels, vectorise)
|
|
# --------------------------------------------------------------------------
|
|
|
|
def label_components(mask, connectivity=4):
|
|
"""Etiquette les composantes connexes d'un masque booleen 2D.
|
|
|
|
Renvoie (labels, count, areas) : image d'etiquettes int32 (0 = fond,
|
|
1..count), nombre de composantes et aire de chacune (areas[0] = 0).
|
|
Travaille sur les plages horizontales de pixels, bien moins nombreuses
|
|
que les pixels : c'est assez rapide sans scipy.
|
|
"""
|
|
mask = np.asarray(mask, dtype=bool)
|
|
h, w = mask.shape
|
|
stride = w + 2
|
|
padded = np.zeros((h, w + 2), dtype=np.int8)
|
|
padded[:, 1:-1] = mask
|
|
diff = np.diff(padded, axis=1)
|
|
rows, starts = np.nonzero(diff == 1)
|
|
_rows_end, ends = np.nonzero(diff == -1) # meme ordre que les debuts
|
|
count_runs = len(rows)
|
|
if count_runs == 0:
|
|
return np.zeros((h, w), dtype=np.int32), 0, np.zeros(1)
|
|
|
|
# Plages de la ligne precedente qui touchent chaque plage : recherche
|
|
# dichotomique sur des cles (ligne, colonne) globalement triees.
|
|
k = 1 if connectivity == 8 else 0
|
|
start_keys = rows * stride + starts
|
|
end_keys = rows * stride + ends
|
|
lo = np.searchsorted(end_keys, (rows - 1) * stride + starts - k, side="right")
|
|
hi = np.searchsorted(start_keys, (rows - 1) * stride + ends + k, side="left")
|
|
counts = np.clip(hi - lo, 0, None)
|
|
total = int(counts.sum())
|
|
if total:
|
|
pi = np.repeat(np.arange(count_runs), counts)
|
|
offsets = np.arange(total) - np.repeat(np.cumsum(counts) - counts, counts)
|
|
pj = np.repeat(lo, counts) + offsets
|
|
else:
|
|
pi = pj = np.zeros(0, dtype=np.int64)
|
|
|
|
# Fusion par accrochage au plus petit representant + compression des
|
|
# chemins, jusqu'a stabilite (quelques iterations).
|
|
parent = np.arange(count_runs)
|
|
while True:
|
|
ri, rj = parent[pi], parent[pj]
|
|
low = np.minimum(ri, rj)
|
|
new = parent.copy()
|
|
np.minimum.at(new, ri, low)
|
|
np.minimum.at(new, rj, low)
|
|
while True:
|
|
jumped = new[new]
|
|
if np.array_equal(jumped, new):
|
|
break
|
|
new = jumped
|
|
if np.array_equal(new, parent):
|
|
break
|
|
parent = new
|
|
|
|
roots, run_labels = np.unique(parent, return_inverse=True)
|
|
run_labels = run_labels.astype(np.int64) + 1
|
|
count = len(roots)
|
|
lengths = ends - starts
|
|
areas = np.bincount(run_labels, weights=lengths, minlength=count + 1)
|
|
|
|
# Peinture des plages : +label au debut, -label a la fin, puis somme cumulee.
|
|
paint = np.zeros((h, w + 1), dtype=np.int64)
|
|
np.add.at(paint, (rows, starts), run_labels)
|
|
np.add.at(paint, (rows, ends), -run_labels)
|
|
labels = np.cumsum(paint, axis=1)[:, :w].astype(np.int32)
|
|
return labels, count, areas
|
|
|
|
|
|
def component_boxes(labels, count):
|
|
"""Boites englobantes (r0, c0, r1, c1 exclus) de chaque etiquette 1..count."""
|
|
rr, cc = np.nonzero(labels)
|
|
lab = labels[rr, cc]
|
|
r0 = np.full(count + 1, labels.shape[0])
|
|
c0 = np.full(count + 1, labels.shape[1])
|
|
r1 = np.zeros(count + 1, dtype=np.int64)
|
|
c1 = np.zeros(count + 1, dtype=np.int64)
|
|
np.minimum.at(r0, lab, rr)
|
|
np.minimum.at(c0, lab, cc)
|
|
np.maximum.at(r1, lab, rr + 1)
|
|
np.maximum.at(c1, lab, cc + 1)
|
|
return r0, c0, r1, c1
|
|
|
|
|
|
def fill_holes(mask, connectivity=8, min_area=0):
|
|
"""Bouche les trous d'un masque.
|
|
|
|
Renvoie (masque plein, [masques des trous d'au moins `min_area` pixels]).
|
|
Les petits trous sont bouches mais pas renvoyes : on evite de construire
|
|
un masque par grain de bruit.
|
|
"""
|
|
mask = np.asarray(mask, dtype=bool)
|
|
labels, count, areas = label_components(~mask, connectivity)
|
|
border = set(np.concatenate([labels[0], labels[-1], labels[:, 0],
|
|
labels[:, -1]]).tolist())
|
|
inner = np.array([k not in border for k in range(count + 1)])
|
|
inner[0] = False
|
|
filled = mask | inner[labels]
|
|
holes = [labels == k for k in np.nonzero(inner & (areas >= min_area))[0].tolist()]
|
|
return filled, holes
|
|
|
|
|
|
def otsu_threshold(values):
|
|
"""Seuil d'Otsu (0-255) d'un ensemble de niveaux de gris."""
|
|
hist = np.bincount(np.asarray(values, dtype=np.uint8).ravel(), minlength=256)
|
|
hist = hist.astype(float)
|
|
total = hist.sum()
|
|
if total == 0:
|
|
return 128
|
|
levels = np.arange(256)
|
|
weight0 = np.cumsum(hist)
|
|
weight1 = total - weight0
|
|
sum0 = np.cumsum(hist * levels)
|
|
mean0 = np.divide(sum0, weight0, out=np.zeros(256), where=weight0 > 0)
|
|
mean1 = np.divide(sum0[-1] - sum0, weight1, out=np.zeros(256), where=weight1 > 0)
|
|
between = weight0 * weight1 * (mean0 - mean1) ** 2
|
|
return int(np.argmax(between))
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Detection des reperes de calibration
|
|
# --------------------------------------------------------------------------
|
|
|
|
def _shape_measures(filled):
|
|
"""Barycentre (x, y continus), moment normalise et rapport d'allongement."""
|
|
rr, cc = np.nonzero(filled)
|
|
area = float(len(rr))
|
|
x, y = cc + 0.5, rr + 0.5
|
|
mx, my = x.mean(), y.mean()
|
|
cov = np.cov(np.vstack([x, y]), bias=True) if area > 1 else np.eye(2)
|
|
eig = np.linalg.eigvalsh(cov)
|
|
moment = (cov[0, 0] + cov[1, 1]) / area if area else 0.0
|
|
ratio = eig[0] / eig[1] if eig[1] > 0 else 0.0
|
|
return (mx, my), moment, ratio, area
|
|
|
|
|
|
def analyse_marker(mask):
|
|
"""Examine une composante sombre isolee (masque recadre).
|
|
|
|
Renvoie un dictionnaire (barycentre, nombre de pastilles, aire pleine,
|
|
ecart au carre ideal) si la forme ressemble a un repere, None sinon.
|
|
"""
|
|
filled, holes = fill_holes(mask)
|
|
(cx, cy), moment, ratio, area = _shape_measures(filled)
|
|
if area < 36:
|
|
return None
|
|
rows, cols = filled.shape
|
|
if area / float(rows * cols) < 0.42 or ratio < 0.45:
|
|
return None
|
|
if not 0.14 <= moment <= 0.20:
|
|
return None
|
|
dots = []
|
|
for hole in holes:
|
|
hole_area = float(hole.sum())
|
|
if hole_area < 0.008 * area:
|
|
continue
|
|
(hx, hy), _m, _r, _a = _shape_measures(hole)
|
|
if math.hypot(hx - cx, hy - cy) > 0.4 * math.sqrt(area):
|
|
return None
|
|
dots.append(hole_area)
|
|
if len(dots) not in (1, 2):
|
|
return None
|
|
if not 0.02 <= sum(dots) / area <= 0.2:
|
|
return None
|
|
return {"center": (cx, cy), "dots": len(dots), "area": area,
|
|
"score": abs(moment - SQUARE_MOMENT)}
|
|
|
|
|
|
def _dark_mask_adaptive(gray, radius, offset=20):
|
|
"""Pixels nettement plus sombres que leur voisinage (seuil adaptatif)."""
|
|
image = Image.fromarray(gray)
|
|
local = np.asarray(image.filter(ImageFilter.BoxBlur(radius)), dtype=np.int16)
|
|
return gray.astype(np.int16) < local - offset
|
|
|
|
|
|
def _best_four(candidates):
|
|
"""Parmi les candidats, les 4 qui forment le plus grand quadrilatere convexe.
|
|
|
|
Les reperes ont tous la meme taille : on ecarte les combinaisons dont les
|
|
aires varient de plus d'un facteur 4 (perspective forte comprise).
|
|
"""
|
|
pool = sorted(candidates, key=lambda c: c["score"])[:12]
|
|
best, best_area = None, 0.0
|
|
for combo in itertools.combinations(pool, 4):
|
|
areas = [c["area"] for c in combo]
|
|
if max(areas) > 4 * min(areas):
|
|
continue
|
|
ordered = core.order_corners([c["center"] for c in combo])
|
|
if not core.is_convex_quad(ordered):
|
|
continue
|
|
quad = abs(core.ring_area(ordered))
|
|
if quad > best_area:
|
|
best, best_area = combo, quad
|
|
return list(best) if best else None
|
|
|
|
|
|
def _refine(gray, center, half):
|
|
"""Affine un repere en pleine resolution autour de `center` (seuil d'Otsu local)."""
|
|
h, w = gray.shape
|
|
cx, cy = center
|
|
r0, r1 = max(0, int(cy - half)), min(h, int(cy + half) + 1)
|
|
c0, c1 = max(0, int(cx - half)), min(w, int(cx + half) + 1)
|
|
crop = gray[r0:r1, c0:c1]
|
|
if crop.size == 0:
|
|
return None
|
|
dark = crop < otsu_threshold(crop)
|
|
labels, count, areas = label_components(dark, 8)
|
|
if not count:
|
|
return None
|
|
# Le centre tombe souvent sur une pastille claire : on essaie, de la plus
|
|
# petite a la plus grande, les composantes sombres dont la boite englobe
|
|
# le centre, et l'on garde la premiere qui a la forme d'un repere.
|
|
lr, lc = cy - r0, cx - c0
|
|
b0, a0, b1, a1 = component_boxes(labels, count)
|
|
around = [k for k in range(1, count + 1)
|
|
if b0[k] <= lr < b1[k] and a0[k] <= lc < a1[k]]
|
|
for k in sorted(around, key=lambda k: areas[k]):
|
|
box = labels[b0[k]:b1[k], a0[k]:a1[k]] == k
|
|
info = analyse_marker(box)
|
|
if info is not None:
|
|
x, y = info["center"]
|
|
info["center"] = (x + a0[k] + c0, y + b0[k] + r0)
|
|
return info
|
|
return None
|
|
|
|
|
|
def _same_marker(a, b):
|
|
"""Vrai si deux candidats designent le meme repere (passes successives)."""
|
|
(ax, ay), (bx, by) = a["center"], b["center"]
|
|
return math.hypot(ax - bx, ay - by) < 0.5 * math.sqrt(min(a["area"], b["area"]))
|
|
|
|
|
|
def _marker_candidates(small, factor, divisor, offset):
|
|
"""Formes de repere trouvees par une passe du seuil adaptatif.
|
|
|
|
`small` : photo reduite d'un facteur `factor` ; les centres et aires
|
|
renvoyes sont ramenes a la pleine resolution.
|
|
"""
|
|
sh, sw = small.shape
|
|
dark = _dark_mask_adaptive(small, max(4, max(sh, sw) // divisor), offset)
|
|
labels, count, areas = label_components(dark, 8)
|
|
if not count:
|
|
return []
|
|
min_area = 64
|
|
max_area = sh * sw / 40.0
|
|
r0, c0, r1, c1 = component_boxes(labels, count)
|
|
candidates = []
|
|
for k in np.nonzero((areas >= min_area) & (areas <= max_area))[0].tolist():
|
|
bh, bw = r1[k] - r0[k], c1[k] - c0[k]
|
|
if bh < 8 or bw < 8 or not 0.4 <= bw / float(bh) <= 2.5:
|
|
continue
|
|
crop = labels[r0[k]:r1[k], c0[k]:c1[k]] == k
|
|
info = analyse_marker(crop)
|
|
if info is None:
|
|
continue
|
|
x, y = info["center"]
|
|
info["center"] = ((x + c0[k]) / factor, (y + r0[k]) / factor)
|
|
info["area"] /= factor * factor
|
|
info["half"] = 0.9 * max(bh, bw) / factor
|
|
candidates.append(info)
|
|
return candidates
|
|
|
|
|
|
def detect_markers(gray):
|
|
"""Trouve les 4 reperes dans une photo (tableau de gris pleine resolution).
|
|
|
|
Renvoie un dictionnaire : `points` (4 centres en coordonnees image,
|
|
ordre HG, HD, BD, BG du repere SVG), `areas` (aires pleines en pixels),
|
|
`oriented` (vrai si le repere origine a fixe l'orientation). Leve
|
|
MarkerError sinon.
|
|
"""
|
|
h, w = gray.shape
|
|
factor = min(1.0, SEARCH_SIDE / float(max(h, w)))
|
|
if factor < 1.0:
|
|
small_img = Image.fromarray(gray).resize(
|
|
(max(1, round(w * factor)), max(1, round(h * factor))), Image.BOX)
|
|
small = np.asarray(small_img, dtype=np.uint8)
|
|
else:
|
|
small = gray
|
|
candidates = []
|
|
for divisor, offset in ADAPTIVE_PASSES:
|
|
for info in _marker_candidates(small, factor, divisor, offset):
|
|
if not any(_same_marker(info, other) for other in candidates):
|
|
candidates.append(info)
|
|
chosen = _best_four(candidates) if len(candidates) >= 4 else None
|
|
if chosen is not None:
|
|
break
|
|
if len(candidates) < 4:
|
|
raise MarkerError("too_few", len(candidates))
|
|
if chosen is None:
|
|
raise MarkerError("layout", len(candidates))
|
|
|
|
refined = []
|
|
for info in chosen:
|
|
better = _refine(gray, info["center"], info["half"]) if factor < 1.0 else None
|
|
refined.append(better or info)
|
|
return orient_points([r["center"] for r in refined],
|
|
[r["dots"] for r in refined],
|
|
[r["area"] for r in refined])
|
|
|
|
|
|
def orient_points(points, dots, areas=None):
|
|
"""Range les centres en HG, HD, BD, BG et fixe l'orientation par le repere origine."""
|
|
if areas is None:
|
|
areas = [0.0] * len(points)
|
|
items = list(zip(points, dots, areas))
|
|
angle_order = core.order_corners(points)
|
|
items.sort(key=lambda item: angle_order.index(tuple(map(float, item[0]))))
|
|
origins = [k for k, item in enumerate(items) if item[1] == 2]
|
|
oriented = len(origins) == 1
|
|
if oriented:
|
|
items = core.rotate_to_origin(items, origins[0])
|
|
return {"points": [item[0] for item in items],
|
|
"dots": [item[1] for item in items],
|
|
"areas": [item[2] for item in items],
|
|
"oriented": oriented}
|
|
|
|
|
|
def dots_near(gray, points):
|
|
"""Nombre de pastilles du repere autour de chaque point (placement manuel).
|
|
|
|
La taille de recherche vient de l'ecart entre points ; renvoie None pour
|
|
un point ou aucun repere n'est reconnu.
|
|
"""
|
|
pts = np.asarray(points, dtype=float)
|
|
spacing = min(math.hypot(*(pts[i] - pts[j]))
|
|
for i in range(len(pts)) for j in range(i + 1, len(pts)))
|
|
half = max(10.0, spacing / 8.0)
|
|
result = []
|
|
for point in points:
|
|
info = _refine(gray, point, half)
|
|
result.append(info["dots"] if info else None)
|
|
return result
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Redressement
|
|
# --------------------------------------------------------------------------
|
|
|
|
def output_size(width_mm, height_mm, resolution):
|
|
"""Taille en pixels de l'image redressee (au moins 1 x 1)."""
|
|
return (max(1, int(round(width_mm * resolution))),
|
|
max(1, int(round(height_mm * resolution))))
|
|
|
|
|
|
def rectify(image, image_to_mm, width_mm, height_mm, resolution, fill=(255, 255, 255),
|
|
offset=(0.0, 0.0)):
|
|
"""Photo redressee couvrant exactement le rectangle `width_mm` x `height_mm`
|
|
dont le coin haut gauche est en `offset` (mm, repere SVG ; zone de travail
|
|
par defaut).
|
|
|
|
`image_to_mm` : homographie coordonnees image -> millimetres (repere SVG).
|
|
Le pixel (i, j) du resultat couvre offset + [j, j+1] x [i, i+1] / resolution mm.
|
|
Renvoie (image, masque des pixels couverts par la photo).
|
|
"""
|
|
out_w, out_h = output_size(width_mm, height_mm, resolution)
|
|
# Pillow attend l'application inverse : sortie (pixels) -> entree (pixels).
|
|
to_mm = np.array([[width_mm / out_w, 0.0, offset[0]],
|
|
[0.0, height_mm / out_h, offset[1]],
|
|
[0.0, 0.0, 1.0]])
|
|
inverse = np.linalg.inv(image_to_mm) @ to_mm
|
|
inverse = inverse / inverse[2, 2]
|
|
coeffs = tuple(inverse.ravel()[:8].tolist())
|
|
result = image.transform((out_w, out_h), Image.PERSPECTIVE, coeffs,
|
|
Image.BICUBIC, fillcolor=fill)
|
|
coverage = Image.new("L", image.size, 255).transform(
|
|
(out_w, out_h), Image.PERSPECTIVE, coeffs, Image.NEAREST, fillcolor=0)
|
|
return result, np.asarray(coverage) > 0
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Vectorisation de la planche
|
|
# --------------------------------------------------------------------------
|
|
|
|
def _morph(mask, size, operation):
|
|
"""Ouverture ('open') ou fermeture ('close') par un carre de cote `size` px."""
|
|
if size < 3:
|
|
return mask
|
|
image = Image.fromarray(mask.astype(np.uint8) * 255)
|
|
first, second = ((ImageFilter.MinFilter, ImageFilter.MaxFilter) if operation == "open"
|
|
else (ImageFilter.MaxFilter, ImageFilter.MinFilter))
|
|
image = image.filter(first(size)).filter(second(size))
|
|
return np.asarray(image) > 127
|
|
|
|
|
|
def extract_board(gray, valid, resolution, lighter=True, threshold=0,
|
|
smoothing=0.5, min_hole_area=4.0, simplify=0.2, exclude=()):
|
|
"""Contour et trous de la planche sur une image redressee (grille en mm).
|
|
|
|
`valid` : pixels couverts par la photo ; `exclude` : rectangles
|
|
(x0, y0, x1, y1) en mm ignores (zones des reperes). Renvoie un
|
|
dictionnaire : `rings` (anneau exterieur puis trous, en mm), `holes`
|
|
(nombre), `threshold` (seuil utilise), `area` (mm2) ; None si aucune
|
|
planche n'est trouvee.
|
|
"""
|
|
gray = np.asarray(gray, dtype=np.uint8)
|
|
valid = np.asarray(valid, dtype=bool).copy()
|
|
h, w = gray.shape
|
|
for x0, y0, x1, y1 in exclude:
|
|
c0, c1 = max(0, int(math.floor(x0 * resolution))), min(w, int(math.ceil(x1 * resolution)))
|
|
r0, r1 = max(0, int(math.floor(y0 * resolution))), min(h, int(math.ceil(y1 * resolution)))
|
|
valid[r0:r1, c0:c1] = False
|
|
if not valid.any():
|
|
return None
|
|
values = gray[valid]
|
|
level = int(threshold) if threshold and threshold > 0 else otsu_threshold(values)
|
|
# Sans contraste entre les deux classes, pas de planche discernable (photo
|
|
# uniforme, ou seuil manuel hors de la plage des gris).
|
|
low, high = values[values <= level], values[values > level]
|
|
if not len(low) or not len(high) or float(high.mean()) - float(low.mean()) < MIN_CONTRAST:
|
|
return None
|
|
board = (gray > level) if lighter else (gray <= level)
|
|
board &= valid
|
|
size = 2 * int(round(smoothing * resolution)) + 1
|
|
board = _morph(_morph(board, size, "open"), size, "close")
|
|
|
|
labels, count, areas = label_components(board, 4)
|
|
if not count:
|
|
return None
|
|
main = int(np.argmax(areas))
|
|
pixel_area = 1.0 / (resolution * resolution)
|
|
if areas[main] * pixel_area < 100.0: # moins de 1 cm2 : pas de planche
|
|
return None
|
|
rr, cc = np.nonzero(labels == main)
|
|
r0, r1 = rr.min(), rr.max() + 1
|
|
c0, c1 = cc.min(), cc.max() + 1
|
|
# Marge d'un pixel pour que le fond exterieur touche toujours le bord du cadre.
|
|
comp = np.pad(labels[r0:r1, c0:c1] == main, 1)
|
|
outer_mask, kept = fill_holes(comp, 8, min_area=min_hole_area / pixel_area)
|
|
|
|
def to_mm(ring):
|
|
return [((x + c0 - 1) / resolution, (y + r0 - 1) / resolution) for x, y in ring]
|
|
|
|
def main_ring(mask):
|
|
rings = core.mask_to_rings(mask)
|
|
ring = max(rings, key=lambda r: abs(core.ring_area(r)))
|
|
return core.simplify_ring(to_mm(ring), simplify)
|
|
|
|
rings = [main_ring(outer_mask)] + [main_ring(hole) for hole in kept]
|
|
area = abs(core.ring_area(rings[0])) - sum(abs(core.ring_area(r)) for r in rings[1:])
|
|
return {"rings": rings, "holes": len(kept), "threshold": level, "area": area}
|