public static FilteredImage GetNormalizedGrayscaleFilteredImage(Bitmap bitmap) { using (Bitmap bmp = new Bitmap(bitmap)) { int height = bitmap.Height; FilteredImageChannel[] channels = new FilteredImageChannel[1]; double[,] channel = new double[height, height]; for (int i = 0; i < bitmap.Height; i++) { for (int j = 0; j < bitmap.Height; j++) { Color color = bitmap.GetPixel(j, i); //redChannel[i, j] = color.R / 255.0; //greenChannel[i, j] = color.G / 255.0; //blueChannel[i, j] = color.B / 255.0; channel[i, j] = (255 - ((color.R + color.G + color.B) / 3)) / 255.0 - 0.5; } } channels[0] = new FilteredImageChannel(height, channel); FilteredImage image = new FilteredImage(1, channels); return(image); } }
//private static RGBPixel[,] GetRGBMatrix(Bitmap image) //{ // int width = image.Width, height = image.Height; // RGBPixel[,] result = new RGBPixel[height, width]; // for (int i = 0; i < height; i++) // { // for (int j = 0; j < width; j++) // { // Color color = image.GetPixel(j, i); // result[i, j] = new RGBPixel(color.R, color.G, color.B); // } // } // return result; //} //private static Bitmap ReconstructFromRGBMatrix(RGBPixel[,] imageMatrix) //{ // int width = imageMatrix.GetLength(1), height = imageMatrix.GetLength(0); // Bitmap image = new Bitmap(width, height); // using (Graphics graph = Graphics.FromImage(image)) // { // Rectangle rectangle = new Rectangle(0, 0, width, height); // graph.FillRectangle(Brushes.White, rectangle); // } // for (int i = 0; i < height; i++) // { // for (int j = 0; j < width; j++) // { // image.SetPixel(j, i, Color.FromArgb((int)imageMatrix[i, j].Red, (int)imageMatrix[i, j].Green, (int)imageMatrix[i, j].Blue)); // } // } // return image; //} //public static Bitmap ReconstructFromNormalizedRGBMatrix(RGBPixel[,] imageMatrix) //{ // return ReconstructFromRGBMatrix(DenormalizeRGBMatrix(imageMatrix)); //} //private static RGBPixel[,] DenormalizeRGBMatrix(RGBPixel[,] imageMatrix) //{ // for (int i = 0; i < imageMatrix.GetLength(0); i++) // { // for (int j = 0; j < imageMatrix.GetLength(1); j++) // { // imageMatrix[i, j] = DenormalizePixel(imageMatrix[i, j]); // } // } // return imageMatrix; //} //private static double DenormalizeValue(double value) //{ // return (int)(value * 255); //} //private static RGBPixel DenormalizePixel(RGBPixel pixel) //{ // return new RGBPixel(DenormalizeValue(pixel.Red), DenormalizeValue(pixel.Green), DenormalizeValue(pixel.Blue)); //} //private static double NormalizeValue(double value) //{ // return value / 255.0; //} //private static RGBPixel NormalizePixel(RGBPixel pixel) //{ // return new RGBPixel(NormalizeValue(pixel.Red), NormalizeValue(pixel.Green), NormalizeValue(pixel.Blue)); //} //private static RGBPixel[,] NormalizeRGBMatrix(RGBPixel[,] imageMatrix) //{ // for(int i = 0; i < imageMatrix.GetLength(0); i++) // { // for(int j = 0; j < imageMatrix.GetLength(1); j++) // { // imageMatrix[i, j] = NormalizePixel(imageMatrix[i,j]); // } // } // return imageMatrix; //} //public static RGBPixel[,] GetNormalizedRGBMatrix(string path) //{ // using (Bitmap bmp = new Bitmap(path)) // { // return NormalizeRGBMatrix(GetRGBMatrix(bmp)); // } //} //public static RGBPixel[,] GetNormalizedRGBMatrix(Bitmap bitmap) //{ // using (Bitmap bmp = new Bitmap(bitmap)) // { // return NormalizeRGBMatrix(GetRGBMatrix(bmp)); // } //} public static FilteredImage GetNormalizedFilteredImage(Bitmap bitmap) { using (Bitmap bmp = new Bitmap(bitmap)) { int height = bitmap.Height; FilteredImageChannel[] channels = new FilteredImageChannel[3]; double[,] redChannel = new double[height, height]; double[,] greenChannel = new double[height, height]; double[,] blueChannel = new double[height, height]; for (int i = 0; i < bitmap.Height; i++) { for (int j = 0; j < bitmap.Height; j++) { Color color = bitmap.GetPixel(j, i); redChannel[i, j] = (255 - color.R) / 255.0 - 0.5; greenChannel[i, j] = (255 - color.G) / 255.0 - 0.5; blueChannel[i, j] = (255 - color.B) / 255.0 - 0.5; //redChannel[i, j] = (255 - color.R) / 255.0; //greenChannel[i, j] = (255 - color.G) / 255.0; //blueChannel[i, j] = (255 - color.B) / 255.0; } } channels[0] = new FilteredImageChannel(height, redChannel); channels[1] = new FilteredImageChannel(height, greenChannel); channels[2] = new FilteredImageChannel(height, blueChannel); FilteredImage image = new FilteredImage(3, channels); return(image); } }