4

I want to detect a quadrilateral shape in an image in Java. I can use the HoughLinesP method in OpenCV (using javaCV for opencv java binding) to detect line segments. But I can't figure out how to detect a quadrilateral shape - is there another method for that or some way to use the hough lines? Also once the corners of the quadrilateral are detected, I want it to return a rectangle just like this class does - http://www.aforgenet.com/framework/docs/html/7039a71d-a87d-47ef-7907-ad873118e374.htm - is there an equivalent library in openCV?

abhishek
  • 817
  • 6
  • 19
  • 33

3 Answers3

4

How do your input images look like? If you detect lots of line segments with the Hough transformation, you could maybe try using RANSAC to generate a number of quadrilateral shape hypothesis, find a way to rate their fitness and return the best hypothesis.

One hypothesis could be generated like this:

  • choose four random line segments from the detected line segment set
  • find the four corners by looking for intersections of the lines the chosen line segments lie on
  • rate the fitness of the quadrilateral shape defined by these four points

The fitness could maybe be the area of the hypothetical quadrilateral (see the Bretschneider's formula for calculating the area of a convex quadrilateral), the distance of the quadrilateral edges from the other line segments in the detected set, or something that better fits your application.

This is just an idea, I haven't tried using this approach yet (but I'm planing on implementing something similar). Let me know if you think this could work, or why it won't! :)

ppalasek
  • 372
  • 2
  • 12
2

Your algorithm could be something like this

  1. Process the image to find edges (Canny filter)
  2. Apply Hough Transformation to find lines
  3. Detect pairs of lines that intersect with an angle of 90 deg (aprox)
jose
  • 2,733
  • 4
  • 37
  • 51
  • Yup, already aware of this - but was looking to find a more optimal algorithm or existing method that gives the quadrilateral corners. Also, quadrilateral transformation needs to be done after corner extraction. – abhishek Oct 24 '12 at 12:06
  • 2
    If the lines result from the perspective projection of a quadrilateral in 3D onto a 2D surface, then there is no guarantee that the angles between adjacent edges will be 90 degrees. However given a set of four lines, you could still perform some kind of evaluation of the likelihood of it being a projected quadrilateral -- it's just a little harder in such a case. – Drew Noakes Dec 19 '12 at 11:32
1

If you use OpenCV library than you definitely should try FindChessboardCorners function. And also here's a good tutorial.

ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53
  • Not sure if this applies in my case. – abhishek Oct 24 '12 at 12:04
  • it detects a chessboard pattern in the image which is not the case here. Won't it fail as the quadrilateral in my image doesn't have any internal corners/squares? – abhishek Oct 24 '12 at 12:10
  • 1
    Sorry, I don't know answer to your question because I have never worked with calibration. Anyway you want me to test it for you? – ArtemStorozhuk Oct 24 '12 at 17:40