Advertisement
ariestamirra

filter

Jun 7th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. private void btn_Filter_Click(object sender, EventArgs e)
  2. {
  3.     String lpf = (String)comboBox1.SelectedItem;
  4.     String hpf = (String)comboBox2.SelectedItem;
  5.     String banding = (String)comboBox3.SelectedItem;
  6.     int[] xt = new int[10];
  7.     objBitmap10 = new Bitmap(objBitmap1);
  8.     for (int x = 1; x < objBitmap1.Width - 1; x++)
  9.         for (int y = 1; y < objBitmap1.Height - 1; y++)
  10.         {
  11.             Color w1 = objBitmap1.GetPixel(x - 1, y - 1);
  12.             Color w2 = objBitmap1.GetPixel(x - 1, y);
  13.             Color w3 = objBitmap1.GetPixel(x - 1, y + 1);
  14.             Color w4 = objBitmap1.GetPixel(x, y - 1);
  15.             Color w5 = objBitmap1.GetPixel(x, y);
  16.             Color w6 = objBitmap1.GetPixel(x, y + 1);
  17.             Color w7 = objBitmap1.GetPixel(x + 1, y - 1);
  18.             Color w8 = objBitmap1.GetPixel(x + 1, y);
  19.             Color w9 = objBitmap1.GetPixel(x + 1, y + 1);
  20.             xt[1] = w1.R; xt[2] = w2.R;
  21.             xt[3] = w3.R; xt[4] = w4.R;
  22.             xt[5] = w5.R; xt[6] = w6.R;
  23.             xt[7] = w7.R; xt[8] = w8.R;
  24.             xt[9] = w9.R;
  25.             int xlpf, xhpf, xbanding;
  26.             switch (lpf)
  27.             {
  28.                 case "Filter Rata-Rata":
  29.                     xlpf = rataFilter(xt);
  30.                     break;
  31.                 case "Filter Gaussian":
  32.                     xlpf = gaussianFilter(xt);
  33.                     break;
  34.                 case "Filter Median":
  35.                     xlpf = medianFilter(xt);
  36.                     break;
  37.                 default:
  38.                     xlpf = rataFilter(xt);
  39.                     break;
  40.             }
  41.             switch (hpf)
  42.             {
  43.                 case "Sobel":
  44.                     xhpf = sobelFilter(xt);
  45.                     break;
  46.                 case "Prewitt":
  47.                     xhpf = prewittFilter(xt);
  48.                     break;
  49.                 case "Laplacian":
  50.                     xhpf = laplacianFilter(xt);
  51.                     break;
  52.                 default:
  53.                     xhpf = sobelFilter(xt);
  54.                     break;
  55.             }
  56.             switch (banding)
  57.             {
  58.                 case "2:1":
  59.                     xbanding = banding1(xt);
  60.                     break;
  61.                 case "1:2":
  62.                     xbanding = banding2(xt);
  63.                     break;
  64.                 default:
  65.                     xbanding = banding1(xt);
  66.                     break;
  67.             }
  68.             int xb = xlpf + xhpf;
  69.             if (xb < 0) xb = -xb;
  70.             if (xb > 255) xb = 255;
  71.             Color wb = Color.FromArgb(xb, xb, xb);
  72.             objBitmap10.SetPixel(x, y, wb);
  73.         }
  74.     pb_Prak11.Image = objBitmap10;
  75. }
  76.  
  77. public int sobelFilter(int[] x)
  78. {
  79.     int xv = (int)(-x[1] - 2 * x[2] - x[3] + x[7] + 2 * x[8] + x[9]);
  80.     int xh = (int)(x[1] + 2 * x[4] + x[7] - x[3] - 2 * x[6] - x[9]);
  81.     return (xv + xh);
  82. }
  83. public int prewittFilter(int[] x)
  84. {
  85.     int xv = (int)(-x[1] - x[2] - x[3] + x[7] + x[8] + x[9]);
  86.     int xh = (int)(x[1] + x[4] + x[7] - x[3] - x[6] - x[9]);
  87.     return (xv + xh);
  88. }
  89. public int laplacianFilter(int[] x)
  90. {
  91.     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]);
  92.     return (xb);
  93. }
  94. public int rataFilter(int[] x)
  95. {
  96.     int xb = (int)((x[1] + x[2] + x[3] + x[4] + x[5] + x[6] + x[7] + x[8]) / 9);
  97.     return xb;
  98. }
  99. public int gaussianFilter(int[] x)
  100. {
  101.     int xb = (int)(xt[1] * x[1] + xt[2] * x[2] + xt[3] * x[3]);
  102.     xb = (int)(xb + xt[4] * x[4] + xt[5] * x[5] + xt[6] * x[6]);
  103.     xb = (int)(xb + xt[7] * x[7] + xt[8] * x[8] + xt[9] * x[9]);
  104.     if (xb < 0) xb = 0;
  105.     if (xb > 255) xb = 255;
  106.     return (xb);
  107. }
  108. public int medianFilter(int[] x)
  109. {
  110.     int max;
  111.     int temp = 0;
  112.     for (int d = 1; d <= 9; d++)
  113.     {
  114.         if (xt[d] < xt[d - 1])
  115.         {
  116.             max = (int)xt[d - 1];
  117.             xt[d - 1] = temp;
  118.             temp = (int)xt[d];
  119.             xt[d] = xt[d - 1];
  120.         }
  121.         else
  122.         {
  123.             continue;
  124.         }
  125.     }
  126.     int xb = (int)xt[5];
  127.     if (xb < 0) xb = 0;
  128.     if (xb > 255) xb = 255;
  129.     return (xb);
  130. }
  131. public int banding1(int[] x)
  132. {
  133.     int xb = (2 * xt[1] + xt[2] + xt[3]) /2;
  134.     return xb;
  135. }
  136. public int banding2(int[] x)
  137. {
  138.     int xb = (xt[1]+2*xt[2]+2*xt[3])/3;
  139.     return xb;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement