如何解决opencv和c ++中的hough变换中的错误 [英] How to solve error in hough transform in opencv and c++

查看:167
本文介绍了如何解决opencv和c ++中的hough变换中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <cv.h>
#include <highgui.h>
#include <math.h>

int main(int argc, char** argv)
{
    IplImage* src;
    if( argc == 2 && (src=cvLoadImage("qqqq.jpg", 0))!= 0)
    {
        IplImage* dst = cvCreateImage( cvGetSize(src), 8, 1 );
        IplImage* color_dst = cvCreateImage( cvGetSize(src), 8, 3 );
        CvMemStorage* storage = cvCreateMemStorage(0);
        CvSeq* lines = 0;
        int i;
        cvCanny( src, dst, 50, 200, 3 );
        cvCvtColor( dst, color_dst, CV_GRAY2BGR );
#if 1
        lines = cvHoughLines2( dst,
                               storage,
                               CV_HOUGH_STANDARD,
                               1,
                               CV_PI/180,
                               100,
                               0,
                               0 );

        for( i = 0; i < MIN(lines->total,100); i++ )
        {
            float* line = (float*)cvGetSeqElem(lines,i);
            float rho = line[0];
            float theta = line[1];
            CvPoint pt1, pt2;
            double a = cos(theta), b = sin(theta);
            double x0 = a*rho, y0 = b*rho;
            pt1.x = cvRound(x0 + 1000*(-b));
            pt1.y = cvRound(y0 + 1000*(a));
            pt2.x = cvRound(x0 - 1000*(-b));
            pt2.y = cvRound(y0 - 1000*(a));
            cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, 8 );
        }
#else
        lines = cvHoughLines2( dst,
                               storage,
                               CV_HOUGH_PROBABILISTIC,
                               1,
                               CV_PI/180,
                               80,
                               30,
                               10 );
        for( i = 0; i < lines->total; i++ )
        {
            CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
            cvLine( color_dst, line[0], line[1], CV_RGB(255,0,0), 3, 8 );
        }
#endif
        cvNamedWindow( "Source", 1 );
        cvShowImage( "Source", src );

        cvNamedWindow( "Hough", 1 );
        cvShowImage( "Hough", color_dst );

        cvWaitKey(0);
    }

}

我将此代码用于hough变换在opencv中检测图像中的对象。和程序运行没有任何错误。但结果是只有控制台窗口快速出现并快速消失。我该怎么办呢。

i used this code for "hough transform" in opencv to detect object in a image. and program run without any error. but the result is only a console window appear quickly and disappear quickly. what should i do for this.

推荐答案

你在那里遇到了一些错误的逻辑:

That's some bad logic you got over there:


  • 第一:如果 argc 大于或小于2,您的主代码将无法运行,也不会收到通知关于它。

  • 第二:如果因任何原因 cvLoadImage()失败,你也不会收到通知。

  • 1st: if argc is bigger or smaller than 2, your main code won't run, nor will you be notified about it.
  • 2nd: if for any reason cvLoadImage() fails, you will also not be notified about it.

我怀疑这两件事之一正在发生:要么你没有使用正确数量的参数调用你的程序,要么 cvLoadImage()失败(无法找到文件或不支持文件类型)。

I suspect one of these 2 things are happening: either you are not calling your program with the right number of parameters, or cvLoadImage() is failing (unable to find the file or the file type is not supported).

我建议你添加适当的调试( printf来看看真实情况。

I suggest you add appropriate debugs (printf calls) to see what is really going on.

编辑:

几个笔记:


  • 如果您的图片被加载为qqqq.jpg如果从Visual Studio中运行程序,则需要将图像放在t中与源代码文件相同的文件夹(而不是.exe所在的文件夹);

  • 如果您正在使用Windows并尝试使用完整路径加载图像,请不要忘记逃避斜杠: C:\\folder\\qqqq.jpg

  • FYI, argc == 2 表示您使用以下格式从命令行运行应用程序: app.exe param1

  • If your image is being loaded as "qqqq.jpg" and if you are running your program from within Visual Studio, you need to put the image inside the same folder as your source code files (and not in the folder where your .exe is);
  • If you are using Windows and trying to load the image using the full path, don't forget to escape the slashes: C:\\folder\\qqqq.jpg
  • FYI, argc == 2 implies that you are running your application from the command-line using the format: app.exe param1

这篇关于如何解决opencv和c ++中的hough变换中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆