public static BitmapImage GetBinaryImage(BitmapImage sourceBitmap) { int height = sourceBitmap.PixelHeight; int width = sourceBitmap.PixelWidth; var brightnessesNumber = new int[256]; int pixelsNumber = height*width; Bitmap bitmap = BitmapImage2Bitmap(sourceBitmap); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { var hsv = new HSV(bitmap.GetPixel(j, i)); brightnessesNumber[hsv.Value]++; } } int treshhold = brightnessesNumber.Select((t, i) => i*t).Sum()/pixelsNumber; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { var hsv = new HSV(bitmap.GetPixel(j, i)); bitmap.SetPixel(j, i, (new HSV(0, 0, hsv.Value >= treshhold ? (byte) 255 : (byte) 0)).ToColor()); } } return Bitmap2BitmapImage(bitmap); }
public static BitmapImage GetGreyImage(BitmapImage sourceBitmap) { int height = sourceBitmap.PixelHeight; int width = sourceBitmap.PixelWidth; Bitmap bitmap = BitmapImage2Bitmap(sourceBitmap); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { var hsv = new HSV(bitmap.GetPixel(j, i)); bitmap.SetPixel(j, i, (new HSV(0, 0, hsv.Value)).ToColor()); } } return Bitmap2BitmapImage(bitmap); }
public RGB(HSV hsv) { byte Hi = (byte) (hsv.Hue/60); byte Vmin = (byte) ((100 - hsv.Saturation)*hsv.Value/100); byte a = (byte) ((hsv.Value - Vmin)*(hsv.Hue%60)/60); byte Vinc = (byte) (Vmin + a); byte Vdec = (byte) (hsv.Value - a); switch (Hi) { case 0: Red = hsv.Value; Green = Vinc; Blue = Vmin; break; case 1: Red = Vdec; Green = hsv.Value; Blue = Vmin; break; case 2: Red = Vmin; Green = hsv.Value; Blue = Vinc; break; case 3: Red = Vmin; Green = Vdec; Blue = hsv.Value; break; case 4: Red = Vinc; Green = Vmin; Blue = hsv.Value; break; case 5: Red = hsv.Value; Green = Vmin; Blue = Vdec; break; default: throw new ApplicationException("Can'tconvert from HSV to RGB"); } }
private static int[,] GetPixelValueArray(Bitmap bitmap) { var pixels = new int[bitmap.Width, bitmap.Height]; for (int y = 0; y < (int)bitmap.Height; y++) { for (int x = 0; x < (int)bitmap.Width; x++) { pixels[x, y] = new HSV(bitmap.GetPixel(x, y)).Value; } } return pixels; }
private int[,] GetPixelValuesArray(Bitmap bitmap) { var pixels = new int[_width, _height]; for (int y = 0; y < _height; y++) { for (int x = 0; x < _width; x++) { pixels[x, y] = new HSV(bitmap.GetPixel(x, y)).Value; } } return pixels; }
private BitmapImage GetBinaryImage() { var brightnessesNumber = new int[256]; Color[,] pixelsArray= GetPixelArray(BitmapImage2Bitmap(GreyImage)); for (int y = 0; y < _height; y++) { for (int x = 0; x < _width; x++) { var hsv = new HSV(pixelsArray[x, y]); brightnessesNumber[hsv.Value]++; } } if(_needCalculateTreshold) { _treshold = brightnessesNumber.Select((t, i) => i*t).Sum()/_pixelsNumber; } for (int y = 0; y < _height; y++) { for (int x = 0; x < _width; x++) { var hsv = new HSV(pixelsArray[x, y]); pixelsArray[x, y] = (new HSV(0, 0, !IsInverted ? hsv.Value >= _treshold ? (byte) 255 : (byte) 0 : hsv.Value >= _treshold ? (byte) 0 : (byte) 255)).ToColor(); } } return Bitmap2BitmapImage(GetBitmapFromPixelsArray(pixelsArray)); }