我在python中使用OpenCV检测到一个棋盘,使用:
- 计算图像的边缘
- 计算Hough变换
- 寻找Hough变换的局部极大值
- 提取图像线
然后我使用了findContours
和drawContours
个函数:
im_gray = cv2.imread('redLines.png', cv2.IMREAD_GRAYSCALE)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
morphed = cv2.dilate(im_gray, kernel, iterations=1)
(ret, thresh) = cv2.threshold(morphed, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(thresh, contours, -1, (255, 255, 255), 3)
而且效果很好,最后一场imshow看起来是这样的:
现在,我试图检测网格中的每个正方形,并将其点保存在向量中唯一的索引中.
我知道我可以用轮廓数组来做.但是,当我打印轮廓的长度时,它一直在快速变化,从2号到112号..
所以我猜它没有很好地识别网格.
任何帮助都将不胜感激.