0

I am trying to detect lines in an Image using OpenCV houghLine algorithm but the issue is the only tutorial I found was in C++, I managed to convert some code to Java but some lines are just too confusing, below is the original code I am converting to Java

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

vector<Vec2f> lines;
// detect lines
HoughLines(dst, lines, 1, CV_PI/180, 150, 0, 0 );

// draw lines
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);
}

And this is what I have done so far

       Mat dst, cdst;
    Imgproc.Canny(src, dst, 50, 200); 
  //  Imgproc.Canny(src, dst, 50, 200, 3); 
    Imgproc.cvtColor(dst, cdst, Imgproc.COLOR_GRAY2BGRA); 

    Vector<Vec2f> lines;
    // detect lines
    Imgproc.HoughLines(dst, lines, 1, Math.PI/180, 150, 0, 0 );

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

My issues start at line 3 Imgproc.Canny(src, dst, 50, 200, 3), Java version of Canny I found with 5 parameters had a boolean as its 5th parameter, Vector<Vec2f> lines I could not locate the Vec2f class in Java Version and Imgproc.line( cdst, pt1, pt2, new Scalar(0,0,255), 3, Core.LINE_AA), there is not method in Imgproc of a line that has 6 parameters, the closest I found was 5 and 7

Chrome Lanta
  • 67
  • 1
  • 11

0 Answers0