Ask Your Question

How to make HoughlinesP to detect more lines?

asked 2015-05-18 05:04:24 -0600

diegomez_86 gravatar image

updated 2020-12-09 08:47:39 -0600

Hello everyone, I am trying to detect the straight lines in a bottle. I am using the example provided by OpenCV tutorials however the maximum I can get is just one.

I have played with most of the parameters of CannyEdge and Houghlinesp, however the maximum I can get is always maximum 1 (I got 2 but just in one example) I am sending the examples of bottles that I am trying also with it's resulting example.

I have several suspects about the problems with this non-detection: one could be the shadow, other one could be the light reflect, and even I am thinking that the camera is not well calibrated. However if someone knows or has worked with this function before and thinks that this doesn't have any relation, I would thank you too much.

Thanks a lot for everything.

image description!

The original images

image description image description image description image description

edit retag flag offensive close merge delete


can you add some code and upload the original images without the title bar. Plus from what I am seeing you get the image from canny and pass it to houghTransfomp without any post-processing (e.g. some morphology operation that could strength the edges/lines and give you a more robust result ;-) Moreover, what is your main purpose by detecting the lines (e.g. obtain orientation of bottle, just get the bottle lines, playing around, etc)?

theodore gravatar imagetheodore ( 2015-05-18 07:13:33 -0600 )edit

Actually I am using CannyEdge + HoughLinesP

My idea is to find the orientation of the bottle. I tried with other options, actually there is an option to find the orientation of the bottle (Object orientation which appears in, however the shadow and reflect of the bottle affects the real orientation of it (So I would need to find an algorithm to detect them)

I will add the photos in my main question.

diegomez_86 gravatar imagediegomez_86 ( 2015-05-18 08:21:56 -0600 )edit

This is the code

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
 const char* filename = "/home/diego/Documents/frame0.jpg";

 Mat src = imread(filename, 0);

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

  vector<Vec4i> lines;
  HoughLinesP(dst, lines, 1, CV_PI/180, 2, 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);

  imshow("original image", src);
  imshow("detected lines", cdst);


 return 0;
diegomez_86 gravatar imagediegomez_86 ( 2015-05-18 08:26:29 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-05-18 16:06:46 -0600

theodore gravatar image

@diegomez_86 as I commented you were not applying some pre-, post-processing which would make your life easier. For example applying the following code:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
    // load image
    Mat src = imread("d.jpg");

    // check that it is loaded correctly
    if(! || src.empty())
        cerr << "Problem loading image!!!" << endl;
        return EXIT_FAILURE;

    imshow("src", src);

    // transform to grayscale
    Mat gray;
    cvtColor(src, gray, CV_BGR2GRAY);

//    imshow("gray", gray);

    // get the binary version
    Mat bin;
    threshold(gray, bin, 50, 255, CV_THRESH_BINARY/* | CV_THRESH_OTSU*/);
    imshow("bin", bin);

    Mat kernel = Mat::ones(3, 3, CV_8UC1);

    dilate(~bin, bin, kernel);

    imshow("dilate", bin);

    return 0;

I managed to get the following results:

image description image description

image description image description

which would make easy to apply the code from the example that you are suggesting and you can also find it here with some more goodies. Which comes up with the following result:

image description image description

image description image description

I guess also it would help to get better lines from houghTransformp (I did not try that). Also I do not know the parameters of your environment (e.g. illumination, shadows, camera angle and perspective, etc...) and your setup, so I cannot say how dynamic or not this solution would perform. I think this is something that you should discover.

edit flag offensive delete link more


You are awesome man. Thanks for the pre and post-processing ideas... I already used them on my program. Actually right now I'm just wanting to give an initial draft of the whole program (A Nao Robot playing spin the bottle game), later I will make more improvements and I will ask more from here.

diegomez_86 gravatar imagediegomez_86 ( 2015-05-20 04:23:51 -0600 )edit

Question Tools



Asked: 2015-05-18 05:04:24 -0600

Seen: 837 times

Last updated: May 18 '15