visual-c++ - 如何比较 OpenCV 中的霍夫线位置?

标签 visual-c++ image-processing opencv

使用 VC++ 和 Open CV。这是我正在尝试做的事情: 找到前三个几乎水平的 hough 线并绘制它们。 找到所有几乎垂直的线并绘制它们 如果任何垂直线高于水平线,则 FLAG 设置为 0 如果水平线上方(全部在下方)没有垂直霍夫线,则 FLAG=1

    int n, i,c=0;
    int flag = 0;

    cvCanny( src, dst, 50, 150, 3 );
    lines = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 10, 5, 5 );
    n = lines->total;

    for( i = 0; i < n; i++ )
    {
        CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
        CvPoint pt1, pt2, hpt1, hpt2, vpt1, vpt2;
        int hy = 0, vy = 0;         
        pt1 = line[0];
        pt2 = line[1];   
        theta = atan( (double)(pt2.y - pt1.y)/(pt2.x - pt1.x) ); /*slope of line*/
        degree = theta*180/CV_PI;  
        if( fabs(degree) < 8) //checking for near horizontal line
        {
            c++;
            if( c > 0 && c <5)  /*main horizontal lines come first*/
            {
                cvLine( out, pt1, pt2, CV_RGB(255, 255,255), 1, CV_AA, 0 );
                hpt1 = line[0];
                hpt2 = line[1];

                if( hpt1.y > hpt2.y ) //finds out lower end-point
                    hy = hpt1.y;
                else
                    hy = hpt2.y;
            }
        }

        if( fabs(degree) > 70 ) /*near vertical lines*/
        {
            cvLine( out, pt1, pt2, CV_RGB(255, 255,255), 1, CV_AA, 0 );

            vpt1 = line[0];
            vpt2 = line[1];

            if( vpt1.y > vpt2.y ) //finds upper end-pt of vertical line
                    vy = vpt1.y;
            else
                vy = vpt2.y;

            if( vy >= hy ) //if vert line is lower than horizontal line
                flag = 1;
            else 
                flag = 0;

        }

    }
    display( out, "hough lines" );
    return flag;
}

但是对于图像,即使在水平线上方检测到垂直线 - 标志仍然返回 1。那么我是否错误地沿轴计数?请帮帮我。

最佳答案

if( fabs(degree) > 70 )if( fabs(degree) < 8 )线条看起来不对。大约 180 度的角度意味着几乎水平……您可能想要更改它,并记住角度的周期性(因此大约 360 度也几乎是水平的)。一种很好地处理这种情况的方法是使用 if (fabs(cos(angle - desired_angle)) > 0.996) ,这大致意味着“如果 angle 和 desired_angle 彼此相差在 5 度以内,则忽略方向”。 0.996大致是 5 度的余弦,如果您需要更精确的数字,请在此处输入更多数字 - 0.9961946980917455是更接近的匹配。

此外,您的循环顺序已关闭。你不find the first three nearly-horizontal hough lines and draw them. find all the nearly-vertical lines and draw them if any vertical line is above the horizontal line在这个序列中,您以任何顺序遍历所有行,并独立处理它们 - 垂直行可能先于水平行,因此您不知道要检查什么。

第三,

            if( hpt1.y > hpt2.y ) //finds out lower end-point
                hy = hpt1.y;
            else
                hy = hpt2.y;

对比

        if( vpt1.y > vpt2.y ) //finds upper end-pt of vertical line
                vy = vpt1.y;
        else
            vy = vpt2.y;

查找较低坐标的代码与查找较高坐标的代码相同。你认为这可行吗?

第四,

        if( vy >= hy ) //if vert line is lower than horizontal line
            flag = 1;
        else 
            flag = 0;

flag 的值取决于最后一次通过这段代码。这与 any 不匹配在你的描述中。

关于visual-c++ - 如何比较 OpenCV 中的霍夫线位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4839010/

相关文章:

c++ - Microsoft Visual C++ 中的共享指针调试

c++ - 如何正确地将 cv::Mat 转换为 CV_8UC1?

python - 如何在Python中获取图像的对象轮廓(外边界)?

python - TypeError:labels不是numpy数组,也不是标量

c++ - opencv中基于卡尔曼滤波器的鼠标跟踪

c++ - 安全的虚拟决赛

c++ - 无法将字符数组转换为具有 utf-8 字符的 wstring

c++ - 如何将 OpenCV 窗口设置为点击窗口?

c++ - Boost::Lambda 方法推导错误 MSVC9.0

python - 在 macOS Sierra 上使用 Homebrew 安装卡住加载 scipy 时出错