private static bool Delete2s(BitmapContext context, int[,] pixels, bool change) { int width = context.Width; int height = context.Height; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (pixels[i, j] == 2) { int weight = CalculateWeight(i, j, context); if (A.Contains(weight)) { pixels[i, j] = 0; PixelHelper.SetWhite(context, i, j); change = true; } else { pixels[i, j] = 1; } } } } return(change); }
public static WriteableBitmap DilateAndErode(WriteableBitmap writeableBitmap, bool?[,] matrix3x3) { int width = writeableBitmap.PixelWidth; int height = writeableBitmap.PixelHeight; WriteableBitmap newImage = writeableBitmap.Clone(); using (BitmapContext newcontext = newImage.GetBitmapContext()) { using (BitmapContext context = writeableBitmap.GetBitmapContext()) { for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { bool exaclyMatch = true; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { bool?matVal = matrix3x3[i, j]; int cX = x - 1 + i; int cY = y - 1 + j; if (cX > 0 && cY > 0 && cX < width && cY < height && matVal != null) { if (!(PixelHelper.IsBlack(context, cX, cY) == matVal)) { exaclyMatch = false; goto ExitLoop; } } } } ExitLoop: if (exaclyMatch) { PixelHelper.SetBlack(newcontext, x, y); } else { PixelHelper.SetWhite(newcontext, x, y); } } } } } return(newImage); }