public static NamedOnnxValue BitmapCHWToTensor(Bitmap img, PixelNormalizationMode pixelmode) { float[] data = img.Image2Array(PixelNormalizationMode.imagenet); var tensor = new Microsoft.ML.OnnxRuntime.Tensors.DenseTensor <float>(data, InferHelper.session.InputMetadata[InferHelper.inputName].Dimensions); return(NamedOnnxValue.CreateFromTensor <float>(InferHelper.inputName, tensor)); }
public static float[] Image2Array(this Bitmap image, PixelNormalizationMode mode = PixelNormalizationMode.ZeroCentral) { var arr = image.ToNDArray().astype(NPTypeCode.Double); if (mode == PixelNormalizationMode.ZeroBased) { arr = arr / 255; } else if (mode == PixelNormalizationMode.ZeroCentral) { arr = (arr - 127.5f) / 127.5f; } else if (mode == PixelNormalizationMode.imagenet) { //[0.485, 0.456, 0.406] //[0.229, 0.224, 0.225] NDArray mean = np.expand_dims(np.expand_dims(np.array(new float[] { 0.485f, 0.456f, 0.406f }), 0), 0); NDArray std = np.expand_dims(np.expand_dims(np.array(new float[] { 0.229f, 0.224f, 0.225f }), 0), 0); arr = (arr / 255f - mean) / std; } arr = np.transpose(arr, new int[] { 2, 0, 1 }); return(arr.flat.ToArray <float>()); }
public static float[] ParallelExtractHWC(this Bitmap image, PixelNormalizationMode mode = PixelNormalizationMode.ZeroCentral) { int heightStride = image.Width * 3; int imageWidth = image.Width; int imageHeight = image.Height; var features = new float[imageWidth * imageHeight * 3]; var bitmapData = image.LockBits(new System.Drawing.Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadOnly, image.PixelFormat); IntPtr ptr = bitmapData.Scan0; int bytes = Math.Abs(bitmapData.Stride) * bitmapData.Height; byte[] rgbValues = new byte[bytes]; int stride = bitmapData.Stride; // 將RGB值複製到array System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); // 根據pixel format對應像素 Func <int, int, int, int> mapPixel = GetPixelMapper(image.PixelFormat, stride); Parallel.For(0, imageHeight, (int h) => { Parallel.For(0, imageWidth, (int w) => { Parallel.For(0, 3, (int c) => { if (mode == PixelNormalizationMode.ZeroBased) { features[heightStride * h + 3 * w + c] = (float)(rgbValues[mapPixel(h, w, c)]) / 255f; } else if (mode == PixelNormalizationMode.ZeroCentral) { features[heightStride * h + 3 * w + c] = (float)(rgbValues[mapPixel(h, w, c)] - 127.5f) / 127.5f; } else { features[heightStride * h + 3 * w + c] = (float)(rgbValues[mapPixel(h, w, c)]); } }); }); }); image.UnlockBits(bitmapData); return(features); }
public static List <float> PrepareImageFloatArray(Bitmap img, int width, int height, PixelNormalizationMode normmode = PixelNormalizationMode.ZeroCentral) { return(img.PadToFit(width, height).ParallelExtractCHW(normmode).Select(x => (float)x).ToList()); }
public static Bitmap FloatListToBitmap(List <IList <float> > p, int width, int height, PixelNormalizationMode zeroCentral) { using (Bitmap bm = new Bitmap(width, height, PixelFormat.Format32bppArgb)) { //long quality = 90; // //EncoderParameters parametre = new EncoderParameters(1); //parametre.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); var features = p[0]; for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { int basenum = width * h * 3 + w * 3; if (features[width * h + w] == 1) { bm.SetPixel(w, h, Color.FromArgb(255, 128, 0, 0)); } else if (features[width * h + w] == 2) { bm.SetPixel(w, h, Color.FromArgb(255, 0, 128, 0)); } else if (features[width * h + w] >= 3) { bm.SetPixel(w, h, Color.FromArgb(255, 128, 128, 0)); } } } bm.Save("D:/Medusa.Server/Medusa.Test/bin/x64/Release/netcoreapp2.1/Images/Calibaratetest02.png", ImageFormat.Png); return(bm); } }
public static Bitmap FloatListToBitmap(float[] imagearr, int width, int height, PixelNormalizationMode mode = PixelNormalizationMode.ZeroCentral) { int stride = width * height; List <float> floatlist = new List <float>(); List <float> features = new List <float>(); int imageHeight = width; int imageWidth = height; //if (mode == ColorNormalizationMode.None) //{ // arr = imagearr.Select(x => (byte)x).ToArray(); //} //else if (mode == ColorNormalizationMode.ZeroBased) //{ // arr = imagearr.Select(x => (byte)(int)(x * 255)).ToArray(); //} //else if (mode == ColorNormalizationMode.ZeroCentral) //{ imagearr = imagearr.Select(x => (float)((x - 127.5) / 127.5)).ToArray(); Func <int, int, int> mapPixel = (h, w) => h * imageWidth + w;// GetPixelMapper(image.PixelFormat, stride); //Func<int, int, int, int> mapPixel = (h, w, c) => c * channelStride + w * h + c;// GetPixelMapper(image.PixelFormat, stride); Parallel.For(0, imageHeight, (int h) => { Parallel.For(0, imageWidth, (int w) => { features[imageWidth * h + w] = (float)imagearr[mapPixel(h, w)]; }); }); features.Select(b => (float)b).ToList(); var dis = imagearr.Distinct().ToList(); Console.WriteLine(dis); // Fill array with random values Bitmap bitmap = new Bitmap(width, height); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { if (imagearr[y * imageWidth + x] == 0) { bitmap.SetPixel(x, y, Color.FromArgb(255, 0, 0, 0)); } else if (imagearr[y * imageWidth + x] == 1) { bitmap.SetPixel(x, y, Color.FromArgb(255, 0, 128, 0)); } else if (imagearr[y * imageWidth + x] == 2) { bitmap.SetPixel(x, y, Color.FromArgb(255, 128, 128, 0)); } else if (imagearr[y * imageWidth + x] == 3) { bitmap.SetPixel(x, y, Color.FromArgb(255, 0, 64, 0)); } else if (imagearr[y * imageWidth + x] == 4) { bitmap.SetPixel(x, y, Color.FromArgb(255, 192, 0, 0)); } } } return(bitmap); }
public static List <float> ParallelExtractCHW(this Bitmap image, PixelNormalizationMode mode = PixelNormalizationMode.ZeroCentral) { int channelStride = image.Width * image.Height; int imageWidth = image.Width; int imageHeight = image.Height; var features = new float[imageWidth * imageHeight * 3]; var bitmapData = image.LockBits(new System.Drawing.Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadOnly, image.PixelFormat); IntPtr ptr = bitmapData.Scan0; int bytes = Math.Abs(bitmapData.Stride) * bitmapData.Height; byte[] rgbValues = new byte[bytes]; int stride = bitmapData.Stride; // 將RGB值複製到array Marshal.Copy(ptr, rgbValues, 0, bytes); // 根據pixel format對應像素 Func <int, int, int, int> mapPixel = GetPixelMapper(image.PixelFormat, stride); Parallel.For(0, imageHeight, (int h) => { Parallel.For(0, imageWidth, (int w) => { Parallel.For(0, 3, (int c) => { if (mode == PixelNormalizationMode.ZeroBased) { features[channelStride * c + imageWidth * h + w] = (float)(rgbValues[mapPixel(h, w, c)]) / 255f; } else if (mode == PixelNormalizationMode.ZeroCentral) { features[channelStride * c + imageWidth * h + w] = (float)(rgbValues[mapPixel(h, w, c)] - 127.5f) / 127.5f; } else if (mode == PixelNormalizationMode.imagenet) { //[0.485, 0.456, 0.406] //[0.229, 0.224, 0.225] if (c == 0) { features[channelStride * c + imageWidth * h + w] = ((float)(rgbValues[mapPixel(h, w, c)] / 255f) - 0.485f) / 0.229f; } else if (c == 1) { features[channelStride * c + imageWidth * h + w] = ((float)(rgbValues[mapPixel(h, w, c)] / 255f) - 0.456f) / 0.224f; } else { features[channelStride * c + imageWidth * h + w] = ((float)(rgbValues[mapPixel(h, w, c)] / 255f) - 0.406f) / 0.225f; } } else { features[channelStride * c + imageWidth * h + w] = (float)(rgbValues[mapPixel(c, h, w)]); } }); }); }); image.UnlockBits(bitmapData); return(features.ToList()); }