如何计算图像的总和;所有元素的总体总数。
源图像是ARGB格式,每个颜色通道可以视为单个2D矩阵。这种做法是对的吗?
伪代码:
instance.save(update_fields=fields)
以下是我尝试的C#代码;但是,我不明白如何在C#中将字节值转换为Real。 Real是C#中的双数据类型。
Total[ImageData[img],2] (*Mathematica code*)
控制台线路输出都给出:2774762是字节值,我怎样才能得到它的双(实际)值?
// 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----------------------------------//
如何在C#中实现这一目标?
答案 0 :(得分:1)
加载图片:
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
}