0

I'm searching for a method to fit a rectangle into an image. The given data is a gradient image of the rectangle which hast to be detected, the width and height the rectangle should have and an approximation of the position and its rotation.

Gradient image

The algorithm should find the best fit for the rectangle. It should not(!) find a minimum enclosing rectangle. This is due to outliers in the data which would corrupt the result. So the edges of the fitted rectangle should lay on the edges in the gradient image and it should propably use the magnitude of the gradient.

Detected rectangle

I was thinking of some kind of least squares matching or gradient descent but couldn't find an approach to get started.

The rectangles do have rounded corners which should not be a problem for the detection but it could be integrated and a scaling factor would also come in handy because the objects have quite big tolerances.

My current approach is kind of a brute force method. I'm drawing a rectangle with the given parameters and multiply the array of the drawn rectangle with the array of the gradient image pointwise. After that I sum up all the values in the array and get the position and rotation where the sum is the maximum. It works pretty well but it takes too many iterations and a good guess at the start of the search is needed.

Minik
  • 11
  • 3
  • Does this answer your question? [detecting lines of a rectangle image using hough transform](https://stackoverflow.com/questions/56414282/detecting-lines-of-a-rectangle-image-using-hough-transform) – Michael Cao Jun 08 '23 at 14:46
  • @MichaelCao No, the quality of the detected object is not consistent. There can be missing edges and/or to much noise in the data. The hough transform often detects wrong or no edge. I'm am searching for a way to fit the rectangle as whole and not single lines. – Minik Jun 09 '23 at 08:18
  • I've done what you're proposing before, but with circles of fixed size, so only 2 degrees of freedom. It worked fine, but was slow. I think the problem is that you have too many of dof, so the search space is too big and you need a good initial guess. First, I'd try to set the steps in the algorithm to something big and an integer, just to be sure it's not getting hung up on subpixels. Then, I'd look into image registration to try to align the rectangles horizontally, then try the algo. If it's still bad, I'd try to use some edge detection heuristic to get some automatic initial guesses. – K.Cl Jun 09 '23 at 09:54
  • 1
    Hey @K.Cl, thanks for the suggestions. The point with the image registration got me thinking of cross correlation. So I only have two dof (rotation and scaling) that I have to try. With the reduced iterations I achieved a speed up of 80 and the exponential growth is manageable when increasing the search parameters. – Minik Jun 09 '23 at 14:11

0 Answers0