Mean filtering and mean filtering algorithm program

Mean Filtering

Mean filtering is a widely used linear filtering technique in image processing. It works by replacing the value of a target pixel with the average value of its neighboring pixels within a defined window or template. This method helps to reduce noise and smooth out variations in the image, making it particularly useful for preprocessing tasks.

The basic idea behind mean filtering is to apply a sliding window over the image, where each pixel is replaced by the average of all the pixels inside the window. For example, if a 3x3 window is used, the current pixel (x, y) is replaced with the average of the surrounding eight pixels plus itself. Mathematically, this can be expressed as:

g(x, y) = (1/m) * Σ f(x + i, y + j), where m is the total number of pixels in the window, and f(x + i, y + j) represents the pixel values in the window.

Mean filtering is also known as neighborhood averaging. While it is effective at reducing random noise, it has some drawbacks. One major limitation is that it tends to blur edges and fine details in the image because it treats all pixels equally, regardless of their importance in defining the structure of the image.

One of the main applications of mean filtering is noise reduction. Since most types of random noise appear as sudden changes in pixel intensity, averaging the values in a local region can help suppress these irregularities. However, this process can also remove small but meaningful details, especially when the filter size is large.

Despite its simplicity, mean filtering is computationally efficient and easy to implement, which makes it a popular choice in many real-time image processing systems.

Implementation of Mean Filtering in C Language

Below is an example of how mean filtering can be implemented in C. The code reads an image from a file, applies the mean filter, and saves the result to another file.

// junzhilvbo.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "stdlib.h"
#include "string.h"

#define DATA_X // Number of horizontal pixels
#define DATA_Y // Number of vertical pixels

void OpenFile(const char *cFilePath, int nOriginalData[DATA_Y][DATA_X]) {
    printf("Getting data...");
    FILE *fp;
    fp = fopen(cFilePath, "r");
    if (NULL == fp) {
        printf("open file failed!");
        return;
    }
    unsigned char *pData = (unsigned char *)malloc(sizeof(unsigned char) * DATA_X * DATA_Y);
    if (NULL == pData) {
        printf("memory malloc failed!");
        return;
    }
    fread(pData, sizeof(unsigned char) * DATA_X * DATA_Y, 1, fp);
    int count_x = 0, count_y = 0;
    for (count_y = 0; count_y < DATA_Y; count_y++) {
        for (count_x = 0; count_x < DATA_X; count_x++) {
            nOriginalData[count_y][count_x] = pData[count_y * DATA_X + count_x];
        }
    }
    free(pData);
    fclose(fp);
}

void SaveFile(const char *cFilePath, int nResultData[DATA_Y][DATA_X]) {
    printf("Saving data.....");
    int count_x, count_y;
    FILE *fp;
    fp = fopen(cFilePath, "w");
    if (NULL == fp) {
        printf("open file failed!");
        return;
    }
    for (count_y = 0; count_y < DATA_Y; count_y++) {
        for (count_x = 0; count_x < DATA_X; count_x++) {
            fwrite(&nResultData[count_y][count_x], sizeof(int), 1, fp);
        }
    }
    fclose(fp);
    printf("File saved successfully!");
}

bool JunZhiLvBo(const int nOriginalData[DATA_Y][DATA_X], int nResultData[DATA_Y][DATA_X]) {
    printf("Averaging filtering is in progress...");
    int count_x, count_y;
    for (count_y = 1; count_y < DATA_Y - 1; count_y++) {
        for (count_x = 1; count_x < DATA_X - 1; count_x++) {
            nResultData[count_y][count_x] = (nOriginalData[count_y - 1][count_x - 1] +
                                             nOriginalData[count_y - 1][count_x] +
                                             nOriginalData[count_y - 1][count_x + 1] +
                                             nOriginalData[count_y][count_x - 1] +
                                             nOriginalData[count_y][count_x] +
                                             nOriginalData[count_y][count_x + 1] +
                                             nOriginalData[count_y + 1][count_x - 1] +
                                             nOriginalData[count_y + 1][count_x] +
                                             nOriginalData[count_y + 1][count_x + 1]) / 9;
        }
    }

    for (count_x = 0; count_x < DATA_X; count_x++) {
        nResultData[0][count_x] = nOriginalData[0][count_x];
        nResultData[DATA_Y - 1][count_x] = nOriginalData[DATA_Y - 1][count_x];
    }

    for (count_y = 0; count_y < DATA_Y; count_y++) {
        nResultData[count_y][0] = nOriginalData[count_y][0];
        nResultData[count_y][DATA_X - 1] = nOriginalData[count_y][DATA_X - 1];
    }

    return true;
}

int _tmain(int argc, _TCHAR* argv[]) {
    int nOriginalData[DATA_Y][DATA_X];
    int nResultData[DATA_Y][DATA_X];
    memset(nOriginalData, 0, sizeof(nOriginalData));
    memset(nResultData, 0, sizeof(nResultData));
    char cOpenFilePath[] = "Lena.raw";
    OpenFile(cOpenFilePath, nOriginalData);
    if (!JunZhiLvBo(nOriginalData, nResultData)) {
        printf("Operation failed!");
        return 1;
    }
    char cSaveFilePath[] = "Result.raw";
    SaveFile(cSaveFilePath, nResultData);
    return 0;
}

Comparison of Mean Filtering Algorithms

Before mean filtering: After mean filtering:

Mean filtering and mean filtering algorithm program

This image shows the difference between an original image and one that has been processed using mean filtering. As you can see, the filtered image appears smoother, with less noise and fewer sharp transitions in pixel intensity.

LCD Interactive Blackboard

Shanghai Really Technology Co.,Ltd , https://www.really-led.com