How do I recognize rectangles with OpenCV

[OpenCV] Recognize simple objects?

Hi Guys,

following problem that I could use (and would be immeasurably grateful for) help with:

With the help of a Kinect, objects are to be recognized in an area of ​​a defined size. These are largely circular discs of a fixed size and color. Some of them are just flat, others can have any shape that does not exceed the edge of the disc and has a maximum height. The background also has clearly defined colors; There are several of these, however, which contrast strongly with the color of the panes.

The Kinect camera is attached at a fixed height, but can be moved in the plane above the scan area. The task now is to use the known position of the Kinect camera to determine the exact coordinates x and y of all disk objects from a corner of the scan area to be defined as the point of origin.

I have already determined the position of the target objects on the camera image by first transferring the image to the HSV area, then running a threshold filter over it, which gives me exactly the necessary color angle of the targets as white (the rest black) . Unfortunately, there is also some background noise from objects of similar color outside the scan area.

The algorithm for finding the objects basically works like this:
The image is traversed pixel by pixel. If a pixel is found that is not equal to black (0), a recursive FloodFill is carried out at that point. The minimum and maximum x and y coordinates of the FloodFill algorithm then result in the boundary of the object found. A simple plausibility check (minimum and maximum height / width of the determined rectangle) then clearly filters out objects that are too small and too large. The rest is mainly the slice you want.

Long story short: I would like to optimize the algorithm described. There are certainly faster ways to find white objects of a certain shape (elliptical, because a circular disc filmed from the side from an acute angle) but of unknown size in an otherwise black image, right?

For information: we use Ubuntu, OpenCV and Freenect.

I would be extremely grateful for any tip.

Best wishes,
Hendrik