0

In OpenCV we can use the approxPolyDP to find the contours of an object. However, sometimes the output contours could be quite different when there are holes/blur in between the lines. For example, the actual object of the following two graphs is a rectangle but the output of approxPolyDP generates the contours that are not-in-order. Is there any well-known algorithm that can process the following points and detect a rectangular shape? If not, what is the best approach to deal with this situation?

enter image description here

enter image description here

chesschi
  • 666
  • 1
  • 8
  • 36
  • if you know which points should form a rectangle you can use `minBoundingRect`. If you have a bunch of noisy point samples and you want to detect a rectangle, you should implement a simple `RANSAC` method yourself I guess. – Micka Dec 22 '14 at 10:55
  • @Micka I am open to any opinion because I am new to openCV. When you say RANSAC method, how can I implement this? Also my rectangle could be slightly rotated, will boundingRect still apply? – chesschi Dec 22 '14 at 12:31
  • You can use `minAreaRect` to find rotated rectangles. After thinking a little, RANSAC isnt completely what you want because you dont have so many samples, right? What about choosing each combination of 4 point samples and test whether they form or approximate a rectangle? What are the number of points you work with and how many points should build a rectangle? – Micka Dec 22 '14 at 13:00
  • @Micka: Yes I don't have many samples. It could be from 4 points (which is an ideal case) to any number of points approxPolyDP returned (like the above examples). How can I "choose" the sample, or somehow group/distribute all the points into 4 point samples when the points are not-in-order? – chesschi Dec 22 '14 at 13:11
  • just test each combination :) – Micka Dec 22 '14 at 13:16
  • @Micka: When you say "test", do you mean to calculate the distance between two points? I am not quite sure how to do this mathemtically and programatically. Please can you kindly explain more :) – chesschi Dec 22 '14 at 13:20
  • yes, compute distances from points and lines. Maybe there are other ways like determinant computation or sth like that too. – Micka Dec 22 '14 at 14:07
  • 2
    have a look at this: http://people.inf.ethz.ch/arbenz/MatlabKurs/node88.html – Micka Dec 22 '14 at 14:16

0 Answers0