.. _segmentdetectionrst: ==================================== Détection de segments dans une image ==================================== .. only:: html **Links:** :download:`notebook `, :downloadlink:`html `, :download:`PDF `, :download:`python `, :downloadlink:`slides `, :githublink:`GitHub|_doc/notebooks/image/segment_detection.ipynb|*` C’est une technique assez vieille et qui consiste à détecter des segments comme des anomalies : l’alignement de points est un événement assez rare dans un nuage de points mais rare comment ? Cette idée mène à la probabilisation d’une image pour quantifier ce qu’est un alignement de points nécessairement rare. .. code:: ipython3 from jyquickhelper import add_notebook_menu add_notebook_menu() .. contents:: :local: Une image aléatoire ------------------- On considère un bruit aléatoire uniforme dans une image et on ajoute des points aléatoires tirés le long d’une ligne selon une loi gaussienne : uniforme sur la ligne, gaussien autour du segment. .. code:: ipython3 from mlstatpy.image.detection_segment import random_noise_image, convert_array2PIL img = random_noise_image((100, 100)) convert_array2PIL(img, mode="binary") .. image:: segment_detection_3_0.png .. code:: ipython3 from mlstatpy.image.detection_segment import random_segment_image random_segment_image(img, density=3., lmin=0.3) .. parsed-literal:: {'size': 36, 'angle': 2.285619160431492, 'x1': 23.597410654261072, 'y1': 40, 'x2': 0, 'y2': 67.18753777770554, 'nbpoints': 108} .. code:: ipython3 convert_array2PIL(img, mode="binary") .. image:: segment_detection_5_0.png .. code:: ipython3 random_segment_image(img, density=5., lmin=0.3) random_segment_image(img, density=5., lmin=0.3) convert_array2PIL(img, mode="binary") .. image:: segment_detection_6_0.png .. code:: ipython3 pilimg = convert_array2PIL(img, mode="binary").convert('RGB') pilimg .. image:: segment_detection_7_0.png .. code:: ipython3 from PIL import ImageFilter pilimg = pilimg.filter(ImageFilter.BLUR).filter(ImageFilter.BLUR).filter(ImageFilter.BLUR).filter(ImageFilter.BLUR) pilimg .. image:: segment_detection_8_0.png .. code:: ipython3 from PIL import ImageEnhance enh = ImageEnhance.Sharpness(pilimg) final_img = enh.enhance(4) final_img .. image:: segment_detection_9_0.png Gradient -------- La détection des segments est basée sur le gradient. .. code:: ipython3 from mlstatpy.image.detection_segment import compute_gradient, plot_gradient grad = compute_gradient(final_img, color=0) .. code:: ipython3 plot_gradient(pilimg.copy(), grad, direction=-2) .. image:: segment_detection_12_0.png Détection de segments --------------------- .. code:: ipython3 from mlstatpy.image.detection_segment import detect_segments seg = detect_segments(final_img, verbose=1, seuil_nfa=1e-1) len(seg) .. parsed-literal:: n = 1000 ... 82 temps 0.27 sec nalign 298 n = 2000 ... 82 temps 0.52 sec nalign 671 n = 3000 ... 164 temps 0.83 sec nalign 964 n = 4000 ... 164 temps 1.10 sec nalign 1357 n = 5000 ... 249 temps 1.39 sec nalign 1544 n = 6000 ... 252 temps 1.66 sec nalign 1924 n = 7000 ... 374 temps 1.95 sec nalign 2183 n = 8000 ... 375 temps 2.23 sec nalign 2460 n = 9000 ... 379 temps 2.56 sec nalign 2728 .. parsed-literal:: 379 .. code:: ipython3 from mlstatpy.image.detection_segment import plot_segments plot_segments(final_img.copy(), seg) .. image:: segment_detection_15_0.png Détection de segments sur une image ----------------------------------- .. code:: ipython3 from PIL import Image egl = Image.open("eglise_zoom2.jpg") egl .. image:: segment_detection_17_0.png On réduit la taille de l’image. .. code:: ipython3 egl2 = egl.crop((0, 0, egl.size[0]//3, 3*egl.size[1]//4)) egl2 .. image:: segment_detection_19_0.png .. code:: ipython3 grad2 = compute_gradient(egl2, color=0) plot_gradient(egl2.copy(), grad2, direction=-2) .. image:: segment_detection_20_0.png .. code:: ipython3 seg2 = detect_segments(egl2) len(seg2) .. parsed-literal:: 490 .. code:: ipython3 from mlstatpy.image.detection_segment import plot_segments res = plot_segments(egl2.copy(), seg2) res .. image:: segment_detection_22_0.png Il faudrait fusionner les segments mais cela a l’air de marcher.