Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void btn_Filter_Click(object sender, EventArgs e)
- {
- String lpf = (String)comboBox1.SelectedItem;
- String hpf = (String)comboBox2.SelectedItem;
- String banding = (String)comboBox3.SelectedItem;
- int[] xt = new int[10];
- objBitmap10 = new Bitmap(objBitmap1);
- for (int x = 1; x < objBitmap1.Width - 1; x++)
- for (int y = 1; y < objBitmap1.Height - 1; y++)
- {
- Color w1 = objBitmap1.GetPixel(x - 1, y - 1);
- Color w2 = objBitmap1.GetPixel(x - 1, y);
- Color w3 = objBitmap1.GetPixel(x - 1, y + 1);
- Color w4 = objBitmap1.GetPixel(x, y - 1);
- Color w5 = objBitmap1.GetPixel(x, y);
- Color w6 = objBitmap1.GetPixel(x, y + 1);
- Color w7 = objBitmap1.GetPixel(x + 1, y - 1);
- Color w8 = objBitmap1.GetPixel(x + 1, y);
- Color w9 = objBitmap1.GetPixel(x + 1, y + 1);
- xt[1] = w1.R; xt[2] = w2.R;
- xt[3] = w3.R; xt[4] = w4.R;
- xt[5] = w5.R; xt[6] = w6.R;
- xt[7] = w7.R; xt[8] = w8.R;
- xt[9] = w9.R;
- int xlpf, xhpf, xbanding;
- switch (lpf)
- {
- case "Filter Rata-Rata":
- xlpf = rataFilter(xt);
- break;
- case "Filter Gaussian":
- xlpf = gaussianFilter(xt);
- break;
- case "Filter Median":
- xlpf = medianFilter(xt);
- break;
- default:
- xlpf = rataFilter(xt);
- break;
- }
- switch (hpf)
- {
- case "Sobel":
- xhpf = sobelFilter(xt);
- break;
- case "Prewitt":
- xhpf = prewittFilter(xt);
- break;
- case "Laplacian":
- xhpf = laplacianFilter(xt);
- break;
- default:
- xhpf = sobelFilter(xt);
- break;
- }
- switch (banding)
- {
- case "2:1":
- xbanding = banding1(xt);
- break;
- case "1:2":
- xbanding = banding2(xt);
- break;
- default:
- xbanding = banding1(xt);
- break;
- }
- int xb = xlpf + xhpf;
- if (xb < 0) xb = -xb;
- if (xb > 255) xb = 255;
- Color wb = Color.FromArgb(xb, xb, xb);
- objBitmap10.SetPixel(x, y, wb);
- }
- pb_Prak11.Image = objBitmap10;
- }
- public int sobelFilter(int[] x)
- {
- int xv = (int)(-x[1] - 2 * x[2] - x[3] + x[7] + 2 * x[8] + x[9]);
- int xh = (int)(x[1] + 2 * x[4] + x[7] - x[3] - 2 * x[6] - x[9]);
- return (xv + xh);
- }
- public int prewittFilter(int[] x)
- {
- int xv = (int)(-x[1] - x[2] - x[3] + x[7] + x[8] + x[9]);
- int xh = (int)(x[1] + x[4] + x[7] - x[3] - x[6] - x[9]);
- return (xv + xh);
- }
- public int laplacianFilter(int[] x)
- {
- int xb = (int)(x[1]- 2 * x[2] + x[3] - 2 * x[4] + 4 * x[5] - 2 * x[6] + x[7] - 2 * x[8] + x[9]);
- return (xb);
- }
- public int rataFilter(int[] x)
- {
- int xb = (int)((x[1] + x[2] + x[3] + x[4] + x[5] + x[6] + x[7] + x[8]) / 9);
- return xb;
- }
- public int gaussianFilter(int[] x)
- {
- int xb = (int)(xt[1] * x[1] + xt[2] * x[2] + xt[3] * x[3]);
- xb = (int)(xb + xt[4] * x[4] + xt[5] * x[5] + xt[6] * x[6]);
- xb = (int)(xb + xt[7] * x[7] + xt[8] * x[8] + xt[9] * x[9]);
- if (xb < 0) xb = 0;
- if (xb > 255) xb = 255;
- return (xb);
- }
- public int medianFilter(int[] x)
- {
- int max;
- int temp = 0;
- for (int d = 1; d <= 9; d++)
- {
- if (xt[d] < xt[d - 1])
- {
- max = (int)xt[d - 1];
- xt[d - 1] = temp;
- temp = (int)xt[d];
- xt[d] = xt[d - 1];
- }
- else
- {
- continue;
- }
- }
- int xb = (int)xt[5];
- if (xb < 0) xb = 0;
- if (xb > 255) xb = 255;
- return (xb);
- }
- public int banding1(int[] x)
- {
- int xb = (2 * xt[1] + xt[2] + xt[3]) /2;
- return xb;
- }
- public int banding2(int[] x)
- {
- int xb = (xt[1]+2*xt[2]+2*xt[3])/3;
- return xb;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement