0

我正在尝试使用 houghlines 变换。该函数运行良好,但程序在 free_base 函数的 free.c 文件中某处的 imshow("detected lines",cdst) 后中断。

请帮帮我

这是我的代码:

#include<opencv\cv.h>
#include<opencv/cxcore.h>
#include<opencv/highgui.h>
#include<opencv2\imgproc\imgproc.hpp>
#include<iostream>
#include<vector>
#include<math.h>
#include <string>

using namespace cv;
using namespace std;

void help()
{
 cout << "\nThis program demonstrates line finding with the Hough transform.\n"
         "Usage:\n"
         "./houghlines <image_name>, Default is pic1.jpg\n" << endl;
}

int houghline(String filename)
{



 //const char* filename = argc >= 2 ? argv[1] : "Release\\D.bmp.bmp";


 Mat src = imread(filename, 0);
 if(src.empty())
 {
     help();
     cout << "can not open " << filename << endl;
     return -1;
 }

 Mat dst, cdst;
 Canny(src, dst, 50, 200, 3);
 cvtColor(dst, cdst, CV_GRAY2BGR);

/* 
 vector<Vec2f> lines;
  HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 );
  float aaa=lines[1][0];

  for( size_t i = 0; i < lines.size(); i++ )
  {
     float rho = lines[i][0], theta = lines[i][1];
     Point 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));
     line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
  }

 */



vector<Vec4i> lines;
  HoughLinesP(dst, lines, 1, CV_PI/180, 10, 25, 2 );
  /*
with the arguments:

    dst: Output of the edge detector. It should be a grayscale image (although in fact it is a binary one)
    lines: A vector that will store the parameters (x_{start}, y_{start}, x_{end}, y_{end}) of the detected lines
    rho : The resolution of the parameter r in pixels. We use 1 pixel.
    theta: The resolution of the parameter \theta in radians. We use 1 degree (CV_PI/180)
    threshold: The minimum number of intersections to “detect” a line
    minLinLength: The minimum number of points that can form a line. Lines with less than this number of points are disregarded.
    maxLineGap: The maximum gap between two points to be considered in the same line
*/

  for( size_t i = 0; i < lines.size(); i++ )
  {
    Vec4i l = lines[i];
    line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,0,0), 1, CV_AA);
    imshow("detected lines", cdst);
    waitKey();
  }

 imshow("source", src);
 imshow("detected lines", cdst);

}

int main(int argc, char** argv)
{
    //for(char alpha='A';alpha<='Z';alpha++)
        //String filename="C:\\Users\\Abhinav\\Documents\\Visual Studio 2012\\Projects\\Classes\\Release\\Microsoft Sans Serif\\"+to_string(alpha)+".bmp";
    try{houghline("C:\\Users\\Abhinav\\Documents\\Visual Studio 2012\\Projects\\Classes\\Release\\Microsoft Sans Serif\\A.bmp");}
    //houghline("C:\\Users\\Abhinav\\Documents\\Visual Studio 2012\\Projects\\Classes\\Release\\Microsoft Sans Serif\\B.bmp");
    catch(Exception e){cout<<e.err;}
 waitKey();


 return 0;
 }
4

1 回答 1

0

您的函数“int houghline(String filename)”似乎没有返回语句。

请加上“return 0;” 在函数的末尾,看看它是否有效。

于 2013-10-01T13:08:34.143 回答