受到@dervish 回答的启发,我有了一些想法。
- 使用 cv::HoughLines() 获取轴方向。
- 估计透视变换矩阵(
M
) 来对齐图像 w.r.t.轴方向。
- 使用 cv::warpPerspective() 扭曲图像。
- 使用@dervish 的答案来获取候选网格线。
- 按颜色信息(蓝色和白色棋盘)和线距离过滤线候选。
- 对最终网格进行逆变换
M
获取原始图像空间中的网格。
或者直接在步骤2中找到最终的网格位置,得到第N条最长的线。并按步骤4过滤结果。
蟒蛇代码:
import cv2
import numpy as np
def main():
im = cv2.imread('image.png')
#edge = cv2.imread('edge.png', 0)
edge = cv2.Canny(im, 100, 200, apertureSize=3)
lines = cv2.HoughLines(edge, 1, np.pi/180, 140)
for rho, theta in lines[0]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(im, (x1, y1), (x2, y2), (0, 0, 255), 2)
# TODO: filter the lines by color and line distance
cv2.imshow('image', im)
cv2.imshow('edge', edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()