简体   繁体   English

使用 python 从图像中提取线条

[英]Extract lines from image with python

I need to extract path/lines from an image .我需要从图像中提取路径/线条。 I apply laplacian filter to this input.我对此输入应用拉普拉斯滤波器。 In laplacian filtered image , the lines to be extracted can be seen as low value pixels connected to form a linear object with high value pixels forming its border (defining the thickness of the linear path).拉普拉斯滤波图像中,要提取的线可以看作是连接形成线性 object 的低值像素,高值像素形成其边界(定义线性路径的粗细)。 The issue is there are many more pixels between these lines which also have similar values.问题是这些线之间有更多的像素,它们也具有相似的值。 Setting threshold to extract these lines does not work.设置阈值以提取这些行不起作用。 Applying filters like entropy or gabor filter also did not work.应用熵或 gabor 过滤器等过滤器也不起作用。 With HoughP or Hough Transformation nothing meaningful comes out, probably arguments are not set properly.使用 HoughP 或 Hough 变换没有任何意义,可能 arguments 设置不正确。 I need help extracting these lines/path from the image.我需要帮助从图像中提取这些线条/路径。

The code below uses cv2.HoughLinesP() on the thresholded image to produce this:下面的代码在阈值图像上使用cv2.HoughLinesP()来生成: 在此处输入图像描述

import cv2
import matplotlib.pyplot as plt
import numpy as np

# Threshold 
img = cv2.imread("subset_comp.tif")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, img_thr = cv2.threshold(img_gray, 150, 255, cv2.THRESH_BINARY)
fig, axs = plt.subplots(1, 2)
axs[0].set_title("Thresholded")
axs[0].imshow(img_thr, aspect="auto", cmap="gray")

# Find lines
lines = cv2.HoughLinesP(
    img_thr, rho=1, theta=np.pi / 180, threshold=128, minLineLength=600, maxLineGap=30,
)
lines = lines.squeeze()
axs[1].set_title("Grayscale with Lines")
axs[1].imshow(img_gray, aspect="auto", cmap="gray")
for x1, y1, x2, y2 in lines:
    axs[1].plot([x1, x2], [y1, y2], "r")
fig.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM