Search code examples
pythonimageopencvimage-processingdetection

Delete a shape from an image


I am working on pictures like this :

enter image description here

My final goal is to detect if the very end of the stem is in the circle or not on new pictures (position and size of the circle may vary a bit), and I wanted to try it with image processing only (no ML for a first try because it seems not necessary) but I don't have knowledge in it.

The problem is that on a part of the dataset, the clear trapezium is not present, and on the other part it is present. The problem without the trapeze seems pretty simple (find if the end of the stem is in the circle), so I want to find a way to "erase" the trapeze of the pictures where it is present because I need my algorithm to work on both images (with and without the trapezium). I am able to know if the trapeze is present with the name of the picture.

First, I denoised the images and by looking at the intensity of the pixel you can see below (intensity in ordinate, count in abscissa), I litteraly just merged the pixel values into 5 groups. enter image description here

enter image description here But as you can see, a part of the stem is closer to the intensity of the trapezium than to the rest of the stem. Moreover, the values of the edges are also problematic.

Just if it can provide more information, I also tried edge detection and achieve to detect the circle whatever his position and size is. Here is a sneak peak of the edge detection result : enter image description here

I'm open to any idea (I know the initial question is how to erase the trapeze, but if you think this is not necessary and a lack of time, I'll consider every option) :)


Solution

  • Since you say that the problem seems easy without the lighter shape I'm going to assume that you know how to detect circles and lines / line segments (e.g. using variants of the Hough transform, as has been suggested in the comments).

    In the edge image you show the circle is clearly visible. You should be able to locate it (and if I understand you correctly, you have already achieved that).

    There are multiple lines which you should be able to find. The remaining problem is to figure out which one is the one you are interested in. There are quite a few ways you could go about this, for example:

    • Notice that the intensity is similar on both sides of the stem but very different on both sides of one of the lines you want to ignore. Use this to find the line where the difference is smallest.
    • The edges of the trapezoid meet. Go through all combinations of line segments and discard those that form a corner. You could also limit the angle to some expected range.
    • In your image the stem consists of two mostly parallel lines. If that is always the case (and the lines can always be differentiated), you can use that to figure out which line is the one you are looking for.
    • Check if there is a line segment which ends inside the circle without also touching another line segment. If you find one, you know that the tip is inside the circle.

    Quantify these conditions and check if one or a combination of them allows you to reliably pick out the stem from all lines.

    The most problematic case would of course be if stem and edges align. Other special but less problematic cases would be if the stem crosses one or more of the edges, or if the stem ends at an edge. It is not clear to me which of these can happen - if they are possible, make sure you consider and test these scenarios.