title: Translating code from matlab to c#
body: <p>I am trying to implement image noise reduction using MRF. I used this <a href="http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/AV0809/ORCHARD/" rel="nofollow noreferrer">article</a>. It also provides <a href="http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/AV0809/ORCHARD/restore_image.html" rel="nofollow noreferrer">this code in matlab</a>. I tried to rewrite it in c# to work with noised images with only blue channel like this<a href="https://i.stack.imgur.com/DwikC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DwikC.png" alt="like this"></a></p>
  
  <p>But after using my code I got only darker image. What am I doing wrong? Is it something in matlab that I didn't include in my code?
  My code is:</p>
  
  <pre><code>public static Bitmap Process(Bitmap src, double covar, double max_diff, double weight_diff, int iterations)
  {
      var buffer = new Bitmap[2];
      var b = new Bitmap(src.Width, src.Height, src.PixelFormat);
      for (var i = 0; i &lt; src.Width; i++)
          for (var j = 0; j &lt; src.Height; j++)
          {
              b.SetPixel(i, j, Color.FromArgb(src.GetPixel(i, j).A, 0, 0, 0));
          }
  
      buffer[0] = src;
      buffer[1] = b;
      var s = 1;
      var d = 0;
      var vMax = (src.Width * src.Height) * (Math.Pow(256, 2) / (2 * covar) + 4 * weight_diff * max_diff);
  
      for (var i = 0; i &lt; iterations; i++)
      {
          Console.WriteLine("Iteration: " + i.ToString());
          if (s == 0) { s = 1; d = 0; }
          else { s = 0; d = 1; }
          for (var r = 0; r &lt; src.Height - 1; r++)
              for (var c = 0; c &lt; src.Width - 1; c++)
              {
                  //val = red
                  var vLocal = vMax;
                  var minVal = -1;
                  for (var val = 0; val &lt; 255; val+=1)
                  {
                      var V_Data = Math.Pow((val - src.GetPixel(r, c).B), 2) / (2 * covar); //r
                      var V_diff = 0;
  
                      if (r &gt; 1)
                      {
                          V_diff = V_diff + (int)Math.Min((val - Math.Pow(buffer[s].GetPixel(r - 1, c).B, 2)), max_diff);
                      }
  
                      if (r &lt; src.Height)
                      {
                          V_diff = V_diff + (int)Math.Min((val - Math.Pow(buffer[s].GetPixel(r + 1, c).B, 2)), max_diff);
                      }
  
                      if (c &gt; 1)
                      {
                          V_diff = V_diff + (int)Math.Min((val - Math.Pow(buffer[s].GetPixel(r, c - 1).B, 2)), max_diff);
                      }
  
                      if (c &lt; src.Height)
                      {
                          V_diff = V_diff + (int)Math.Min((val - Math.Pow(buffer[s].GetPixel(r, c + 1).B, 2)), max_diff);
                      }
  
                      var V_current = V_Data + weight_diff * V_diff;
  
                      if (V_current &lt; vLocal)
                      {
                          minVal = val;
                          vLocal = V_current;
                      }
                  }
                  Color color = buffer[d].GetPixel(r, c); 
                  var e = Color.FromArgb(color.A, color.R, color.G, minVal);
                  buffer[d].SetPixel(r, c, e);
              }
      }
      return buffer[d];
  }
  </code></pre>