Opencv:在盒子中找到盒子

2023-12-10

后勤

opencv 2.4 和 Python 2.7

The image I'm working with: enter image description here

问题

我感兴趣的是隔离围绕 9 条垂直线和水平线形成盒子的轮廓。我只是不确定该怎么做。我看过各种教程,例如关于数独谜题的教程,这些教程只是假设最大的盒子就是您正在寻找的盒子(因为数独谜题没有盒子中的盒子,减去实际的网格) 。我尝试使用 findContour 函数并按大小过滤轮廓,但没有成功。我最终得到的结果不一致,有时找到正确的轮廓,但有时却发现完全错误的轮廓。有人能指出我正确的方向吗?谢谢。

Original Image: enter image description here


受到@dervish 回答的启发,我有了一些想法。

  1. 使用 cv::HoughLines() 获取轴方向。
  2. 估计透视变换矩阵(M) 来对齐图像 w.r.t.轴方向。
  3. 使用 cv::warpPerspective() 扭曲图像。
  4. 使用@dervish 的答案来获取候选网格线。
  5. 按颜色信息(蓝色和白色棋盘)和线距离过滤线候选。
  6. 对最终网格进行逆变换M获取原始图像空间中的网格。

或者直接在步骤2中找到最终的网格位置,得到第N条最长的线。并按步骤4过滤结果。

HoughLine results

蟒蛇代码:

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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Opencv:在盒子中找到盒子 的相关文章