如何计算 ARGB 图像的总值?

How to calculate Total value of of an ARGB image?

如何计算图像的总数;所有元素的总和。

源图像为ARGB格式,每个颜色通道都可以视为单个二维矩阵。这种做法对吗?

伪代码:

Total[ImageData[img],2] (*Mathematica code*)

下面是我试过的C#代码;但是,我不明白如何在 C# 中将 Byte 值转换为 Real。 Real 是 C# 中的 Double 数据类型。

// Calculate total of Image; Total[ImageData[img]];
                Rectangle rect = new Rectangle(0, 0, MskImg3G.Width, MskImg3G.Height);
                System.Drawing.Imaging.BitmapData bmpData =
                MskImg3G.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, MskImg3G.PixelFormat);
                IntPtr ptr = bmpData.Scan0;
                int bytes = Math.Abs(bmpData.Stride) * MskImg3G.Height;
                byte[] TotalValues = new byte[bytes];

                // Copy the RGB values into the array.
                System.Runtime.InteropServices.Marshal.Copy(ptr, TotalValues, 0, bytes);

                // do something with the array
                //Console.WriteLine(rgbValues.Length);
                int sum = 0; foreach (byte b in TotalValues) sum += b;
                Console.WriteLine(sum);//Total[ImageData[img, "Byte"], 2]
                double vOut = Convert.ToDouble(sum);//Byte value - original ImageType - Real(MMA type)
                Console.WriteLine(vOut);//Total[ImageData[img, "Byte"], 2]

                // Copy the RGB values back to the bitmap
                System.Runtime.InteropServices.Marshal.Copy(TotalValues, 0, ptr, bytes);
                MskImg3G.UnlockBits(bmpData);
//-------------------------TEST----------------------------------//

两个控制台行输出都给出:2774762 是 Byte 值,我怎样才能得到它的 Double(real) 值?

> 2774762
> 2774762

如何在 C# 中实现这一点?

加载图像:

Bitmap bitmap = Bitmap.FromFile("C:\Temp\img.bmp", true);

获取并处理像素:

for for (int i=0;i<bitmap .Height;i++) for (int j=0;j<bitmap .width;j++) 
{
  Color c=bitmap .GetPixel(i,j) ;
  // Do your totalisation(s) here using c.R, c.G and c.B
}