Search code examples
algorithmcomputer-visionnoiseoutliers

Removing outlier pixels from a small binary image


I am currently implementing an algorithm for identifying the axis of minimum inertia of a colored mass (provided by the second moments). In order to do so, I need to acquire the centre of mass, as given by the first moments.

The weighted averaging function works well, but due to outlier pixels, I am receiving undesired results.

Here is the averaging function:

(e.g. x's weighted average)

for (i = 0, i < rows, i++) {
    for (j = 0, j < cols, j++) {
        if (colorAt(i,j).isForeground()) {
            tempSumX++;
            totalForeground++;
        }
    }
    x_ += i*tempSumX;
    tempSumX = 0;
}
x_ /= totalForeground; //where x_ represents the x coordinate of the weighted center of mass.

Incorrect Center of Mass

Given an image such as this, which is represented by exclusively two colors (background and foreground), how can I remove outlying pixels? Note: Outlying pixels refers to anything not part of the big color-mass. The white dot is the calculated center of mass, which is incorrect.

Much appreciated.


Solution

  • There are a lot of flood fill algorithms that would identify all the connected pixels given a starting point.

    Alternatively a common way to remove small outliars like these that come from noise is to erode the image, then dilate it to return to the same size - although if you are purely doing CoG you don't necessarily need the dilate step