简体   繁体   中英

Why different result of HoughLines of opencv in java and c++

I am new to Opencv and I am reading the book named OpenCV 3.0 Computer Vision with Java from Daniel Lélis Baggio. I test a image from opencv site that tries to detect lines but I got different result with only one line detection with Daniel Lélis Baggio java code. I don't know why this code doesn't work properly. The Code from the book:

else if(houghString.equals(operation)){
        Mat canny = new Mat();
        Imgproc.Canny(originalImage, canny,10 , 50, aperture, false);
        image = originalImage.clone();
        Mat lines = new Mat();

        Imgproc.HoughLines(canny, lines, 1, Math.PI/180, lowThreshold);

        for( int i = 0; i < lines.cols(); i++ )
        {
          double rho = lines.get(0, i)[0];
          double theta = lines.get(0, i)[1];
          Point pt1 = new Point(), pt2= new Point();
          double a = Math.cos(theta), b = Math.sin(theta);
          double x0 = a*rho, y0 = b*rho;
          pt1.x = Math.round(x0 + 1000*(-b));
          pt1.y = Math.round(y0 + 1000*(a));
          pt2.x = Math.round(x0 - 1000*(-b));
          pt2.y = Math.round(y0 - 1000*(a));
          Imgproc.line( image, pt1, pt2, new Scalar(255,0,0), 2, Core.LINE_AA,0);
        }   
    }
else if(pHoughString.equals(operation)){
        Mat canny = new Mat();
        Imgproc.Canny(originalImage, canny,10 , 50, aperture, false);
        //canny = originalImage.clone();
        image = originalImage.clone();

        //Imgproc.cvtColor(image, image, Imgproc.COLOR_GRAY2BGR);

    Mat lines = new Mat();

    Imgproc.HoughLinesP(canny, lines, 1, 360,  lowThreshold, 50, 5 );
    for( int i = 0; i < lines.cols(); i++ )
    {
        double a = lines.get(0, i)[0];
        double b = lines.get(0, i)[1];
        double c = lines.get(0, i)[2];
        double d = lines.get(0, i)[3];
        Imgproc.line( image, new Point(a, b), new Point(c, d), new Scalar(0,0,255), 1, Core.LINE_AA,0);

    }

And the code from opencv site for c++ is below:

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

 #if 0
  vector<Vec2f> lines;
  HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 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);
  }
 #else
  vector<Vec4i> lines;
  HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );
  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(0,0,255), 3, CV_AA);
  }
 #endif

The result from Java code:

与DanielLélisBaggiojava代码的结果

and the result from opencv site:

Opencv HoughLines

Why are different results from an image with java and c++?

It's late but I just hit the same szenario and it may help some other...

lines are returned in rows not columns, so correct loop should be:

for( int i = 0; i < lines.rows(); i++ ) {
      double rho = lines.get(i, 0)[0];
      double theta = lines.get(i, 0)[1];
      ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM