gpt4 book ai didi

python - 在图像中仅显示 45 度线

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:16 27 4
gpt4 key购买 nike

我想检测图像中仅与原点成 45 度角的线条。我必须只用 3x3 卷积来做。我已经解决了它,所有 45 度角的线都被删除,其他一切都保留下来(与我想要的相反)。从这里达到我的最终目标的任何帮助将不胜感激,谢谢。

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


img = cv2.imread('Lines.png')

plt.imshow(img, cmap='gray')
plt.show()

kernel = np.array([[0, -1, 0],
[1, 0, 1],
[0, -1, 0]])

dst = cv2.filter2D(img, -1, kernel)
cv2.imwrite("filtered.png", dst)

这是图像卷积之前:

enter image description here

这是卷积后的图像:

This is what is happening right now

最佳答案

好吧,根据您在问题中提供的代码,我们获得了除我们想要获得的行之外的行。所以我们可以利用它并扩张它来填充线条。

img = cv2.imread('lines.png')
kernel = np.array([[0, -1, 0],
[1, 0, 1],
[0, -1, 0]])

dst = cv2.filter2D(img, -1, kernel)
kernel = np.ones((5, 5), np.uint8)
dilated = cv2.dilate(dst, kernel, iterations = 1)

dilated lines

然后我们需要移除线上方 45 度角的点,因此我们为此使用形态学开运算并对图像设置阈值以将所有线转换为像素值 = 255。

kernel = np.ones((7, 7), np.uint8)
opening = cv2.morphologyEx(dilated, cv2.MORPH_OPEN, kernel)
_,thresh = cv2.threshold(opening,10,255,cv2.THRESH_BINARY)

filled and thresholded

然后使用原始图像的 cv2.bitwise_and 和获得的阈值的 cv2.bitwise_not 我们得到我们的线条。

res = cv2.bitwise_and(img, cv2.bitwise_not(thresh))

intermediate result

我们获得了线条,但我们需要移除中间的圆圈。为此,我们在原始图像上使用 cv2.erode 仅获取中间圆,对其进行阈值处理,然后再次使用 cv2.bitwise_andcv2.bitwise_not 将其从 res 中删除。

kernel = np.ones((7, 7), np.uint8)
other = cv2.erode(img, kernel, iterations = 1)
_,thresh = cv2.threshold(other,10,255,cv2.THRESH_BINARY)
result = cv2.bitwise_and(res, cv2.bitwise_not(thresh))
cv2.imshow("Image", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Final result

关于python - 在图像中仅显示 45 度线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54723152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com