OpenCV
Используя OpenCV findContours
и drawContours
, можно сначала векторизовать линии, а затем точно воссоздать исходное изображение:
import numpy as np
import cv2
img = cv2.imread('loadtest.png', 0)
result_fill = np.ones(img.shape, np.uint8) * 255
result_borders = np.zeros(img.shape, np.uint8)
# the '[:-1]' is used to skip the contour at the outer border of the image
contours = cv2.findContours(img, cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)[0][:-1]
# fill spaces between contours by setting thickness to -1
cv2.drawContours(result_fill, contours, -1, 0, -1)
cv2.drawContours(result_borders, contours, -1, 255, 1)
# xor the filled result and the borders to recreate the original image
result = result_fill ^ result_borders
# prints True: the result is now exactly the same as the original
print(np.array_equal(result, img))
cv2.imwrite('contours.png', result)
Результат
Scikit-Image
Использование scikit-изображения find_contours
и approximate_polygon
позволяет уменьшить число линий путем аппроксимации полигонов (на основе этот пример ):
import numpy as np
from skimage.measure import approximate_polygon, find_contours
import cv2
img = cv2.imread('loadtest.png', 0)
contours = find_contours(img, 0)
result_contour = np.zeros(img.shape + (3, ), np.uint8)
result_polygon1 = np.zeros(img.shape + (3, ), np.uint8)
result_polygon2 = np.zeros(img.shape + (3, ), np.uint8)
for contour in contours:
print('Contour shape:', contour.shape)
# reduce the number of lines by approximating polygons
polygon1 = approximate_polygon(contour, tolerance=2.5)
print('Polygon 1 shape:', polygon1.shape)
# increase tolerance to further reduce number of lines
polygon2 = approximate_polygon(contour, tolerance=15)
print('Polygon 2 shape:', polygon2.shape)
contour = contour.astype(np.int).tolist()
polygon1 = polygon1.astype(np.int).tolist()
polygon2 = polygon2.astype(np.int).tolist()
# draw contour lines
for idx, coords in enumerate(contour[:-1]):
y1, x1, y2, x2 = coords + contour[idx + 1]
result_contour = cv2.line(result_contour, (x1, y1), (x2, y2),
(0, 255, 0), 1)
# draw polygon 1 lines
for idx, coords in enumerate(polygon1[:-1]):
y1, x1, y2, x2 = coords + polygon1[idx + 1]
result_polygon1 = cv2.line(result_polygon1, (x1, y1), (x2, y2),
(0, 255, 0), 1)
# draw polygon 2 lines
for idx, coords in enumerate(polygon2[:-1]):
y1, x1, y2, x2 = coords + polygon2[idx + 1]
result_polygon2 = cv2.line(result_polygon2, (x1, y1), (x2, y2),
(0, 255, 0), 1)
cv2.imwrite('contour_lines.png', result_contour)
cv2.imwrite('polygon1_lines.png', result_polygon1)
cv2.imwrite('polygon2_lines.png', result_polygon2)
Результаты
Выход Python:
Contour shape: (849, 2)
Polygon 1 shape: (28, 2)
Polygon 2 shape: (9, 2)
Contour shape: (825, 2)
Polygon 1 shape: (31, 2)
Polygon 2 shape: (9, 2)
Contour shape: (1457, 2)
Polygon 1 shape: (9, 2)
Polygon 2 shape: (8, 2)
Contour shape: (879, 2)
Polygon 1 shape: (5, 2)
Polygon 2 shape: (5, 2)
Contour shape: (973, 2)
Polygon 1 shape: (5, 2)
Polygon 2 shape: (5, 2)
Contour shape: (224, 2)
Polygon 1 shape: (4, 2)
Polygon 2 shape: (4, 2)
Contour shape: (825, 2)
Polygon 1 shape: (13, 2)
Polygon 2 shape: (13, 2)
Contour shape: (781, 2)
Polygon 1 shape: (13, 2)
Polygon 2 shape: (13, 2)
contour_lines.png:
polygon1_lines.png:
polygon2_lines.png:
Затем можно рассчитать длину линий, применив теорему Пифагора к координатам: line_length = math.sqrt(abs(x2 - x1)**2 + abs(y2 - y1)**2)
. Если вы хотите получить ширину линий в виде числовых значений, взгляните на ответы «Как определить ширину линий?» для некоторых предлагаемых подходов.