我有以下颤抖图,如下所示:
如果我像这样截取图像的一部分:
然后在图像的小节中找到箭头的平均方向。
我尝试了以下方法,使用霍夫线来解析图像:
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')
但是问题是我无法很好地区分平均方向,因此对于以下2张图像:
因此,从视觉上看,箭头朝着不同的方向流动,但是我无法在代码中检测到这一点。
我该如何解决这个问题并找到正确的平均方向?
答案 0 :(得分:1)
线没有特定的方向。它们无限地向两个方向延伸。因此,您需要以某种方式检测箭头以获取特定方向。 naife方法可以是检查每行的2端并用箭头标记该行。您可以通过简单地获取该小区域的平均颜色来检测线端是否包含箭头。如果您找到更好的解决方案,请与我分享。祝你好运!