Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting different bitmap object from same image file
    primarykey
    data
    text
    <p>I am creating Bitmap object from the same image, but in the end getting different results. It should calculate deviation from the image colors and images are the same so why results are different?</p> <pre><code> double test1 = GetStdDev("C:\\temp\\images\\file.jpg"); Bitmap img = new Bitmap("C:\\temp\\images\\file.jpg"); double test2 = GetStdDev(img); public static double GetStdDev(string imageFileName) { double total = 0, totalVariance = 0; int count = 0; double stdDev = 0; // First get all the bytes using (Bitmap b = new Bitmap(imageFileName)) { BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat); int stride = bmData.Stride; IntPtr Scan0 = bmData.Scan0; unsafe { byte* p = (byte*)(void*)Scan0; int nOffset = stride - b.Width * 3; for (int y = 0; y &lt; b.Height; ++y) { for (int x = 0; x &lt; b.Width; ++x) { count++; byte blue = p[0]; byte green = p[1]; byte red = p[2]; int pixelValue = Color.FromArgb(0, red, green, blue).ToArgb(); total += pixelValue; double avg = total / count; totalVariance += Math.Pow(pixelValue - avg, 2); stdDev = Math.Sqrt(totalVariance / count); p += 3; } p += nOffset; } } b.UnlockBits(bmData); } return stdDev; } private static double GetStdDev(Bitmap img) { double total = 0, totalVariance = 0; int count = 0; double stdDev = 0; // First get all the bytes using (Bitmap b = new Bitmap(img)) { BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat); int stride = bmData.Stride; IntPtr Scan0 = bmData.Scan0; unsafe { byte* p = (byte*)(void*)Scan0; int nOffset = stride - b.Width * 3; for (int y = 0; y &lt; b.Height; ++y) { for (int x = 0; x &lt; b.Width; ++x) { count++; byte blue = p[0]; byte green = p[1]; byte red = p[2]; int pixelValue = Color.FromArgb(0, red, green, blue).ToArgb(); total += pixelValue; double avg = total / count; totalVariance += Math.Pow(pixelValue - avg, 2); stdDev = Math.Sqrt(totalVariance / count); p += 3; } p += nOffset; } } b.UnlockBits(bmData); } return stdDev; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload