How to solve error in converting hough to opencv and C ++

#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); } } 

I used this code to "convert hough" to opencv to detect an object in an image. and running the program without any errors. but as a result, only the console window appears and quickly disappears. what should i do for this.

+1
source share
1 answer

Here is some bad logic you got there:

  • 1st: if argc greater than or less than 2, your main code will not run, and you will not be notified.
  • 2nd: if for some reason cvLoadImage() fails, you will also not be notified.

I suspect that one of these two things is happening: either you are not calling your program with the right number of parameters, or cvLoadImage() not working (it is impossible to find a file or the file type is not supported).

I suggest you add the appropriate debugs (printf calls) to see what really happens.

EDIT:

A few notes:

  • If your image is downloaded as "qqqq.jpg" , and if you run your program from Visual Studio, you need to place the image in the same folder as the source code files (and not in the folder where your .exe is);
  • If you are using Windows and trying to load an image using the full path, be sure to exit the slashes: C:\\folder\\qqqq.jpg
  • FYI, argc == 2 implies that you start the application from the command line using the format: app.exe param1
+1
source

Source: https://habr.com/ru/post/976549/


All Articles