简体   繁体   中英

OpenCV HoughLine only detect one line in image

I am following the docs/ tutorial of openCV to detect lines in image. However, I only got one out of the four similar lines in the image. Here is the result

And here is my code:

Mat im = Imgcodecs.imread("C:/Users/valer/eclipse-workspace/thesis-application/StartHere/resource/4 lines.JPG");

    Mat gray = new Mat(im.rows(), im.cols(), CvType.CV_8SC1);

    Imgproc.cvtColor(im, gray, Imgproc.COLOR_RGB2GRAY);

    Imgproc.Canny(gray, gray, 50, 150);

    Mat lines = new Mat();

    Imgproc.HoughLines(gray, lines, 1, Math.PI/180, 200);

    for (int i = 0; i < lines.cols(); i++){
        double data[] = lines.get(0, i);
        double rho = data[0];
        double theta = data[1];
        double cosTheta = Math.cos(theta);
        double sinTheta = Math.sin(theta);
        double x0 = cosTheta * rho;
        double y0 = sinTheta * rho;
        Point pt1 = new Point(x0 + 10000 * (-sinTheta), y0 + 10000 * cosTheta);
        Point pt2 = new Point(x0 - 10000 * (-sinTheta), y0 - 10000 * cosTheta);
        Imgproc.line(im, pt1, pt2, new Scalar(0, 0, 200), 3);
    }
    Imgcodecs.imwrite("C:/Users/valer/eclipse-workspace/thesis-application/StartHere/resource/process/line_output.jpg", im);

I have tried playing around with the parameters for threshold, but I kept getting same (and sometimes worst) results. Would anyone please point out where am I doing wrong?

In the lines matrix result, lines are stored by row, not by column. So lines.rows() gives you line count and you can iterate with lines.get(i, 0) to fetch each line.

It seems that your code is true but maybe you can't use cv::line in proper way, for instance you can use below code for this purpose

Mat img=imread("/home/saeed/Desktop/zracV.jpg"),img2;
cvtColor(img,img,CV_BGR2GRAY);
Canny(img,img2,50,150,3,true);
vector<Vec2f> lines;
HoughLines(img2,lines,1,CV_PI/180,200);
cvtColor(img2,img2,CV_GRAY2BGR);
vector<Vec2f>::const_iterator it = lines.begin();
while(it!=lines.end())
{
    float rho = (*it)[0];
    float theta = (*it)[1];
    if (theta < CV_PI/4.
            || theta > 3.*CV_PI/4.) { // ~vertical line
        // point of intersection of the line with first row
        cv::Point pt1(rho/cos(theta),0);
        // point of intersection of the line with last row
        cv::Point pt2((rho-img2.rows*sin(theta))/
                      cos(theta),img2.rows);
        // draw a white line
        cv::line( img2, pt1, pt2, cv::Scalar(255), 3);
    } else { // ~horizontal line
        // point of intersection of the
        // line with first column
        cv::Point pt1(0,rho/sin(theta));
        // point of intersection of the line with last column
        cv::Point pt2(img2.cols,
                      (rho-img2.cols*cos(theta))/sin(theta));
        // draw a white line
        cv::line(img2, pt1, pt2, cv::Scalar(255),3);
    }
    ++it;
}
imshow("i",img2);
waitKey(0);

output of above code

在此处输入图片说明

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