public static Bitmap RectangleMorph(Bitmap _b, Point _E, Point _F, Point _G, Point _H) // gradation diff { Point E = new Point(_b.Width * _E.X, _b.Height * _E.Y); Point F = new Point(_b.Width * _F.X, _b.Height * _F.Y); Point G = new Point(_b.Width * _G.X, _b.Height * _G.Y); Point H = new Point(_b.Width * _H.X, _b.Height * _H.Y); int res_w = Math.Max(F.X - E.X, G.X - H.X); int res_h = Math.Max(H.Y - E.Y, G.Y - F.Y); Bitmap result = new Bitmap(res_w, res_h); XBitmap result_xbmp = new XBitmap(result); for (int i = 0; i < result.Width; i++) { for (int j = 0; j < result.Height; j++) { int new_width = j * ((G.X - H.X) - (F.X - E.X)) / H.Y - E.Y + F.X - E.X; int new_height = i * ((G.Y - F.Y) - (H.Y - E.Y)) / F.X - E.X + H.Y - E.Y; int x1 = i * _b.Width / new_width; int y1 = j * _b.Height / new_height; Color px = (y1 >= _b.Height || x1 >= _b.Width || y1 < 0 || x1 < 0) ? Color.FromArgb(0, 0, 0) : _b.GetPixel(x1, y1); result_xbmp.setPixel(i, j, px); } } return(result_xbmp.getBitmap()); }
public static Bitmap filterColor(Bitmap _b, Color _c, Color _t, Color _mark) // gradation diff { Bitmap result = copy(_b); XBitmap framedImage_xbmp = new XBitmap(result); for (int i = 0; i < result.Width; i++) { for (int j = 0; j < result.Height; j++) { Color px = framedImage_xbmp.getPixel(i, j); bool isWarnaKulit = (px.R > _c.R - _t.R && px.R < _c.R + _t.R) && (px.G > _c.G - _t.G && px.G < _c.G + _t.G) && (px.B > _c.B - _t.B && px.B < _c.B + _t.B); int maxdr = isWarnaKulit ? _mark.R : 0; int maxdg = isWarnaKulit ? _mark.G : 0; int maxdb = isWarnaKulit ? _mark.B : 0; if (isWarnaKulit) { framedImage_xbmp.setPixel(i, j, _mark); } else { framedImage_xbmp.setPixel(i, j, Color.FromArgb(0, 0, 0)); } } } return(framedImage_xbmp.getBitmap()); }
public static Bitmap processDegreeTwo(Bitmap _b, int _mode) { XBitmap temp = new XBitmap(addMirroredFrame(_b)); Bitmap output = new Bitmap(_b.Width, _b.Height); double[,] convmatrix; switch (_mode) { case 0: convmatrix = (double[, ])mat2degree["prewitt"].Clone(); break; case 1: convmatrix = (double[, ])mat2degree["robinson"].Clone(); break; case 2: convmatrix = (double[, ])mat2degree["kirsch"].Clone(); break; default: convmatrix = (double[, ])mat2degree["prewitt"].Clone(); break; } for (int i = 1; i < temp.Width - 1; i++) { // iterasi for (int j = 1; j < temp.Height - 1; j++) { double N = XImage.dotProduct(temp, convmatrix, i, j); // North convmatrix = (double[, ])rotateMatrix(convmatrix).Clone(); double NE = XImage.dotProduct(temp, convmatrix, i, j); // North-East convmatrix = (double[, ])rotateMatrix(convmatrix).Clone(); double E = XImage.dotProduct(temp, convmatrix, i, j); // East convmatrix = (double[, ])rotateMatrix(convmatrix).Clone(); double SE = XImage.dotProduct(temp, convmatrix, i, j); // South-East convmatrix = (double[, ])rotateMatrix(convmatrix).Clone(); double S = XImage.dotProduct(temp, convmatrix, i, j); // South convmatrix = (double[, ])rotateMatrix(convmatrix).Clone(); double SW = XImage.dotProduct(temp, convmatrix, i, j); // South-West convmatrix = (double[, ])rotateMatrix(convmatrix).Clone(); double W = XImage.dotProduct(temp, convmatrix, i, j); // West convmatrix = (double[, ])rotateMatrix(convmatrix).Clone(); double NW = XImage.dotProduct(temp, convmatrix, i, j); // North-West int dis = (int)distance(N, distance(NE, distance(E, distance(SE, distance(S, distance(SW, distance(W, NW))))))); int jarak = dis > 255 ? 255 : dis; output.SetPixel(i - 1, j - 1, Color.FromArgb(jarak, jarak, jarak)); } } return(output); }
private static double dotProduct(XBitmap _bmp, double[,] _convmatrix, int i, int j) // putar matrix 45 drj, 8x jadi kembali ke asal, untuk derajat 2 { double res = (double)_bmp.getPixel(i - 1, j - 1).R *_convmatrix[0, 0] + (double)_bmp.getPixel(i - 1, j).R *_convmatrix[0, 1] + (double)_bmp.getPixel(i - 1, j + 1).R *_convmatrix[0, 2] + (double)_bmp.getPixel(i, j - 1).R *_convmatrix[1, 0] + (double)_bmp.getPixel(i, j).R *_convmatrix[1, 1] + (double)_bmp.getPixel(i, j + 1).R *_convmatrix[1, 2] + (double)_bmp.getPixel(i + 1, j - 1).R *_convmatrix[2, 0] + (double)_bmp.getPixel(i + 1, j).R *_convmatrix[2, 1] + (double)_bmp.getPixel(i + 1, j + 1).R *_convmatrix[2, 2]; return(res); }
public static Bitmap processDegreeOne(Bitmap _b, int _mode) { XBitmap temp = new XBitmap(addMirroredFrame(_b)); Bitmap output = new Bitmap(_b.Width, _b.Height); double[,] convmatrix; switch (_mode) { case 0: convmatrix = (double[, ])mat1degree["prewitt"].Clone(); break; case 1: convmatrix = (double[, ])mat1degree["sobel"].Clone(); break; case 2: convmatrix = (double[, ])mat1degree["freichen"].Clone(); break; case 3: convmatrix = (double[, ])mat1degree["robert"].Clone(); break; case 4: convmatrix = (double[, ])mat1degree["kayyali"].Clone(); break; default: convmatrix = (double[, ])mat1degree["prewitt"].Clone(); break; } for (int i = 1; i < temp.Width - 1; i++) { // iterasi for (int j = 1; j < temp.Height - 1; j++) { double N = XImage.dotProduct(temp, convmatrix, i, j); // North convmatrix = (double[, ])rotateMatrix(rotateMatrix(convmatrix)).Clone(); double E = XImage.dotProduct(temp, convmatrix, i, j); // East, rotated twice (90 degree); int dis = (int)distance(N, E); int jarak = dis > 255 ? 255 : dis; output.SetPixel(i - 1, j - 1, Color.FromArgb(jarak, jarak, jarak)); } } return(output); }
public static Bitmap grad2(Bitmap _b, int _radius, Color _mark, int _distance) // gradation diff //Bitmap result = XImage.copy(_b); { int r = _radius; int r2 = _distance / 2 + 1; int limit = (int)(Math.Pow((2 * r + 1), 2) * (0.3)); Bitmap framedImage = addNFrame(_b, Color.FromArgb(0, 0, 0), r); XBitmap xbmp_framedImage = new XBitmap(framedImage); Bitmap result = XImage.getPlainImage(_b, Color.Black); Bitmap framedResultImage = addNFrame(_b, Color.FromArgb(0, 0, 0), r2); for (int i = 0; i < result.Width; i += _distance) { for (int j = 0; j < result.Height; j += _distance) { int x = i + r; int y = j + r; int count = 0; for (int k = x - r; k < x + r; k++) { for (int l = y - r; l < y + r; l++) { Color px = xbmp_framedImage.getPixel(k, l); count += (px.R != 0) ? 1 : 0; } } if (count >= limit) { int d = _distance / 2; for (int k = i - d; k <= i + d; k++) { for (int l = j - d; l <= j + d; l++) { framedResultImage.SetPixel(k + r2, l + r2, _mark); } }/**/ //framedResultImage.SetPixel(i + r2, j + r2, _mark); } } }/**/ return(XImage.removeNFrame(framedResultImage, r2)); }
public static Bitmap edgeDiff2(Bitmap _b) // inversi warna { Bitmap result = copy(_b); Bitmap framedImage = addMirroredFrame(_b); XBitmap framedImage_xbmp = new XBitmap(framedImage); for (int i = 1; i < framedImage.Width - 1; i++) { for (int j = 1; j < framedImage.Height - 1; j++) { Color dc1 = XColor.differrence(framedImage_xbmp.getPixel(i, j - 1), framedImage_xbmp.getPixel(i, j + 1)); Color dc2 = XColor.differrence(framedImage_xbmp.getPixel(i + 1, j), framedImage_xbmp.getPixel(i - 1, j)); int maxdr = Math.Max(dc1.R, dc2.R); int maxdg = Math.Max(dc1.G, dc2.G); int maxdb = Math.Max(dc1.B, dc2.B); result.SetPixel(i - 1, j - 1, Color.FromArgb(maxdr, maxdr, maxdb)); } } return(result); }
public static Bitmap gradDiff4(Bitmap _b) // gradation diff { Bitmap result = copy(_b); Bitmap framedImage = addMirroredFrame(_b); XBitmap framedImage_xbmp = new XBitmap(framedImage); for (int i = 1; i < framedImage.Width - 1; i++) { for (int j = 1; j < framedImage.Height - 1; j++) { Color px = framedImage_xbmp.getPixel(i, j); int thres = 21; bool isWarnaKulit = (px.R > 102 - thres && px.R < 102 + thres) && (px.G > 107 - thres && px.G < 107 + thres) && (px.B > 95 - thres && px.B < 95 + thres); int maxdr = isWarnaKulit ? 255 : 0; int maxdg = isWarnaKulit ? 255 : 0; int maxdb = isWarnaKulit ? 255 : 0; result.SetPixel(i - 1, j - 1, Color.FromArgb(maxdr, maxdr, maxdb)); } } return(result); }
private static int getB(XBitmap image, int x, int y, ref int p2, ref int p4, ref int p6, ref int p8) { int count = 0; //p2 if (isNotLatar(image, x, y - 1)) { p2 = 1; count++; } else p2 = 0; //p3 if (isNotLatar(image, x + 1, y - 1)) count++; //p4 if (isNotLatar(image, x + 1, y)) { p4 = 1; count++; } else p4 = 0; //p5 if (isNotLatar(image, x + 1, y + 1)) count++; //p6 if (isNotLatar(image, x, y + 1)) { p6 = 1; count++; } else p6 = 0; //p7 if (isNotLatar(image, x - 1, y + 1)) count++; //p8 if (isNotLatar(image, x - 1, y)) { p8 = 1; count++; } else p8 = 0; //p9 if (isNotLatar(image, x - 1, y - 1)) count++; return count; }
private static int getA(XBitmap image, int x, int y) { int count = 0; //p2 p3 if (isLatar(image, x, y - 1) && isNotLatar(image, x + 1, y - 1)) count++; //p3 p4 if (isLatar(image, x + 1, y - 1) && isNotLatar(image, x + 1, y)) count++; //p4 p5 if (isLatar(image, x + 1, y) && isNotLatar(image, x + 1, y + 1)) count++; //p5 p6 if (isLatar(image, x + 1, y + 1) && isNotLatar(image, x, y + 1)) count++; //p6 p7 if (isLatar(image, x, y + 1) && isNotLatar(image, x - 1, y + 1)) count++; //p7 p8 if (isLatar(image, x - 1, y + 1) && isNotLatar(image, x - 1, y)) count++; //p8 p9 if (isLatar(image, x - 1, y) && isNotLatar(image, x - 1, y - 1)) count++; //p9 p2 if (isLatar(image, x - 1, y - 1) && isNotLatar(image, x, y - 1)) count++; return count; }
public static Bitmap zhangsuen(Bitmap _b) { Bitmap framedImage = addFrame(_b, latar); XBitmap framedImage_xbmp = new XBitmap(framedImage); List<System.Drawing.Point> pointsToChange = new List<System.Drawing.Point>(); int a, b; int p2 = 0; int p4 = 0; int p6 = 0; int p8 = 0; bool notSkeleton = true; while (notSkeleton) { notSkeleton = false; for (int i = 1; i < _b.Height + 1; i++) { for (int j = 1; j < _b.Width + 1; j++) { if (isNotLatar(framedImage_xbmp, j, i)) { a = getA(framedImage_xbmp, j, i); b = getB(framedImage_xbmp, j, i, ref p2, ref p4, ref p6, ref p8); if (2 <= b && b <= 6 && a == 1 && (p2 * p4 * p6 == 0) && (p4 * p6 * p8 == 0)) { pointsToChange.Add(new System.Drawing.Point(j, i)); notSkeleton = true; } } } } foreach (System.Drawing.Point point in pointsToChange) { framedImage_xbmp.setPixel(point.X, point.Y, latar); } pointsToChange.Clear(); for (int i = 1; i < _b.Height + 1; i++) { for (int j = 1; j < _b.Width + 1; j++) { if (isNotLatar(framedImage_xbmp, j, i)) { a = getA(framedImage_xbmp, j, i); b = getB(framedImage_xbmp, j, i, ref p2, ref p4, ref p6, ref p8); if (2 <= b && b <= 6 && a == 1 && (p2 * p4 * p8 == 0) && (p2 * p6 * p8 == 0)) { pointsToChange.Add(new System.Drawing.Point(j, i)); notSkeleton = true; } } } } foreach (System.Drawing.Point point in pointsToChange) { framedImage_xbmp.setPixel(point.X, point.Y, latar); } pointsToChange.Clear(); } return XImage.removeFrame(framedImage_xbmp.getBitmap()); }
public static Bitmap RectangleMorph(Bitmap _b, System.Drawing.Point _E, System.Drawing.Point _F, System.Drawing.Point _G, System.Drawing.Point _H) { // gradation diff System.Drawing.Point E = new System.Drawing.Point(_b.Width * _E.X, _b.Height * _E.Y); System.Drawing.Point F = new System.Drawing.Point(_b.Width * _F.X, _b.Height * _F.Y); System.Drawing.Point G = new System.Drawing.Point(_b.Width * _G.X, _b.Height * _G.Y); System.Drawing.Point H = new System.Drawing.Point(_b.Width * _H.X, _b.Height * _H.Y); int res_w = Math.Max(F.X - E.X, G.X - H.X); int res_h = Math.Max(H.Y - E.Y, G.Y - F.Y); Bitmap result = new Bitmap(res_w, res_h); XBitmap result_xbmp = new XBitmap(result); for (int i = 0; i < result.Width; i++) { for (int j = 0; j < result.Height; j++) { int new_width = j * ((G.X - H.X) - (F.X - E.X)) / H.Y - E.Y + F.X - E.X; int new_height = i * ((G.Y - F.Y) - (H.Y - E.Y)) / F.X - E.X + H.Y - E.Y; int x1 = i * _b.Width / new_width; int y1 = j * _b.Height / new_height; Color px = (y1 >= _b.Height || x1 >= _b.Width || y1 < 0 || x1 < 0) ? Color.FromArgb(0, 0, 0) : _b.GetPixel(x1, y1); result_xbmp.setPixel(i, j, px); } } return (result_xbmp.getBitmap()); }
public static Bitmap grad2(Bitmap _b, int _radius, Color _mark, int _distance) { // gradation diff //Bitmap result = XImage.copy(_b); int r = _radius; int r2 = _distance/2+1; int limit = (int)(Math.Pow((2*r+1), 2)*(0.3)); Bitmap framedImage = addNFrame(_b, Color.FromArgb(0, 0, 0), r); XBitmap xbmp_framedImage = new XBitmap(framedImage); Bitmap result = XImage.getPlainImage(_b, Color.Black); Bitmap framedResultImage = addNFrame(_b, Color.FromArgb(0, 0, 0), r2); for (int i = 0; i < result.Width; i+=_distance) { for (int j = 0; j < result.Height; j+=_distance) { int x = i+r; int y = j+r; int count = 0; for (int k = x-r; k < x+r; k++) { for (int l = y-r; l < y+r; l++) { Color px = xbmp_framedImage.getPixel(k, l); count += (px.R!=0) ? 1 : 0; } } if (count>=limit) { int d = _distance/2; for (int k = i-d; k <= i+d; k++) { for (int l = j-d; l <= j+d; l++) { framedResultImage.SetPixel(k + r2, l + r2, _mark); } }/**/ //framedResultImage.SetPixel(i + r2, j + r2, _mark); } } }/**/ return XImage.removeNFrame(framedResultImage, r2); }
public static Bitmap gradDiff4(Bitmap _b) { // gradation diff Bitmap result = copy(_b); Bitmap framedImage = addMirroredFrame(_b); XBitmap framedImage_xbmp = new XBitmap(framedImage); for (int i = 1; i < framedImage.Width-1; i++) { for (int j = 1; j < framedImage.Height-1; j++) { Color px = framedImage_xbmp.getPixel(i, j); int thres = 21; bool isWarnaKulit = (px.R>102-thres && px.R<102+thres) && (px.G>107-thres && px.G<107+thres) && (px.B>95-thres && px.B<95+thres); int maxdr = isWarnaKulit ? 255 : 0; int maxdg = isWarnaKulit ? 255 : 0; int maxdb = isWarnaKulit ? 255 : 0; result.SetPixel(i-1, j-1, Color.FromArgb(maxdr, maxdr, maxdb)); } } return result; }
public static Bitmap processDegreeOne(Bitmap _b, int _mode) { XBitmap temp = new XBitmap(addMirroredFrame(_b)); Bitmap output = new Bitmap(_b.Width, _b.Height); double[,] convmatrix; switch (_mode) { case 0: convmatrix = (double[,])mat1degree["prewitt"].Clone(); break; case 1: convmatrix = (double[,])mat1degree["sobel"].Clone(); break; case 2: convmatrix = (double[,])mat1degree["freichen"].Clone(); break; case 3: convmatrix = (double[,])mat1degree["robert"].Clone(); break; case 4: convmatrix = (double[,])mat1degree["kayyali"].Clone(); break; default: convmatrix = (double[,])mat1degree["prewitt"].Clone(); break; } for (int i = 1; i < temp.Width - 1; i++) { // iterasi for (int j = 1; j < temp.Height - 1; j++) { double N = XImage.dotProduct(temp, convmatrix, i, j); // North convmatrix = (double[,])rotateMatrix(rotateMatrix(convmatrix)).Clone(); double E = XImage.dotProduct(temp, convmatrix, i, j); // East, rotated twice (90 degree); int dis = (int)distance(N, E); int jarak = dis > 255 ? 255 : dis; output.SetPixel(i - 1, j - 1, Color.FromArgb(jarak, jarak, jarak)); } } return output; }
public static Bitmap processDegreeTwo(Bitmap _b, int _mode) { XBitmap temp = new XBitmap(addMirroredFrame(_b)); Bitmap output = new Bitmap(_b.Width, _b.Height); double[,] convmatrix; switch (_mode) { case 0: convmatrix = (double[,])mat2degree["prewitt"].Clone(); break; case 1: convmatrix = (double[,])mat2degree["robinson"].Clone(); break; case 2: convmatrix = (double[,])mat2degree["kirsch"].Clone(); break; default: convmatrix = (double[,])mat2degree["prewitt"].Clone(); break; } for (int i = 1; i < temp.Width - 1; i++) { // iterasi for (int j = 1; j < temp.Height - 1; j++) { double N = XImage.dotProduct(temp, convmatrix, i, j); // North convmatrix = (double[,])rotateMatrix(convmatrix).Clone(); double NE = XImage.dotProduct(temp, convmatrix, i, j); // North-East convmatrix = (double[,])rotateMatrix(convmatrix).Clone(); double E = XImage.dotProduct(temp, convmatrix, i, j); // East convmatrix = (double[,])rotateMatrix(convmatrix).Clone(); double SE = XImage.dotProduct(temp, convmatrix, i, j); // South-East convmatrix = (double[,])rotateMatrix(convmatrix).Clone(); double S = XImage.dotProduct(temp, convmatrix, i, j); // South convmatrix = (double[,])rotateMatrix(convmatrix).Clone(); double SW = XImage.dotProduct(temp, convmatrix, i, j); // South-West convmatrix = (double[,])rotateMatrix(convmatrix).Clone(); double W = XImage.dotProduct(temp, convmatrix, i, j); // West convmatrix = (double[,])rotateMatrix(convmatrix).Clone(); double NW = XImage.dotProduct(temp, convmatrix, i, j); // North-West int dis = (int)distance(N, distance(NE, distance(E, distance(SE, distance(S, distance(SW, distance(W, NW))))))); int jarak = dis > 255 ? 255 : dis; output.SetPixel(i - 1, j - 1, Color.FromArgb(jarak, jarak, jarak)); } } return output; }
public static Bitmap filterColor(Bitmap _b, Color _c, Color _t, Color _mark) { // gradation diff Bitmap result = copy(_b); XBitmap framedImage_xbmp = new XBitmap(result); for (int i = 0; i < result.Width; i++) { for (int j = 0; j < result.Height; j++) { Color px = framedImage_xbmp.getPixel(i, j); bool isWarnaKulit = (px.R>_c.R-_t.R && px.R<_c.R+_t.R) && (px.G>_c.G-_t.G && px.G<_c.G+_t.G) && (px.B>_c.B-_t.B && px.B<_c.B+_t.B); int maxdr = isWarnaKulit ? _mark.R : 0; int maxdg = isWarnaKulit ? _mark.G : 0; int maxdb = isWarnaKulit ? _mark.B : 0; if (isWarnaKulit) framedImage_xbmp.setPixel(i, j, _mark); else framedImage_xbmp.setPixel(i, j, Color.FromArgb(0,0,0)); } } return (framedImage_xbmp.getBitmap()); }
private static bool isLatar(XBitmap _b, int i, int j) { Color temp = _b.getPixel(i, j); return XColor.isEqual(temp, latar); }
// putar matrix 45 drj, 8x jadi kembali ke asal, untuk derajat 2 private static double dotProduct(XBitmap _bmp, double[,] _convmatrix, int i, int j) { double res = (double)_bmp.getPixel(i - 1, j - 1).R * _convmatrix[0, 0] + (double)_bmp.getPixel(i - 1, j).R * _convmatrix[0, 1] + (double)_bmp.getPixel(i - 1, j + 1).R * _convmatrix[0, 2] + (double)_bmp.getPixel(i, j - 1).R * _convmatrix[1, 0] + (double)_bmp.getPixel(i, j).R * _convmatrix[1, 1] + (double)_bmp.getPixel(i, j + 1).R * _convmatrix[1, 2] + (double)_bmp.getPixel(i + 1, j - 1).R * _convmatrix[2, 0] + (double)_bmp.getPixel(i + 1, j).R * _convmatrix[2, 1] + (double)_bmp.getPixel(i + 1, j + 1).R * _convmatrix[2, 2]; return res; }
private static bool isNotLatar(XBitmap _b, int i, int j) { return !isLatar(_b, i, j); }
public static Bitmap edgeDiff4(Bitmap _b) { // inversi warna Bitmap result = copy(_b); Bitmap framedImage = addMirroredFrame(_b); XBitmap framedImage_xbmp = new XBitmap(framedImage); for (int i = 1; i < framedImage.Width-1; i++) { for (int j = 1; j < framedImage.Height-1; j++) { Color dc1 = XColor.differrence(framedImage_xbmp.getPixel(i, j - 1), framedImage_xbmp.getPixel(i, j + 1)); Color dc2 = XColor.differrence(framedImage_xbmp.getPixel(i + 1, j - 1), framedImage_xbmp.getPixel(i - 1, j + 1)); Color dc3 = XColor.differrence(framedImage_xbmp.getPixel(i + 1, j), framedImage_xbmp.getPixel(i - 1, j)); Color dc4 = XColor.differrence(framedImage_xbmp.getPixel(i + 1, j + 1), framedImage_xbmp.getPixel(i - 1, j - 1)); int maxdr = Math.Max(dc1.R, Math.Max(dc2.R, Math.Max(dc3.R, dc4.R))); int maxdg = Math.Max(dc1.G, Math.Max(dc2.G, Math.Max(dc3.G, dc4.G))); int maxdb = Math.Max(dc1.B, Math.Max(dc2.B, Math.Max(dc3.B, dc4.B))); result.SetPixel(i-1, j-1, Color.FromArgb(maxdr, maxdr, maxdb)); } } return result; }