Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.72 KB | None | 0 0
  1. private double Grayscale(Color c)
  2.         {
  3.             return (double)(c.R + c.G + c.B / 3;
  4.         }
  5.  
  6.  
  7.  
  8.         private void button14_Click(object sender, EventArgs e)
  9.         {
  10.             int template_size = workImageCorelatie.Width * workImageCorelatie.Height;
  11.             double sum = 0;
  12.             double sqrsum = 0;
  13.             workImage.Lock();
  14.             workImageCorelatie.Lock();
  15.             for (int i = 0; i < workImageCorelatie.Width; i++)
  16.             {
  17.                 for (int j = 0; j < workImageCorelatie.Height; j++)
  18.                 {
  19.                     double template = Grayscale(workImageCorelatie.GetPixel(i, j));
  20.                     sum += template;
  21.                     sqrsum += template * template;
  22.                 }
  23.             }
  24.             double mean_template = sum / template_size;
  25.             double var2template = sqrsum - (sum * sum) / template_size;
  26.             double var2image = 0;
  27.             double prodsum = 0;
  28.             double[,] corr_coeff = new double[workImage.Width, workImage.Height];
  29.             for (int i = 0; i < workImage.Width - workImageCorelatie.Width; i++)
  30.             {
  31.                 for (int j = 0; j < workImage.Height - workImageCorelatie.Height; j++)
  32.                 {
  33.                     sum = 0;
  34.                     sqrsum = 0;
  35.                     prodsum = 0;
  36.                     for (int k = 0; k < workImageCorelatie.Width; k++)
  37.                         for (int m = 0; m < workImageCorelatie.Height; m++)
  38.                         {
  39.                             double img = Grayscale(workImage.GetPixel(i + k, j + m));
  40.                             double template = Grayscale(workImageCorelatie.GetPixel(k, m));
  41.                             sum += img;
  42.                             sqrsum += img * img;
  43.                             prodsum += img * template;
  44.                         }
  45.                     var2image = sqrsum - (sum * sum) / template_size;
  46.                     corr_coeff[i, j] = ((prodsum - (mean_template * sum)) / Math.Sqrt(var2image * var2template));
  47.                 }
  48.             }
  49.             //cauti maximul in matrice
  50.             double max = Double.NegativeInfinity;
  51.             int imax = 0, jmax = 0;
  52.             for (int i = 0; i < workImage.Width - workImageCorelatie.Width; i++)
  53.                 for (int j = 0; j < workImage.Height - workImageCorelatie.Height; j++)
  54.                     if (max < corr_coeff[i, j])
  55.                     {
  56.                         max = corr_coeff[i, j];
  57.                         imax = i;
  58.                         jmax = j;
  59.                     }
  60.  
  61.             // desenezi liniile de pe poza mare
  62.             for (int i = 0; i < workImageCorelatie.Width; i++)
  63.             {
  64.                 workImage.SetPixel(imax + i, jmax, Color.Blue);
  65.                 workImage.SetPixel(imax + i, jmax + workImageCorelatie.Height, Color.Blue);
  66.             }
  67.             for (int j = 0; j < workImageCorelatie.Height; j++)
  68.             {
  69.                 workImage.SetPixel( imax, jmax + j, Color.Blue);
  70.                 workImage.SetPixel( imax + workImageCorelatie.Width, jmax + j, Color.Blue);
  71.             }
  72.             panelDestination.BackgroundImage = null;
  73.             panelDestination.BackgroundImage = workImage.GetBitMap();
  74.             workImage.Unlock();
  75.             workImageCorelatie.Unlock();
  76.        
  77.         }
  78.  
  79.         private void button15_Click(object sender, EventArgs e)
  80.         {
  81.             openFileDialog.ShowDialog();
  82.             sSourceFileNameCorelatie = openFileDialog.FileName;
  83.             imageCorelatie = new Bitmap(sSourceFileNameCorelatie);
  84.             workImageCorelatie = new FastImage(imageCorelatie);
  85.             panelDestination.BackgroundImage = workImageCorelatie.GetBitMap();
  86.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement