I made rectangle detection work with contour detection and apply polygon with OpenCv to get location of the rectangle before adjusting the perspective projection. And it's working great. But some people in my group suggested Hough transformation instead. I wonder if there is any advantage of using Hough transformation for rectangle detection.
Update: I tried both of the methods. In my example, both methods worked fine after Canny edge detection. But since Hough transformation produces lines, we have to assume several things such as length of lines and connectability of the lines and should do additional computations such as searching connected lines and find corner points from the connected lines. Personally, I liked contour method better since its concept is simpler. With the method, you just search contours that can be approximated with closed and convex polygons with 4 corners and adjust the polygons for their perspective projections. That's about it.
In this method, every pixel of the image is scanned, and a sliding window is used to compute the Hough Transform of small regions of the image. Peaks of the Hough image (which correspond to line segments) are then extracted, and a rectangle is detected when four extracted peaks satisfy certain geometric conditions.
The Hough transform (HT) can be used to detect lines circles or • The Hough transform (HT) can be used to detect lines, circles or other parametric curves. It was introduced in 1962 (Hough 1962) and first used to find lines in images a decade later (Duda 1972). The goal is to find the location of lines in images.
Hough transform is an algorithm used to detect straight lines in images. It usually takes the output of an edge detection algorithm as an input (in our case, we use Canny for that). To find lines in an image, the algorithm maps the edge points in an image onto the polar coordinate system.
If two edge points lay on the same line, their corresponding cosine curves will intersect each other on a specific (ρ, θ) pair. Thus, the Hough Transform algorithm detects lines by finding the (ρ, θ) pairs that have a number of intersections larger than a certain threshold.
What sort of results are you getting with contour detection so far? Got any examples?
Hough transform should work well for rectangle detection IFF you can assume that the sides of the rectangle are the most prominent lines in your image. Then you can simply detect the 4 biggest peaks in hough space and you got your rectangle.
This works for example with a photo of a white sheet of paper in front of a dark background.
Ideally you would preprocess the image with blur, threshold, morphological operators to remove any small-scale structures before hough transform.
If there are multiple smaller rectangles or other sorts of prominent lines in your images, contour detection might be the better choice.
Some general advantages for the hough transform off the top of my head:
In the end it probably depends on the input data. Got any examples?
Perhaps a combined approach would be best? see Combining Hough Transform and Contour Algorithm for detecting Vehicles License-Plates
I did some experiments in using hough transform to detect rectangles a while back, you can see some preliminary results here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=14491&start=9
Unfortunately that is all that exists at the moment, the project is currently on hiatus, eventually I hope to resume it when I am less busy.
I'd be very interested in your results in comparison.
(If you are doing perspective correction, also check out proportions of a perspective-deformed rectangle )
Searching for contour detection with hough transform brought me to this SO.
To help future searchers, this blog post has a good walkthrough to do this with opencv:
http://opencv-code.com/tutorials/automatic-perspective-correction-for-quadrilateral-objects/
The concept:
1. Get the edge map - canny, sobel
2. Detect lines with Hough transform
3. Get the corners by finding intersections between lines.
4. Check if the approximate polygonal curve has 4 vertices with approxPolyDP
5. Determine top-left, bottom-left, top-right, and bottom-right corner.
6. Apply the perspective transformation with getPerspectiveTransform to get the transformation matrix and warpPerspective to apply the transformation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With