Search code examples
.netimageimage-processingcomputer-visionobject-detection

Identifying multiple (graphical) object's edges from points


I've got an application which is identifying motion in a webcam image. It produces something like the following...

enter image description here

Areas in black indicate motion. This is largely done on a per-pixel basis (although nearby pixels are taken into account)

So... Now that I've got a movement true/false for each pixel, I need to use that information to identify object outlines.

What I'd like is something like the following.

enter image description here

Outlines don't have to be precise and I could accept a bounding box. There are also some areas of noise which are more visible if I show you both combined...

enter image description here

As you can see there are a couple of "movement" pixels outside the objects. Presumably, I'd eliminate these by specifying a minimum area for an object.

So, what algorithm(s) are there for identifying the edges of objects. Ideally, I'd then be able to use this information to calculate the approximate center of each object.

Note: As mentioned by @mmgp, the images above are all full RGB, even the B&W one. This is due to the way I'm generating the image for export. Internally, it's a Bit array.


Solution

  • This seems to be a perfect task for Mathematical Morphology. To remove the small objects that, in this case, is a form a noise, perform a morphological opening by area. The area is easily estimated in your problem, as the interest regions are much larger than the unwanted ones. Now you also want to eliminate holes inside the large objects (notice there are some of these in your example). To do this you perform a operation that is called hole filling, which will simply discard those points that cannot be reached from the background of your image. At this point you can proceed to detect the centroids of your objects, but if you want to make the borders of your objects more uniform, you can use morphological dilations with small structuring elements or maybe morphological closing to possibly preserve more of the object.

    These tasks are performed in Matlab as:

    f = imread('https://i.sstatic.net/DexHs.png');
    % The PNG is in RGB, but it actually describes a binary image.    
    f = ~im2bw(f, 0);
    g = bwareaopen(f, 100); % 100 is the maximum area for unwanted objects here
    h = imfill(g, 'holes');
    l = bwlabel(h);
    cent = regionprops(l, 'centroid');
    

    Which results in:

    enter image description here

    The two closed white curves are the boundaries of the remaining objects and in yellow you see their centroids. If you want a "softer" boundary as described in the initial comments of this answer:

    h1 = imclose(h, strel('disk', 3));
    

    enter image description here

    I see you tagged this as .net, but I would expect these tools to be readily available in some .net package as they are very basic and common.