private Bitmap mosaic(Bitmap bitmap, int effect) { int width = bitmap.Width, height = bitmap.Height, count = effect * effect, offset = (effect / 2) + (effect % 2); Bitmap dstBitmap = new Bitmap(bitmap); byte[,] pix = ImageExtract.getimageArray(bitmap); byte[,] resPix = new byte[3, width *height]; for (int y = offset; y < (height - offset); y += effect) { for (int x = offset; x < (width - offset); x += effect) { //mask int current = x + y * width; int[] sum = { 0, 0, 0 }; for (int my = 0; my < effect; my++) { for (int mx = 0; mx < effect; mx++) { int pos = current + (mx - 1) + ((my - 1) * width); sum[0] += pix[0, pos]; sum[1] += pix[1, pos]; sum[2] += pix[2, pos]; } } sum[0] = (byte)(sum[0] / count); sum[1] = (byte)(sum[1] / count); sum[2] = (byte)(sum[2] / count); for (int my = 0; my < effect; my++) { for (int mx = 0; mx < effect; mx++) { int pos = current + (mx - 1) + ((my - 1) * width); resPix[0, pos] = (byte)sum[0]; resPix[1, pos] = (byte)sum[1]; resPix[2, pos] = (byte)sum[2]; } } } } ImageExtract.writeImageByArray(resPix, dstBitmap); return(dstBitmap); }
private Bitmap horizontal(Bitmap srcBitmap) { Bitmap dstBitmap = new Bitmap(srcBitmap); int width = srcBitmap.Width, height = srcBitmap.Height; byte[,] pixels = ImageExtract.getimageArray(srcBitmap); byte[,] result = new byte[3, width *height]; int x, y, offset, halfOffset, halfHeightOffset, halfHeight = height / 2; int r, g, b; for (y = 0; y < height - (height % 2); y += 2) { for (x = 0; x < width - (width % 2); x++) { offset = y * width; halfOffset = (y / 2) * width; halfHeightOffset = halfHeight * width; r = pixels[ImageExtract.COLOR_R, x + offset + width] + pixels[ImageExtract.COLOR_R, x + offset + width]; result[ImageExtract.COLOR_R, x + halfOffset] = (byte)((r > 255) ? 255 : (r < 0) ? 0 : r); g = pixels[ImageExtract.COLOR_G, x + offset + width] + pixels[ImageExtract.COLOR_G, x + offset + width]; result[ImageExtract.COLOR_G, x + halfOffset] = (byte)((g > 255) ? 255 : (g < 0) ? 0 : g); b = pixels[ImageExtract.COLOR_B, x + offset + width] + pixels[ImageExtract.COLOR_B, x + offset + width]; result[ImageExtract.COLOR_B, x + halfOffset] = (byte)((b > 255) ? 255 : (b < 0) ? 0 : b); r = pixels[ImageExtract.COLOR_R, x + offset] - pixels[ImageExtract.COLOR_R, x + offset + width]; result[ImageExtract.COLOR_R, x + halfOffset + halfHeightOffset] = (byte)((r > 255) ? 255 : (r < 0) ? 0 : r); g = pixels[ImageExtract.COLOR_G, x + offset] - pixels[ImageExtract.COLOR_G, x + offset + width]; result[ImageExtract.COLOR_G, x + halfOffset + halfHeightOffset] = (byte)((g > 255) ? 255 : (g < 0) ? 0 : g); b = pixels[ImageExtract.COLOR_B, x + offset] - pixels[ImageExtract.COLOR_B, x + offset + width]; result[ImageExtract.COLOR_B, x + halfOffset + halfHeightOffset] = (byte)((b > 255) ? 255 : (b < 0) ? 0 : b); } } ImageExtract.writeImageByArray(result, dstBitmap); return(dstBitmap); }
private static Bitmap sobel(Bitmap bitmap) { int width = bitmap.Width, height = bitmap.Height; int w = 3, h = 3; Bitmap dstBitmap = new Bitmap(bitmap); byte[,] pix = ImageExtract.getimageArray(bitmap); byte[,] resPix = new byte[3, width *height]; for (int y = 1; y < (height - 1); y++) { for (int x = 1; x < (width - 1); x++) { //b,g,r int current = x + (y * width); for (int c = 0; c < 3; c++) { //mask byte[] mask = new byte[w * h]; for (int my = 0; my < h; my++) { for (int mx = 0; mx < w; mx++) { int pos = current + (mx - 1) + ((my - 1) * width); mask[mx + my * w] = pix[c, pos]; } } resPix[c, current] = sobelMask33(mask); //resPix[c, current] = pix[c, current]; } } } ImageExtract.writeImageByArray(resPix, dstBitmap); return(dstBitmap); }
private Bitmap vertical(Bitmap srcBitmap) { Bitmap dstBitmap = new Bitmap(srcBitmap); int width = srcBitmap.Width, height = srcBitmap.Height; byte[,] pixels = ImageExtract.getimageArray(srcBitmap); byte[,] result = new byte[3, width *height]; int x, y, offset; int r, g, b, halfWidth = width / 2; for (y = 0; y < height - (height % 2); y++) { for (x = 0; x < width - (width % 2); x += 2) { offset = y * width; r = pixels[ImageExtract.COLOR_R, x + offset] + pixels[ImageExtract.COLOR_R, x + 1 + offset]; result[ImageExtract.COLOR_R, x / 2 + offset] = (byte)((r > 255) ? 255 : (r < 0) ? 0 : r); g = pixels[ImageExtract.COLOR_G, x + offset] + pixels[ImageExtract.COLOR_G, x + 1 + offset]; result[ImageExtract.COLOR_G, x / 2 + offset] = (byte)((g > 255) ? 255 : (g < 0) ? 0 : g); b = pixels[ImageExtract.COLOR_B, x + offset] + pixels[ImageExtract.COLOR_B, x + 1 + offset]; result[ImageExtract.COLOR_B, x / 2 + offset] = (byte)((b > 255) ? 255 : (b < 0) ? 0 : b); r = pixels[ImageExtract.COLOR_R, x + offset] - pixels[ImageExtract.COLOR_R, x + 1 + offset]; result[ImageExtract.COLOR_R, x / 2 + halfWidth + offset] = (byte)((r > 255) ? 255 : (r < 0) ? 0 : r); g = pixels[ImageExtract.COLOR_G, x + offset] - pixels[ImageExtract.COLOR_G, x + 1 + offset]; result[ImageExtract.COLOR_G, x / 2 + halfWidth + offset] = (byte)((g > 255) ? 255 : (g < 0) ? 0 : g); b = pixels[ImageExtract.COLOR_B, x + offset] - pixels[ImageExtract.COLOR_B, x + 1 + offset]; result[ImageExtract.COLOR_B, x / 2 + halfWidth + offset] = (byte)((b > 255) ? 255 : (b < 0) ? 0 : b); } } ImageExtract.writeImageByArray(result, dstBitmap); return(dstBitmap); }
private Bitmap _filter(Bitmap bitmap, int maskWidth, int maskHeight) { byte[,] pix, resPix; int width = bitmap.Width, height = bitmap.Height, pos, count = maskWidth * maskHeight, current; Bitmap dstBitmap = ImageExtract.extract(bitmap, out pix, out resPix); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { pos = x + y * width; if (!ImageProcess.IsFilterOnSide(ref pix, ref resPix, width, height, 1, 1, x, y, pos)) { current = x + y * width; int[] sum = { 0, 0, 0 }; for (int my = 0; my < maskHeight; my++) { for (int mx = 0; mx < maskWidth; mx++) { pos = current + (mx - 1) + ((my - 1) * width); sum[0] += pix[0, pos]; sum[1] += pix[1, pos]; sum[2] += pix[2, pos]; } } resPix[0, current] = (byte)(sum[0] / count); resPix[1, current] = (byte)(sum[1] / count); resPix[2, current] = (byte)(sum[2] / count); } } } ImageExtract.writeImageByArray(resPix, dstBitmap); return(dstBitmap); }