I have a object and I would like to define exact center of it. Using OpenCV I detect the edges with Canny
and then I perform HoughLinesP
to detect lines: see this .
I use Hough transform because object is not completely rectangular and sometimes there are bumps detected here .
Lines are defined in a struct. I also calculate the middle point, angle and length:
struct hLine {
Point pStart, pEnd, pMidpoint;
float angle;
int length;
};
As you can see there are multiple lines detected along the sides.
The approach I tried was to calculate line equation y = kx + n , then sort the lines by angle, length and n and keep only lines, which have n separated by some number (opposite sides of rectangle). I have a problem with vertical lines ( n cannot be calculated) and also when object is almost vertical n number is large so line is not deleted.
Next step is to calculate line intersections and then calculate the center. Idea is based on this tutorial: opencv-code.com/tutorials/automatic-perspective-correction-for-quadrilateral-objects/
Also if someone has a better solution for detecting accurate center point, please speak up :)
After using canny to detect edges you can use a morphological closing operation to join the edges. Then find the contours and smooth the contours using a polygon approximation. For every quadrangle that you find you can check if the corners are roughly 90 degrees to verify that it is a rectangle. According to your sample image, the largest rectangle that you detect should be the rectangle that you are interested in. (you may also find the convex hull of each contour and then apply the polygon approximation to the convex hull).
I would do:
I would do it like this:
Steps 1 and 2 are already done in the link you provide btw.
What if Hough line detection is not robust enough?
In this case, I would go with one very robust detection algorithm called meaningful alignments proposed by A. Desolneux. The math behind it can look a bit scary at first, but the algorithm is still very easy to implement (maybe a bit slow though). I've used it in the past and the results are really as good as the author claims.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.