Beispiel #1
0
        public void Colorize(string sourceImagePath, string greyImagePath, string destinationImagePath)
        {
            List <PixelYRGB> trainingData;
            List <PixelYRGB> validationData;

            Console.WriteLine("Processing image data and splitting into Training and Validation Sets");
            Bitmap           sourceBMP = (Bitmap)Image.FromFile(sourceImagePath);
            List <PixelYRGB> allData   = GeneratePCRDataForColorImage(sourceBMP);

            sourceBMP.Dispose();

            int splitValue = (allData.Count / 100 * 80);

            Shuffle(ref allData);

            trainingData   = allData.Take(splitValue).ToList();
            validationData = allData.TakeLast(allData.Count - splitValue).ToList();

            /************/
            /* Training */
            /************/
            MLContext mlContext        = new MLContext(seed: 0);
            IDataView trainingDataView = mlContext.Data.LoadFromEnumerable(trainingData);

            Console.WriteLine("Training Red Channel Model");
            var r_model = MLFunctions.Train(mlContext, trainingDataView, "R");

            Console.WriteLine("Training Green Channel Model");
            var g_model = MLFunctions.Train(mlContext, trainingDataView, "G");

            Console.WriteLine("Training Blue Channel Model");
            var b_model = MLFunctions.Train(mlContext, trainingDataView, "B");

            /**************/
            /* Evaluating */
            /**************/
            IDataView testDataView = mlContext.Data.LoadFromEnumerable(validationData);

            Console.WriteLine("Evaluating Red Channel Model:");
            MLFunctions.Evaluate(mlContext, r_model, testDataView);

            Console.WriteLine("Green Red Channel Model:");
            MLFunctions.Evaluate(mlContext, g_model, testDataView);

            Console.WriteLine("Blue Red Channel Model:");
            MLFunctions.Evaluate(mlContext, b_model, testDataView);

            /*********************************/
            /* Using Model to Predict Colors */
            /*********************************/
            Console.WriteLine("Loading Greyscale image");
            Bitmap bmp2 = (Bitmap)Image.FromFile(greyImagePath);

            List <PixelYRGB> realData = GeneratePCRDataForGreyImage(bmp2);

            CImage greyImage = new CImage(bmp2.Width, bmp2.Height, 24);

            bmp2.Dispose();

            IDataView realDataView = mlContext.Data.LoadFromEnumerable(realData);

            Console.WriteLine("Predicting Red Channel values");
            float[] rData = MLFunctions.MakeColorPredictions(mlContext, r_model, realDataView);

            Console.WriteLine("Predicting Green Channel values");
            float[] gData = MLFunctions.MakeColorPredictions(mlContext, g_model, realDataView);

            Console.WriteLine("Predicting Blue Channel values");
            float[] bData = MLFunctions.MakeColorPredictions(mlContext, b_model, realDataView);

            Console.WriteLine("Populating RGB Values");
            int pixelColorIndex = -1;

            for (int y3 = 0; y3 < greyImage.Height; y3++)
            {
                for (int x3 = 0; x3 < greyImage.Width; x3++)
                {
                    pixelColorIndex += 1;
                    int i3 = (x3 + (greyImage.Width * y3));

                    // Populate RGB with a little data massaging
                    greyImage.Grid[3 * i3 + 2] = Convert.ToByte((int)Math.Min(Math.Round(rData[pixelColorIndex]), 255));
                    greyImage.Grid[3 * i3 + 1] = Convert.ToByte((int)Math.Min(Math.Round(gData[pixelColorIndex]), 255));
                    greyImage.Grid[3 * i3 + 0] = Convert.ToByte((int)Math.Min(Math.Round(bData[pixelColorIndex]), 255));
                }
            }

            /*********************/
            /* Outputing results */
            /*********************/
            Console.WriteLine("Saving Colorized Image");
            Bitmap greyColored = greyImage.ToBitmap();

            greyColored.Save(destinationImagePath);
            greyColored.Dispose();
        }
Beispiel #2
0
        public void Colorize(string sourceImagePath, string greyImagePath, string destinationImagePath, IProgress <int> progressReporter = null)
        {
            List <PixelYUV> trainingData;
            List <PixelYUV> validationData;

            Console.WriteLine("Processing image data and splitting into Training and Validation Sets");

            Bitmap          sourceBMP = (Bitmap)Image.FromFile(sourceImagePath);
            List <PixelYUV> allData   = GenerateYUVCRDataForColorImage(sourceBMP);

            sourceBMP.Dispose();

            int splitValue = (allData.Count / 100 * 80);

            Shuffle(ref allData);



            trainingData   = allData.Take(splitValue).ToList();
            validationData = allData.TakeLast(allData.Count - splitValue).ToList();

            MLContext mlContext = new MLContext(seed: 0);

            /************/
            /* Training */
            /************/
            IDataView trainingDataView = mlContext.Data.LoadFromEnumerable(trainingData);

            Console.WriteLine("Training U Channel Model");
            var u_model = MLFunctions.Train(mlContext, trainingDataView, "U");

            Console.WriteLine("Training V Channel Model");
            var v_model = MLFunctions.Train(mlContext, trainingDataView, "V");

            Common.Utilities.SetProgress(progressReporter, 25);

            /**************/
            /* Evaluating */
            /**************/
            IDataView testDataView = mlContext.Data.LoadFromEnumerable(validationData);

            Console.WriteLine("Evaluating U Channel Model:");
            MLFunctions.Evaluate(mlContext, u_model, testDataView);

            Console.WriteLine("Evaluating U Channel Model:");
            MLFunctions.Evaluate(mlContext, v_model, testDataView);

            Common.Utilities.SetProgress(progressReporter, 50);

            /*********************************/
            /* Using Model to Predict Colors */
            /*********************************/
            Console.WriteLine("Loading Greyscale image");
            Bitmap bmp2 = (Bitmap)Image.FromFile(greyImagePath);

            List <PixelYUV> realData = GenerateYUVPCRDataForGreyImage(bmp2);

            CImage greyImage = new CImage(bmp2.Width, bmp2.Height, 24);

            bmp2.Dispose();

            IDataView realDataView = mlContext.Data.LoadFromEnumerable(realData);

            Console.WriteLine("Predicting U Channel values");
            float[] uData = MLFunctions.MakeColorPredictions(mlContext, u_model, realDataView);

            Console.WriteLine("Predicting V Channel values");
            float[] vData = MLFunctions.MakeColorPredictions(mlContext, v_model, realDataView);

            Common.Utilities.SetProgress(progressReporter, 75);

            Console.WriteLine("Populating RGB Values");
            int pixelColorIndex = -1;

            for (int y3 = 0; y3 < greyImage.Height; y3++)
            {
                for (int x3 = 0; x3 < greyImage.Width; x3++)
                {
                    pixelColorIndex += 1;
                    int i3 = (x3 + (greyImage.Width * y3));

                    // Convert our YUV values to RGB
                    var rgb = new YUV(
                        realData[pixelColorIndex].Y,
                        Convert.ToDouble(uData[pixelColorIndex]),
                        Convert.ToDouble(vData[pixelColorIndex])
                        ).ToRGB();

                    greyImage.Grid[3 * i3 + 2] = rgb.R;
                    greyImage.Grid[3 * i3 + 1] = rgb.G;
                    greyImage.Grid[3 * i3 + 0] = rgb.B;
                }
            }

            /*********************/
            /* Outputing results */
            /*********************/
            Console.WriteLine("Saving Colorized Image");
            Bitmap greyColored = greyImage.ToBitmap();

            greyColored.Save(destinationImagePath);
            greyColored.Dispose();

            Common.Utilities.SetProgress(progressReporter, 100);
        }
Beispiel #3
0
        public iTemplate(Bitmap myImage)
        {
            //BitmapFilter.GrayScale(myImage);
            BitmapFilter.FixHistogram(ref myImage);
            //   myImage.Save("FixedHistogram.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            BitmapFilter.Binarize(ref myImage, 110);
            //    myImage.Save("Binarized.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            //BitmapFilter.GaussianBlur(myImage, 1);


            /******************************************************/
            CImage cimage = new CImage(myImage);
            float  scale  = 1f / (float)Math.Sqrt(cimage.Width * cimage.Height);

            ComplexF[] data = cimage.Data;

            int offset = 0;

            for (int y = 0; y < cimage.Height; y++)
            {
                for (int x = 0; x < cimage.Width; x++)
                {
                    if (((x + y) & 0x1) != 0)
                    {
                        data[offset] *= -1;
                    }
                    offset++;
                }
            }

            Fourier.FFT2(data, cimage.Width, cimage.Height, FourierDirection.Forward);

            cimage.FrequencySpace = true;

            for (int i = 0; i < data.Length; i++)
            {
                data[i] *= scale;
            }

            myImage = cimage.ToBitmap();

            //       myImage.Save("FFT.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            //pictureBoxFiltred.Size = new System.Drawing.Size(myImage.Width, myImage.Height);
            /******************************************************/


            Bitmap zerImage = new Bitmap(zerSize, zerSize);
            int    ow, oh;

            ow = myImage.Width / 2 - zerSize / 2;
            oh = myImage.Height / 2 - zerSize / 2;

            for (int y = 0; y < zerSize; y++)
            {
                for (int x = 0; x < zerSize; x++)
                {
                    zerImage.SetPixel(x, y, myImage.GetPixel(x + ow, y + oh));
                }
            }

            ZernikeDesc zernikeDesc = new ZernikeDesc(zerImage);

            zerCoefficients = zernikeDesc.Process();
        }