我有下面的箭图图像,看起来像这样:
如果我像这样取图像的一小部分:
并在图像的子区域中找到箭头的平均方向。
我尝试了以下方法,使用Hough行来解析图像:
import cv2
import numpy as np
from numpy import mean
import matplotlib.pyplot as plt
def get_hough_lines(img, name):
try:
inputImageGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(inputImageGray, 150, 200, apertureSize=3)
minLineLength = 30
maxLineGap = 1
dtype = [('x1', float), ('y1', float), ('x2', float), ('y2', float)]
lines = cv2.HoughLinesP(edges, cv2.HOUGH_PROBABILISTIC, np.pi / 180, 30, minLineLength, maxLineGap)
a = np.array(lines, dtype=dtype)
np.sort(a, order='x1')
x_s = []
y_s = []
x_flow = [i[0][2] - i[0][0] for i in lines]
y_flow = [i[0][3] - i[0][1] for i in lines]
plt.plot([i for i in range(len(x_flow))], x_flow, 'o', color='black')
plt.savefig("debug.png", bbox_inches='tight', pad_inches=0)
# [np.sqrt(np.square(i[2] - i[0]) + np.square(i[3] - i[1])) for i in a]
for x in range(0, len(lines)):
for x1, y1, x2, y2 in lines[x]:
# cv2.line(inputImage,(x1,y1),(x2,y2),(0,128,0),2, cv2.LINE_AA)
pts = np.array([[x1, y1], [x2, y2]], np.int32)
cv2.polylines(img, [pts], True, (0, 255, 0))
x_s.append(x2 - x1)
y_s.append(y2 - y1)
average_direction = [mean(x_s), mean(y_s)]
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.imwrite(f"TEST_{name}_hough_direction_right_{average_direction[0]}_up__{average_direction[1]}.jpg", img)
except TypeError as e:
print('could not parse hough lines')
except Exception as e:
print('could not parse hough lines')
但问题是,我不能很好地区分平均方向,所以对于以下两张图像:
所以从眼睛上看,箭头在不同的方向上流动,但我在代码中检测不到这一点。
我该如何解决这个问题并找到正确的平均方向呢?
发布于 2020-06-15 06:25:06
线没有特定的方向。它们无限延伸到两个方向。因此,您需要以某种方式检测箭头,以便获得特定方向。naife方法可以检查每一行的两端,并用箭头标记这一端。您可以通过简单地获取小区域的平均颜色来检测线端是否包含箭头。如果你找到了更好的解决方案,请与我分享。祝好运!
https://stackoverflow.com/questions/62344915
复制相似问题