Ereny Magdy
Ereny Magdy

Reputation: 1

how to calculate noise denisty per square inch in black and white image in c#

I need to calculate noise density per square inch in a black and white image.

I tried to convert the black and white image to grayscale and calculate the mean and standard deviation for each pixel, but I always get 0.

I don't know what's wrong in my code:

Image image = Image.FromFile(imagePath);

// Convert the image to grayscale
Bitmap bmp = new Bitmap(image);
Bitmap grayImage = Grayscale(bmp);

Rectangle area = new Rectangle(0, 0, grayImage.Width, grayImage.Height);
int squareInchSize = 1;
int pixelCount = (int)(squareInchSize / 2.54 * grayImage.HorizontalResolution) *
(int)(squareInchSize / 2.54 * grayImage.VerticalResolution);

double totalNoise = 0.0;
for (int x = area.X; x < area.X + area.Width; x++)
{
    for (int y = area.Y; y < area.Y + area.Height; y++)
    {
        Color pixelColor = grayImage.GetPixel(x, y);
        double pixelNoise = CalculateNoise(pixelColor.R, pixelColor.G, pixelColor.B);
        totalNoise += pixelNoise;
    }
}
double noisePerSquareInch = totalNoise / pixelCount;

private static Bitmap Grayscale(Bitmap bmp)
{
    Bitmap grayImage = new Bitmap(bmp.Width, bmp.Height);

    for (int x = 0; x < bmp.Width; x++)
    {
        for (int y = 0; y < bmp.Height; y++)
        {
            Color pixelColor = bmp.GetPixel(x, y);
            int grayValue = (int)(0.299 * pixelColor.R + 0.587 * pixelColor.G + 0.114 * pixelColor.B);
            Color grayColor = Color.FromArgb(grayValue, grayValue, grayValue);
            grayImage.SetPixel(x, y, grayColor);
        }
    }

    return grayImage;
}

private static double CalculateNoise(byte r, byte g, byte b)
{
    double avg = (r + g + b) / 3.0;
    double var = ((r - avg) * (r - avg) + (g - avg) * (g - avg) + (b - avg) * (b - avg)) / 3.0;
    double stdDev = Math.Sqrt(var);
    return stdDev / 255.0;
}

Upvotes: 0

Views: 164

Answers (1)

JonasH
JonasH

Reputation: 36629

You cannot calculate the average or standard deviation from a single pixel. If the image is greyscale all the rgb values will be the same, and even if you had a color image, the result would be meaningless.

What you can do is calculate the standard deviation in regions of the image. I.e. process the images in blocks of 100x100, or whatever size you prefer, and calculate the average and standard deviation in that block.

Just compute the average of the pixel values in that block, and for each pixel compute the difference from the average, square the differences, sum all the squares, and take the square root of the sum to get the standard deviation.

But while the standard deviation will give some idea about the variance of the block it will not really tell you anything about the noise. A sharp edge will give a high standard deviations, as will a single white pixel on an otherwise black background. But how do you determine if that pixel is due to noise or is intended? You cannot, you may be able to analyze the frequencies in the image to make guesses about the noise level, but that need to include a model about the spectrum of the noise.

I would probably just apply a high pass filter to the image, and use that as the noise level, since the high frequencies tend to have the worst noise to signal ratio.

Upvotes: 0

Related Questions