简体   繁体   中英

Detecting accurate center point of rectangular object using HoughLinesP

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 question is: How to get the longest line for each side of the rectangle (0, 1, 3, 4 in this case)?

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:

  • Calculate the OBB (orientated bounding box) of the data, first.
  • Create four bounding boxes along the edges of the OBB with a width some predefined accuracy.
  • Discard all points, which are in more of one of the four edge bounding boxes.
  • Make four minimum edge bounding boxes for the edges (discard bumps, here)
  • Create a rectangle from the center lines of the four edge bounding boxes.
  • Calculate the center of the rectangle.

I would do it like this:

  1. use Hough transforms to compute line equations. There should be no problem with vertical/horizontal lines if you parameterize a line by its normal vector;
  2. then, get the 4 corners of the rectangle by computing the intersections of the lines;
  3. finally, get the centre by computing the equations of the diagonal lines of the rectangle followed by looking for their intersection.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM