Advertisement
waydub12

Snapshot Filtering C#

Apr 3rd, 2022
2,107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1.         Bitmap bmpAsli, bmpHasil;
  2.         private void assignMatrix(int i, int j, ref byte[] red, ref byte[] green, ref byte[] blue)
  3.         {
  4.             int hi = i - 1;
  5.             int hj = j - 1;
  6.  
  7.             for (int index = 0; index < 9; index++)
  8.             {
  9.                 red[index] = bmpAsli.GetPixel(hi, hj).R;
  10.                 green[index] = bmpAsli.GetPixel(hi, hj).G;
  11.                 blue[index] = bmpAsli.GetPixel(hi, hj).R;
  12.  
  13.                 if (hj - (j - 1) == 2)
  14.                 {
  15.                     hi += 1;
  16.                     hj = j - 1;
  17.                 }
  18.                 else
  19.                 {
  20.                     hj += 1;
  21.                 }
  22.             }
  23.         }
  24.  
  25.         private void sortMatrix(int size, ref byte[] red, ref byte[] green, ref byte[] blue)
  26.         {
  27.             for (int m = 0; m < size - 1; m++)
  28.             {
  29.                 for (int n = 0; n < size - 1 - m; n++)
  30.                 {
  31.                     if (red[n] > red[n + 1])
  32.                     {
  33.                         byte temp = red[n];
  34.                         red[n] = red[n + 1];
  35.                         red[n + 1] = temp;
  36.                     }
  37.  
  38.                     if (green[n] > green[n + 1])
  39.                     {
  40.                         byte temp = green[n];
  41.                         green[n] = green[n + 1];
  42.                         green[n + 1] = temp;
  43.                     }
  44.  
  45.                     if (blue[n] > blue[n + 1])
  46.                     {
  47.                         byte temp = blue[n];
  48.                         blue[n] = blue[n + 1];
  49.                         blue[n + 1] = temp;
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.  
  55.         private void buttonMedianFilter_Click(object sender, EventArgs e)
  56.         {
  57.             Cursor = Cursors.WaitCursor;
  58.  
  59.             byte[] red = new byte[9];
  60.             byte[] green = new byte[9];
  61.             byte[] blue = new byte[9];
  62.  
  63.             bmpAsli = (Bitmap)pictureBoxInput.Image;
  64.             int nWidth = bmpAsli.Width - 1;
  65.             int nHeight = bmpAsli.Height - 1;
  66.             bmpHasil = new Bitmap(bmpAsli.Width, bmpAsli.Height);
  67.  
  68.             for (int i = 1; i < nWidth; i++)
  69.             {
  70.                 for (int j = 1; j < nHeight; j++)
  71.                 {
  72.                     assignMatrix(i, j, ref red, ref green, ref blue);
  73.                     sortMatrix(9, ref red, ref green, ref blue);
  74.                     bmpHasil.SetPixel(i, j, Color.FromArgb(red[4], green[4], blue[4]));
  75.                 }
  76.             }
  77.  
  78.             pictureBoxOutput.Image = bmpHasil;
  79.  
  80.             Cursor = Cursors.Default;
  81.         }
  82.  
  83.         private void buttonMaxFilter_Click(object sender, EventArgs e)
  84.         {
  85.             Cursor = Cursors.WaitCursor;
  86.  
  87.             byte[] red = new byte[9];
  88.             byte[] green = new byte[9];
  89.             byte[] blue = new byte[9];
  90.  
  91.             bmpAsli = (Bitmap)pictureBoxInput.Image;
  92.             int nWidth = bmpAsli.Width - 1;
  93.             int nHeight = bmpAsli.Height - 1;
  94.             bmpHasil = new Bitmap(bmpAsli.Width, bmpAsli.Height);
  95.  
  96.             for (int i = 1; i < nWidth; i++)
  97.             {
  98.                 for (int j = 1; j < nHeight; j++)
  99.                 {
  100.                     assignMatrix(i, j, ref red, ref green, ref blue);
  101.                     sortMatrix(9, ref red, ref green, ref blue);
  102.                     bmpHasil.SetPixel(i, j, Color.FromArgb(red[8], green[8], blue[8]));
  103.                 }
  104.             }
  105.  
  106.             pictureBoxOutput.Image = bmpHasil;
  107.  
  108.             Cursor = Cursors.Default;
  109.         }
  110.  
  111.         private void buttonMinFilter_Click(object sender, EventArgs e)
  112.         {
  113.             Cursor = Cursors.WaitCursor;
  114.  
  115.             byte[] red = new byte[9];
  116.             byte[] green = new byte[9];
  117.             byte[] blue = new byte[9];
  118.  
  119.             bmpAsli = (Bitmap)pictureBoxInput.Image;
  120.             int nWidth = bmpAsli.Width - 1;
  121.             int nHeight = bmpAsli.Height - 1;
  122.             bmpHasil = new Bitmap(bmpAsli.Width, bmpAsli.Height);
  123.  
  124.             for (int i = 1; i < nWidth; i++)
  125.             {
  126.                 for (int j = 1; j < nHeight; j++)
  127.                 {
  128.                     assignMatrix(i, j, ref red, ref green, ref blue);
  129.                     sortMatrix(9, ref red, ref green, ref blue);
  130.                     bmpHasil.SetPixel(i, j, Color.FromArgb(red[0], green[0], blue[0]));
  131.                 }
  132.             }
  133.  
  134.             pictureBoxOutput.Image = bmpHasil;
  135.  
  136.             Cursor = Cursors.Default;
  137.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement