private void FilterChromacity(YCrCbImage yCrCbImage) { for (int i = 0; i < yCrCbImage.Width; i++) { for (int j = 0; j < yCrCbImage.Height; j++) { var crs = new List <int>(); var cbs = new List <int>(); for (int x = i - WINDOW; x <= i + WINDOW; x++) { for (int y = j - WINDOW; y <= j + WINDOW; y++) { if (x >= 0 && x < yCrCbImage.Width && y >= 0 && y < yCrCbImage.Height) { crs.Add(yCrCbImage.Cr[x, y]); cbs.Add(yCrCbImage.Cb[x, y]); } } } yCrCbImage.Cr[i, j] = Median(crs); yCrCbImage.Cb[i, j] = Median(cbs); } } }
private void ConvertToBitmap(YCrCbImage yCrCbImage, Bitmap result) { for (int i = 0; i < result.Width; i++) { for (int j = 0; j < result.Height; j++) { var newR = (int)(yCrCbImage.Y[i, j] + 1.402f * (yCrCbImage.Cr[i, j] - 128.0f)); var newG = (int)(yCrCbImage.Y[i, j] - 0.34414f * (yCrCbImage.Cb[i, j] - 128.0f) - 0.71414f * (yCrCbImage.Cr[i, j] - 128.0f)); var newB = (int)(yCrCbImage.Y[i, j] + 1.772f * (yCrCbImage.Cb[i, j] - 128.0f)); newR = Math.Max(0, newR); newG = Math.Max(0, newG); newB = Math.Max(0, newB); newR = Math.Min(255, newR); newG = Math.Min(255, newG); newB = Math.Min(255, newB); var newPixel = Color.FromArgb(newR, newG, newB); result.SetPixel(i, j, newPixel); } } }
private YCrCbImage ConvertToYCrCbImage(Bitmap image) { var result = new YCrCbImage(); result.Width = image.Width; result.Height = image.Height; result.Y = new int[image.Width, image.Height]; result.Cr = new int[image.Width, image.Height]; result.Cb = new int[image.Width, image.Height]; for (int i = 0; i < image.Width; i++) { for (int j = 0; j < image.Height; j++) { var pixel = image.GetPixel(i, j); result.Y[i, j] = (int)(0.299f * pixel.R + 0.587f * pixel.G + 0.114f * pixel.B); result.Cr[i, j] = (int)(128.0f + 0.5f * pixel.R - 0.418688f * pixel.G - 0.081312f * pixel.B); result.Cb[i, j] = (int)(128.0f - 0.168736 * pixel.R - 0.331264f * pixel.G + 0.5f * pixel.B); } } return(result); }