使用 python 从图像中提取线条

Extract lines from image with python

我需要从an image. I apply laplacian filter to this input. In laplacian filtered image中提取path/lines,要提取的线可以看作是低值像素连接形成一个线性对象,高值像素形成它的边界(定义粗细线性路径)。问题是这些线之间有更多像素,它们也具有相似的值。设置阈值以提取这些行不起作用。应用熵或 gabor 过滤器等过滤器也不起作用。使用 HoughP 或 Hough 变换没有任何有意义的结果,可能参数设置不正确。 我需要帮助从图像中提取这些 lines/path。

下面的代码在阈值图像上使用 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()