Line detection example

This example shows how to use the line detector with Pylena.

import matplotlib.pyplot as plt
from skimage.data import text
import pylena as pln
import numpy as np

from typing import List

Then, we read the image.

img = text()

plt.imshow(img, cmap="gray")
plt.xticks([])
plt.yticks([])
plt.show()
example line detector

Then, we use the line detector.

img_label: np.ndarray
superpositions: List[pln.scribo.LSuperposition]
lines: List[pln.scribo.VSegment]

img_label, superpositions, lines = pln.scribo.line_detector(
    img,
    "full",
    verbose=True,
    min_len=100,
    blumi=110,
    llumi=110,
    discontinuity_relative=5,
)
min_len = 100
params = <scribo.SegDetParams: preprocess=0, tracker=0, traversal_mode=2, extraction_type=0, negate_image=0, dyn=0.600000, size_mask=11, double_exponential_alpha=0.600000, simple_moving_average_memory=30.000000, exponential_moving_average_memory=16.000000, one_euro_beta=0.007000, one_euro_mincutoff=1.000000, one_euro_dcutoff=1.000000, bucket_size=32, nb_values_to_keep=30, discontinuity_relative=5, discontinuity_absolute=0, minimum_for_fusion=15, default_sigma_position=2, default_sigma_thickness=2, default_sigma_luminosity=57, min_nb_values_sigma=10, sigma_pos_min=1.000000, sigma_thickness_min=0.640000, sigma_luminosity_min=13.000000, gradient_threshold=30, llumi=110, blumi=110, ratio_lum=1.000000, max_thickness=100, threshold_intersection=0.800000, remove_duplicates=1>

Finally, we display the results of the line detector.

plt.imshow(img, cmap="gray")
for line in lines:
    print(line)
    plt.plot([line.x0, line.x1], [line.y0, line.y1], color="red")

plt.xticks([])
plt.yticks([])
plt.show()
example line detector
2: (0, 86) -- (124, 154)
3: (5, 137) -- (191, 86)
4: (0, 12) -- (265, 132)
5: (310, 70) -- (427, 50)
6: (341, 31) -- (447, 67)
7: (110, 0) -- (446, 131)

We can also display the pixel information.

img_rgb = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
img_rgb[:, :, 0] = img
img_rgb[:, :, 1] = img
img_rgb[:, :, 2] = img

max_label = np.max(img_label)

np.random.seed(0)
color = np.random.randint(low=0, high=255, size=(max_label - 2, 3))

for i in range(2, max_label):
    img_rgb[img_label == i, 0] = color[i - 2, 0]
    img_rgb[img_label == i, 1] = color[i - 2, 1]
    img_rgb[img_label == i, 2] = color[i - 2, 2]

plt.imshow(img_rgb)

plt.xticks([])
plt.yticks([])
plt.show()
example line detector

We can also display the include the superposition information.

img_rgb_with_superposition = img_rgb.copy()

for superposition in superpositions:
    print(superposition)
    img_rgb_with_superposition[superposition.y, superposition.x, 0] = 255
    img_rgb_with_superposition[superposition.y, superposition.x, 1] = 0
    img_rgb_with_superposition[superposition.y, superposition.x, 2] = 0

plt.imshow(img_rgb_with_superposition)

plt.xticks([])
plt.yticks([])
plt.show()
example line detector
2: (56, 117)
3: (56, 117)
2: (56, 118)
3: (56, 118)
2: (56, 119)
3: (56, 119)
2: (56, 120)
3: (56, 120)
2: (56, 121)
3: (56, 121)
2: (56, 122)
3: (56, 122)
2: (57, 117)
3: (57, 117)
2: (57, 118)
3: (57, 118)
2: (57, 119)
3: (57, 119)
2: (57, 120)
3: (57, 120)
2: (57, 121)
3: (57, 121)
2: (57, 122)
3: (57, 122)
2: (58, 118)
3: (58, 118)
2: (58, 119)
3: (58, 119)
2: (58, 120)
3: (58, 120)
2: (58, 121)
3: (58, 121)
2: (59, 118)
3: (59, 118)
2: (59, 119)
3: (59, 119)
2: (59, 120)
3: (59, 120)
2: (59, 121)
3: (59, 121)
2: (60, 118)
3: (60, 118)
2: (60, 119)
3: (60, 119)
2: (60, 120)
3: (60, 120)
2: (60, 121)
3: (60, 121)
2: (61, 118)
3: (61, 118)
2: (61, 119)
3: (61, 119)
2: (61, 120)
3: (61, 120)
2: (61, 121)
3: (61, 121)
2: (62, 118)
3: (62, 118)
2: (62, 119)
3: (62, 119)
2: (62, 120)
3: (62, 120)
2: (62, 121)
3: (62, 121)
2: (62, 122)
3: (62, 122)
3: (165, 85)
4: (165, 85)
3: (165, 86)
4: (165, 86)
3: (165, 87)
4: (165, 87)
3: (166, 85)
4: (166, 85)
3: (166, 86)
4: (166, 86)
3: (166, 87)
4: (166, 87)
3: (166, 88)
4: (166, 88)
3: (167, 87)
4: (167, 87)
3: (167, 88)
4: (167, 88)
3: (168, 87)
4: (168, 87)
3: (168, 88)
4: (168, 88)
3: (169, 87)
4: (169, 87)
3: (169, 88)
4: (169, 88)
3: (170, 88)
4: (170, 88)
3: (170, 89)
4: (170, 89)
3: (171, 88)
4: (171, 88)
3: (171, 89)
4: (171, 89)
3: (172, 88)
4: (172, 88)
3: (172, 89)
4: (172, 89)
3: (172, 90)
4: (172, 90)
3: (173, 88)
4: (173, 88)
3: (173, 89)
4: (173, 89)
5: (399, 50)
6: (399, 50)
5: (399, 51)
6: (399, 51)
5: (399, 52)
6: (399, 52)
5: (399, 53)
6: (399, 53)
5: (400, 50)
6: (400, 50)
5: (400, 51)
6: (400, 51)
5: (400, 52)
6: (400, 52)
5: (400, 53)
6: (400, 53)
5: (401, 50)
6: (401, 50)
5: (401, 51)
6: (401, 51)
5: (401, 52)
6: (401, 52)
5: (401, 53)
6: (401, 53)
5: (402, 51)
6: (402, 51)
5: (402, 52)
6: (402, 52)
5: (402, 53)
6: (402, 53)
5: (403, 51)
6: (403, 51)
5: (403, 52)
6: (403, 52)
5: (403, 53)
6: (403, 53)
5: (404, 51)
6: (404, 51)
5: (404, 52)
6: (404, 52)
5: (404, 53)
6: (404, 53)
5: (405, 51)
6: (405, 51)
5: (405, 52)
6: (405, 52)
5: (405, 53)
6: (405, 53)
5: (406, 51)
6: (406, 51)
5: (406, 52)
6: (406, 52)
5: (406, 53)
6: (406, 53)
5: (407, 51)
6: (407, 51)
5: (407, 52)
6: (407, 52)
5: (407, 53)
6: (407, 53)
5: (408, 51)
6: (408, 51)
5: (408, 52)
6: (408, 52)
5: (408, 53)
6: (408, 53)
5: (409, 50)
6: (409, 50)
5: (409, 51)
6: (409, 51)
5: (409, 52)
6: (409, 52)
5: (398, 49)
6: (398, 49)
5: (398, 50)
6: (398, 50)
5: (398, 51)
6: (398, 51)

Total running time of the script: (0 minutes 0.211 seconds)

Gallery generated by Sphinx-Gallery