10

I am trying to think of the best method to detect rectangles in an image.

My initial thought is to use the Hough transform for lines, and to select combinations of lines where you have two lines intersected at both the lower portion and upper portion by the same two lines, but this is not sufficient.

Would using a corner detector along with the Hough transform work?

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
Jim
  • 4,509
  • 16
  • 50
  • 80
  • By not sufficient, you mean not efficient, right? That really depends on your application -- unless you're doing this for high-definition video in real time, Hough transform might be good enough. – mpenkov Dec 17 '11 at 16:06

3 Answers3

8

Check out /samples/c/squares.c in your OpenCV distribution. This example provides a square detector, and it should be a pretty good start.

My answer here also applies.

Community
  • 1
  • 1
Throwback1986
  • 5,887
  • 1
  • 30
  • 22
3

I don't think that currently there exists a simple and robust method to detect rectangles in an image. You have to deal with many problems such as the rectangles not being exactly rectangular but only approximately, partial occlusions, lighting changes, etc.

One possible direction is to do a segmentation of the image and then check how close each segment is to being a rectangle. Since you can't trust your segmentation algorithm, you can run it multiple times with different parameters.

Another direction is to try to parametrically fit a rectangle to the image such that the image gradient magnitude along the contour will be maximized.

If you choose to work on a parametric approach, notice that while the trivial way to parameterize a rectangle is by the locations of it's four corners, which is 8 parameters, there are a few other representations that require less parameters.

nojka_kruva
  • 1,454
  • 1
  • 10
  • 23
  • for a simple geometric shape a hough transform answers these points - for something more complex like a face you are exactly correct. – Martin Beckett Dec 17 '11 at 16:13
  • Actually I did some work on detecting rectangles and I have encountered these problems. Obviously, if you work with synthetic images then the Hough method will work wonderfully, but if you want to want to find rectangles "in the wild", then it's a completely different story. – nojka_kruva Dec 18 '11 at 12:52
1

There is an extension of Hough that can be useful.
http://en.wikipedia.org/wiki/Generalised_Hough_transform

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104