Ejemplo n.º 1
0
        public static Bitmap ImageToBitmap(ColorFloatImageFormat image)
        {
            Bitmap b = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb);

            LockBitmapInfo lbi = LockBitmap(b);

            try
            {
                for (int j = 0; j < image.Height; j++)
                {
                    for (int i = 0; i < image.Width; i++)
                    {
                        ColorFloatPixel p = image[i, j];
                        lbi.data[lbi.linewidth * j + i * 4]     = p.B <0.0f ? (byte)0 : p.B> 255.0f ? (byte)255 : (byte)p.B;
                        lbi.data[lbi.linewidth * j + i * 4 + 1] = p.G <0.0f ? (byte)0 : p.G> 255.0f ? (byte)255 : (byte)p.G;
                        lbi.data[lbi.linewidth * j + i * 4 + 2] = p.R <0.0f ? (byte)0 : p.R> 255.0f ? (byte)255 : (byte)p.R;
                    }
                }
            }
            finally
            {
                UnlockBitmap(lbi);
            }

            return(b);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Преобразование из одномерного массива пикселей в матрицу, в соответвии с размерами картинки
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static ColorFloatPixel[,] ColorImage(ColorFloatImageFormat image)
        {
            ColorFloatPixel[,] imageData = new ColorFloatPixel[image.Height, image.Width];

            for (int height = 0, countPixel = 0; height < image.Height; ++height)
            {
                for (int width = 0; width < image.Width; ++width)
                {
                    imageData[height, width] = image.RawData[countPixel++];
                }
            }

            return(imageData);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Преобразует все изображения в список
        /// с их матрицами пикселей
        /// </summary>
        /// <param name="imagesName"></param>
        /// <returns></returns>
        public static List <ColorFloatPixel[, ]> ImagesToCheck(List <string> imagesName)
        {
            List <ColorFloatPixel[, ]> pixelImages = new List <ColorFloatPixel[, ]>();
            string path = "E:/DigitRecognition/DigitRecognition/DigitRecognition/assets/";

            for (int i = 0; i < imagesName.Count; i++)
            {
                string InputFileName        = path + imagesName[i];
                ColorFloatImageFormat image = ImageIO.FileToColorFloatImage(InputFileName);
                pixelImages.Add(PatternHelper.ColorImage(image));
            }

            return(pixelImages);
        }
Ejemplo n.º 4
0
        public static ColorFloatImageFormat FileToColorFloatImage(string filename)
        {
            if (CheckPGM(filename))
            {
                return(ReadPGM(filename).ToColorFloatImage());
            }

            Bitmap B = new Bitmap(filename);
            ColorFloatImageFormat res = BitmapToColorFloatImage(B);

            B.Dispose();

            return(res);
        }
Ejemplo n.º 5
0
        public ColorFloatImageFormat ToColorFloatImage()
        {
            ColorFloatImageFormat res = new ColorFloatImageFormat(Width, Height);

            for (int i = 0; i < res.RawData.Length; i++)
            {
                res.RawData[i] = new ColorFloatPixel()
                {
                    B = RawData[i],
                    G = RawData[i],
                    R = RawData[i],
                    A = 0.0f
                }
            }
            ;

            return(res);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// //Разложение в rgb и запись в массив
        /// </summary>
        /// <param name="B"></param>
        /// <returns></returns>
        public static ColorFloatImageFormat BitmapToColorFloatImage(Bitmap B)
        {
            int W = B.Width, H = B.Height;
            ColorFloatImageFormat res = new ColorFloatImageFormat(W, H);

            if (B.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                Color[] pi  = B.Palette.Entries;
                byte[]  pal = new byte[1024];
                for (int i = 0; i < pi.Length; i++)
                {
                    Color C = pi[i];
                    pal[i * 4]     = C.B;
                    pal[i * 4 + 1] = C.G;
                    pal[i * 4 + 2] = C.R;
                    pal[i * 4 + 3] = C.A;
                }

                LockBitmapInfo lbi = LockBitmap(B, PixelFormat.Format8bppIndexed, 1);
                try
                {
                    for (int j = 0; j < H; j++)
                    {
                        for (int i = 0; i < W; i++)
                        {
                            int c = lbi.data[lbi.linewidth * j + i];
                            int b = pal[c * 4];
                            int g = pal[c * 4 + 1];
                            int r = pal[c * 4 + 2];

                            res[i, j] = new ColorFloatPixel()
                            {
                                B = b,
                                G = g,
                                R = r,
                                A = 0.0f
                            };
                        }
                    }
                }
                finally
                {
                    UnlockBitmap(lbi);
                }
            }
            else
            {
                LockBitmapInfo lbi = LockBitmap(B);
                try
                {
                    for (int j = 0; j < H; j++)
                    {
                        for (int i = 0; i < W; i++)
                        {
                            int b = lbi.data[lbi.linewidth * j + i * 4];
                            int g = lbi.data[lbi.linewidth * j + i * 4 + 1];
                            int r = lbi.data[lbi.linewidth * j + i * 4 + 2];

                            res[i, j] = new ColorFloatPixel()
                            {
                                B = b,
                                G = g,
                                R = r,
                                A = 0.0f
                            };
                        }
                    }
                }
                finally
                {
                    UnlockBitmap(lbi);
                }
            }

            return(res);
        }
Ejemplo n.º 7
0
 public static void ImageToFile(ColorFloatImageFormat image, string filename)
 {
     using (Bitmap b = ImageToBitmap(image))
         b.Save(filename);
 }