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); }
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(); }