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 bool Equals(YUV yuv) { return((Y == yuv.Y) && (U == yuv.U) && (V == yuv.V)); }