private void ignoreEdgesRadioButton_CheckedChanged(object sender, EventArgs e) { edgeProcessingMethod = EdgeProcessingMethod.IgnoreEdges; }
public Bitmap KirschFilter(Bitmap originalImage, ScalingMethod scalingMethod = ScalingMethod.Cutting, EdgeProcessingMethod edgeProcessingMethod = EdgeProcessingMethod.UseExistingPixels) { if (originalImage.PixelFormat != PixelFormat.Format8bppIndexed) { throw new NotSupportedException("Only 8bppIndexed bitmap are supported"); } int[,] nResult; int[,] neResult; int[,] eResult; int[,] seResult; int[,] sResult; int[,] swResult; int[,] wResult; int[,] nwResult; switch (edgeProcessingMethod) { case EdgeProcessingMethod.DuplicateEdges: nResult = ApplyMaskDuplicatingEdges(originalImage, Mask.KirschN); neResult = ApplyMaskDuplicatingEdges(originalImage, Mask.KirschNE); eResult = ApplyMaskDuplicatingEdges(originalImage, Mask.KirschE); seResult = ApplyMaskDuplicatingEdges(originalImage, Mask.KirschSE); sResult = ApplyMaskDuplicatingEdges(originalImage, Mask.KirschS); swResult = ApplyMaskDuplicatingEdges(originalImage, Mask.KirschSW); wResult = ApplyMaskDuplicatingEdges(originalImage, Mask.KirschW); nwResult = ApplyMaskDuplicatingEdges(originalImage, Mask.KirschNW); break; case EdgeProcessingMethod.IgnoreEdges: nResult = ApplyMaskIgnoringEdges(originalImage, Mask.KirschN); neResult = ApplyMaskIgnoringEdges(originalImage, Mask.KirschNE); eResult = ApplyMaskIgnoringEdges(originalImage, Mask.KirschE); seResult = ApplyMaskIgnoringEdges(originalImage, Mask.KirschSE); sResult = ApplyMaskIgnoringEdges(originalImage, Mask.KirschS); swResult = ApplyMaskIgnoringEdges(originalImage, Mask.KirschSW); wResult = ApplyMaskIgnoringEdges(originalImage, Mask.KirschW); nwResult = ApplyMaskIgnoringEdges(originalImage, Mask.KirschNW); break; case EdgeProcessingMethod.UseExistingPixels: nResult = ApplyMaskUsingExistingNeighbourhood(originalImage, Mask.KirschN); neResult = ApplyMaskUsingExistingNeighbourhood(originalImage, Mask.KirschNE); eResult = ApplyMaskUsingExistingNeighbourhood(originalImage, Mask.KirschE); seResult = ApplyMaskUsingExistingNeighbourhood(originalImage, Mask.KirschSE); sResult = ApplyMaskUsingExistingNeighbourhood(originalImage, Mask.KirschS); swResult = ApplyMaskUsingExistingNeighbourhood(originalImage, Mask.KirschSW); wResult = ApplyMaskUsingExistingNeighbourhood(originalImage, Mask.KirschW); nwResult = ApplyMaskUsingExistingNeighbourhood(originalImage, Mask.KirschNW); break; default: throw new ArgumentException("Unknown edge processing method"); } int width = nResult.GetLength(1); int height = nResult.GetLength(0); int[,] result = new int[height, width]; int[] buf = new int[8]; for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { buf[0] = nResult[i, j]; buf[1] = neResult[i, j]; buf[2] = eResult[i, j]; buf[3] = seResult[i, j]; buf[4] = eResult[i, j]; buf[5] = swResult[i, j]; buf[6] = wResult[i, j]; buf[7] = nwResult[i, j]; result[i, j] = buf.Max(); } } byte[,] rescaled; switch (scalingMethod) { case ScalingMethod.RangeRescale: rescaled = RescaleStretch(result); break; case ScalingMethod.Cutting: rescaled = RescaleCut(result); break; case ScalingMethod.Trivalent: rescaled = RescaleTrivalent(result); break; default: throw new ArgumentException("Unknown rescaling method"); } Bitmap finalImage = new Bitmap(width, height, originalImage.PixelFormat); ColorPalette finalPalette = finalImage.Palette; for (int i = 0; i < 256; ++i) { finalPalette.Entries[i] = Color.FromArgb(i, i, i); } finalImage.Palette = finalPalette; BitmapData finalBitmapData = finalImage.LockBits( new Rectangle(0, 0, finalImage.Width, finalImage.Height), ImageLockMode.ReadOnly, finalImage.PixelFormat); byte *finalBitmapPointer = (byte *)finalBitmapData.Scan0.ToPointer(); byte finalBitmapRemain = (byte)(finalBitmapData.Stride - finalBitmapData.Width); for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { finalBitmapPointer[0] = rescaled[i, j]; ++finalBitmapPointer; } finalBitmapPointer += finalBitmapRemain; } finalImage.UnlockBits(finalBitmapData); return(finalImage); }
private void duplicateRadioButton_CheckedChanged(object sender, EventArgs e) { edgeProcessingMethod = EdgeProcessingMethod.DuplicateEdges; }
private void useExistingRadioButton_CheckedChanged(object sender, EventArgs e) { edgeProcessingMethod = EdgeProcessingMethod.UseExistingPixels; }
public Bitmap ApplyFilter(Bitmap originalImage, int[,] maskX, int[,] maskY, ScalingMethod scalingMethod = ScalingMethod.Cutting, EdgeProcessingMethod edgeProcessingMethod = EdgeProcessingMethod.UseExistingPixels) { if (originalImage.PixelFormat != PixelFormat.Format8bppIndexed) { throw new NotSupportedException("Only 8bppIndexed bitmap are supported"); } int[,] xResult; int[,] yResult; switch (edgeProcessingMethod) { case EdgeProcessingMethod.DuplicateEdges: xResult = ApplyMaskDuplicatingEdges(originalImage, maskX); yResult = ApplyMaskDuplicatingEdges(originalImage, maskY); break; case EdgeProcessingMethod.IgnoreEdges: xResult = ApplyMaskIgnoringEdges(originalImage, maskX); yResult = ApplyMaskIgnoringEdges(originalImage, maskY); break; case EdgeProcessingMethod.UseExistingPixels: xResult = ApplyMaskUsingExistingNeighbourhood(originalImage, maskX); yResult = ApplyMaskUsingExistingNeighbourhood(originalImage, maskY); break; default: throw new ArgumentException("Unknown edge processing method"); } int width = xResult.GetLength(1); int height = xResult.GetLength(0); int[,] gradient = new int[height, width]; for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { gradient[i, j] = Math.Abs(xResult[i, j]) + Math.Abs(yResult[i, j]); } } byte[,] rescaled; switch (scalingMethod) { case ScalingMethod.RangeRescale: rescaled = RescaleStretch(gradient); break; case ScalingMethod.Cutting: rescaled = RescaleCut(gradient); break; case ScalingMethod.Trivalent: rescaled = RescaleTrivalent(gradient); break; default: throw new ArgumentException("Unknown rescaling method"); } Bitmap finalImage = new Bitmap(width, height, originalImage.PixelFormat); ColorPalette finalPalette = finalImage.Palette; for (int i = 0; i < 256; ++i) { finalPalette.Entries[i] = Color.FromArgb(i, i, i); } finalImage.Palette = finalPalette; BitmapData finalBitmapData = finalImage.LockBits( new Rectangle(0, 0, finalImage.Width, finalImage.Height), ImageLockMode.ReadOnly, finalImage.PixelFormat); byte *finalBitmapPointer = (byte *)finalBitmapData.Scan0.ToPointer(); byte finalBitmapRemain = (byte)(finalBitmapData.Stride - finalBitmapData.Width); for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { finalBitmapPointer[0] = rescaled[i, j]; ++finalBitmapPointer; } finalBitmapPointer += finalBitmapRemain; } finalImage.UnlockBits(finalBitmapData); return(finalImage); }
public Bitmap ApplyFilter(Bitmap originalImage, int[,] mask, ScalingMethod scalingMethod = ScalingMethod.Cutting, EdgeProcessingMethod edgeProcessingMethod = EdgeProcessingMethod.UseExistingPixels) { if (originalImage.PixelFormat != PixelFormat.Format8bppIndexed) { throw new NotSupportedException("Only 8bppIndexed bitmap are supported"); } switch (edgeProcessingMethod) { case EdgeProcessingMethod.DuplicateEdges: return(ApplyMaskDuplicatingEdges(originalImage, mask, scalingMethod)); case EdgeProcessingMethod.IgnoreEdges: return(ApplyMaskIgnoringEdges(originalImage, mask, scalingMethod)); case EdgeProcessingMethod.UseExistingPixels: return(ApplyMaskUsingExistingNeighbourhood(originalImage, mask, scalingMethod)); default: throw new ArgumentException("Unknown edge processing method"); } }
public Bitmap ApplyFilter(Bitmap originalImage, int maskWidth, int maskHeight, EdgeProcessingMethod edgeProcessingMethod = EdgeProcessingMethod.UseExistingPixels) { if (originalImage.PixelFormat != PixelFormat.Format8bppIndexed) { throw new NotSupportedException("Only 8bppIndexed bitmap are supported"); } switch (edgeProcessingMethod) { case EdgeProcessingMethod.DuplicateEdges: return(ApplyMedianFilterDuplicatingEdges(originalImage, maskWidth, maskHeight, 1)); case EdgeProcessingMethod.IgnoreEdges: return(ApplyMedianFilterIgnoringEdges(originalImage, maskWidth, maskHeight, 1));; case EdgeProcessingMethod.UseExistingPixels: return(ApplyMedianFilterUsingExistingNeighbourhood(originalImage, maskWidth, maskHeight, 1));; default: throw new ArgumentException("Unknown edge processing method"); } }