public static FastBitmap Dylatacja(FastBitmap bmp, Spojnosc spojnosc) { int i, j, pam; int[,] dilate = new int[bmp.Width, bmp.Height]; int[,] tab = new int[bmp.Width, bmp.Height]; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { tab[x, y] = bmp[x, y].G; } } for (i = 1; i < bmp.Height - 1; i++) { for (j = 1; j < bmp.Width - 1; j++) { pam = tab[j, i]; if (spojnosc == Spojnosc.kwadrat) { if (pam <= tab[j + 1, i]) { pam = tab[j + 1, i]; } if (pam <= tab[j + 1, i + 1]) { pam = tab[j + 1, i + 1]; } if (pam <= tab[j, i + 1]) { pam = tab[j, i + 1]; } if (pam <= tab[j - 1, i + 1]) { pam = tab[j - 1, i + 1]; } if (pam <= tab[j - 1, i]) { pam = tab[j - 1, i]; } if (pam <= tab[j - 1, i - 1]) { pam = tab[j - 1, i - 1]; } if (pam <= tab[j, i - 1]) { pam = tab[j, i - 1]; } if (pam <= tab[j + 1, i - 1]) { pam = tab[j + 1, i - 1]; } } else if (spojnosc == Spojnosc.romb) { if (pam <= tab[j + 1, i]) { pam = tab[j + 1, i]; } if (pam <= tab[j, i + 1]) { pam = tab[j, i + 1]; } if (pam <= tab[j - 1, i]) { pam = tab[j - 1, i]; } if (pam <= tab[j, i - 1]) { pam = tab[j, i - 1]; } } dilate[j, i] = pam; } } for (i = 0; i < bmp.Height; i++) { for (j = 0; j < bmp.Width; j++) { byte color = (byte)dilate[j, i]; bmp[j, i] = Color.FromArgb(255, color, color, color); } } return(bmp); }
public static Bitmap DilateAndErodeFilter(Bitmap sourceBitmap, int matrixSize, MorphologyType morphType, Spojnosc spojnoscType, bool applyBlue = true, bool applyGreen = true, bool applyRed = true) { BitmapData sourceData = sourceBitmap.LockBits(new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height]; byte[] resultBuffer = new byte[sourceData.Stride * sourceData.Height]; Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length); sourceBitmap.UnlockBits(sourceData); int filterOffset = (matrixSize - 1) / 2; int calcOffset = 0; int byteOffset = 0; byte blue = 0; byte green = 0; byte red = 0; byte morphResetValue = 0; if (morphType == MorphologyType.Erosion) { morphResetValue = 255; } for (int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++) { for (int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++) { byteOffset = offsetY * sourceData.Stride + offsetX * 4; blue = morphResetValue; green = morphResetValue; red = morphResetValue; if (morphType == MorphologyType.Dilatation) { for (int filterY = -filterOffset; filterY <= filterOffset; filterY++) { for (int filterX = -filterOffset; filterX <= filterOffset; filterX++) { if (spojnoscType == Spojnosc.Czterospojne) { if (filterY == 0 || filterX == 0) { calcOffset = byteOffset + (filterX * 4) + (filterY * sourceData.Stride); if (pixelBuffer[calcOffset] > blue) { blue = pixelBuffer[calcOffset]; } if (pixelBuffer[calcOffset + 1] > green) { green = pixelBuffer[calcOffset + 1]; } if (pixelBuffer[calcOffset + 2] > red) { red = pixelBuffer[calcOffset + 2]; } } } else if (spojnoscType == Spojnosc.Osmiospojne) { calcOffset = byteOffset + (filterX * 4) + (filterY * sourceData.Stride); if (pixelBuffer[calcOffset] > blue) { blue = pixelBuffer[calcOffset]; } if (pixelBuffer[calcOffset + 1] > green) { green = pixelBuffer[calcOffset + 1]; } if (pixelBuffer[calcOffset + 2] > red) { red = pixelBuffer[calcOffset + 2]; } } } } } else if (morphType == MorphologyType.Erosion) { for (int filterY = -filterOffset; filterY <= filterOffset; filterY++) { for (int filterX = -filterOffset; filterX <= filterOffset; filterX++) { if (spojnoscType == Spojnosc.Czterospojne) { if (filterY == 0 || filterX == 0) { calcOffset = byteOffset + (filterX * 4) + (filterY * sourceData.Stride); if (pixelBuffer[calcOffset] < blue) { blue = pixelBuffer[calcOffset]; } if (pixelBuffer[calcOffset + 1] < green) { green = pixelBuffer[calcOffset + 1]; } if (pixelBuffer[calcOffset + 2] < red) { red = pixelBuffer[calcOffset + 2]; } } } else if (spojnoscType == Spojnosc.Osmiospojne) { calcOffset = byteOffset + (filterX * 4) + (filterY * sourceData.Stride); if (pixelBuffer[calcOffset] < blue) { blue = pixelBuffer[calcOffset]; } if (pixelBuffer[calcOffset + 1] < green) { green = pixelBuffer[calcOffset + 1]; } if (pixelBuffer[calcOffset + 2] < red) { red = pixelBuffer[calcOffset + 2]; } } } } } if (applyBlue == false) { blue = pixelBuffer[byteOffset]; } if (applyGreen == false) { green = pixelBuffer[byteOffset + 1]; } if (applyRed == false) { red = pixelBuffer[byteOffset + 2]; } resultBuffer[byteOffset] = blue; resultBuffer[byteOffset + 1] = green; resultBuffer[byteOffset + 2] = red; resultBuffer[byteOffset + 3] = 255; } } Bitmap resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height); BitmapData resultData = resultBitmap.LockBits(new Rectangle(0, 0, resultBitmap.Width, resultBitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); Marshal.Copy(resultBuffer, 0, resultData.Scan0, resultBuffer.Length); resultBitmap.UnlockBits(resultData); return(resultBitmap); }
public static FastBitmap Dylatacja(FastBitmap bmp, Spojnosc spojnosc) { int i, j, pam; int[,] dilate = new int[bmp.Width, bmp.Height]; int[,] tab = new int[bmp.Width, bmp.Height]; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { tab[x, y] = bmp[x, y].R; } } for (i = 1; i < bmp.Height - 1; i++) { for (j = 1; j < bmp.Width - 1; j++) { pam = tab[j, i]; if (spojnosc == Spojnosc.Osmiospojne) { if (pam <= tab[j + 1, i]) { pam = tab[j + 1, i]; } if (pam <= tab[j + 1, i + 1]) { pam = tab[j + 1, i + 1]; } if (pam <= tab[j, i + 1]) { pam = tab[j, i + 1]; } if (pam <= tab[j - 1, i + 1]) { pam = tab[j - 1, i + 1]; } if (pam <= tab[j - 1, i]) { pam = tab[j - 1, i]; } if (pam <= tab[j - 1, i - 1]) { pam = tab[j - 1, i - 1]; } if (pam <= tab[j, i - 1]) { pam = tab[j, i - 1]; } if (pam <= tab[j + 1, i - 1]) { pam = tab[j + 1, i - 1]; } } else if (spojnosc == Spojnosc.Czterospojne) { if (pam <= tab[j + 1, i]) { pam = tab[j + 1, i]; } if (pam <= tab[j, i + 1]) { pam = tab[j, i + 1]; } if (pam <= tab[j - 1, i]) { pam = tab[j - 1, i]; } if (pam <= tab[j, i - 1]) { pam = tab[j, i - 1]; } } dilate[j, i] = pam; } } for (i = 0; i < bmp.Height; i++) { for (j = 0; j < bmp.Width; j++) { bmp[j, i] = Color.FromArgb(dilate[j, i], dilate[j, i], dilate[j, i]); } } return(bmp); }
public static Bitmap CloseMorphologyFilter(Bitmap sourceBitmap, int matrixSize, Spojnosc spojnosc, bool applyBlue = true, bool applyGreen = true, bool applyRed = true) { Bitmap resultBitmap = DilateAndErodeFilter(sourceBitmap, matrixSize, MorphologyType.Dilatation, spojnosc, applyBlue, applyGreen, applyRed); resultBitmap = DilateAndErodeFilter(sourceBitmap, matrixSize, MorphologyType.Erosion, spojnosc, applyBlue, applyGreen, applyRed); return(resultBitmap); }