title: Selective histogram equalization (only on a specified area of the image)
body: <p>I am developping on Qt creator, with opencv.</p>
  
  <p>I have to developp a program that does the histogram equalization of an image.
  My images are 16bits grayscale images so I cannot use the opencv function "equalizeHist" because it only works with 8bit grayscale images.</p>
  
  <p>The code I wrote to do it is the following :</p>
  
  <pre><code>void equalizeHist_16U(Mat* img)
  {
      long hist[65535] = {0};
      double ratio;
      int i, j;
  
      assert(img-&gt;channels() == 1);
      assert(img-&gt;type() == CV_16U);
  
      ratio = 65535.0 / (img-&gt;cols*img-&gt;rows);
  
      //Cumulative histogram calculation
      compute_hist_16U(img, hist, true);
  
      for(i=0 ; i&lt;img-&gt;cols ; i++)
      {
          for(j=0 ; j&lt;img-&gt;rows ; j++)
          {
              img-&gt;at&lt;unsigned short&gt;(j,i) = ratio*hist[img-&gt;at&lt;unsigned short&gt;(j,i)];
          }
      }
  
  }
  
  long compute_hist_16U (Mat* img, long* hist, bool cumul)
  {
      unsigned short i, j, k;
      long* b;
      long max = 0;
  
      //is the image 16bits grayscale ?
      assert(img-&gt;channels() == 1);
      assert(CV_16U == img-&gt;type());
  
      //histogram calculation
      for(i=0 ; i&lt;img-&gt;cols ; i++)
      {
          for(j=0 ; j&lt;img-&gt;rows ; j++)
          {
              hist[img-&gt;at&lt;unsigned short&gt;(j,i)]++;
              if(hist[img-&gt;at&lt;unsigned short&gt;(j,i)] &gt; max)
                  max = hist[img-&gt;at&lt;unsigned short&gt;(j,i)];
          }
      }
  
      //Cumulative histogram calculation (if cumul=true)
      if(cumul)
      {
          for(b=hist ; b&lt;hist+65535 ; b++)
          {
              *(b+1) += *b;
          }
      }
      return (cumul ? hist[65535] : max);
  }
  </code></pre>
  
  <p>It does what I expected, now I want to do the histogram equialization of my image but only on a specified part of the image.
  I added x1,x2,y1,y2 parameters to my function and changed the bounds of my "for" like this (the lines of code i changed have arrows):</p>
  
  <pre><code>----&gt;void equalizeHist_16U(Mat* img, int x1, int x2, int y1, int y2)
  {
      long hist[65535] = {0};
      double ratio;
      int i, j;
  
      assert(img-&gt;channels() == 1);
      assert(img-&gt;type() == CV_16U);
  
      ratio = 65535.0 / (img-&gt;cols*img-&gt;rows);
  
      //Cumulative histogram calculation
      compute_hist_16U(img, hist, true);
  
      ----&gt;for(i=x1 ; i&lt;=x2 ; i++)
      {
          ----&gt;for(j=y1 ; j&lt;=y2 ; j++)
          {
              img-&gt;at&lt;unsigned short&gt;(j,i) = ratio*hist[img-&gt;at&lt;unsigned short&gt;(j,i)];
          }
      }
  
  }
  
  ----&gt;long compute_hist_16U (Mat* img, long* hist, bool cumul, int x1, int x2, int y1, int y2)
  {
      unsigned short i, j, k;
      long* b;
      long max = 0;
  
      //is the image 16bits grayscale ?
      assert(img-&gt;channels() == 1);
      assert(CV_16U == img-&gt;type());
  
      //histogram calculation
      ----&gt;for(i=x1 ; i&lt;=x2  ; i++)
      {
          ----&gt;for(j=y1 ; j&lt;=y2 ; j++)
          {
              hist[img-&gt;at&lt;unsigned short&gt;(j,i)]++;
              if(hist[img-&gt;at&lt;unsigned short&gt;(j,i)] &gt; max)
                  max = hist[img-&gt;at&lt;unsigned short&gt;(j,i)];
          }
      }
  
      //Cumulative histogram calculation (if cumul=true)
      if(cumul)
      {
          for(b=hist ; b&lt;hist+65535 ; b++)
          {
              *(b+1) += *b;
          }
      }
      return (cumul ? hist[65535] : max);
  }
  </code></pre>
  
  <p>But it does not work as expected, my image is not equalized, I do not have extrem values (clear white and dark black) on my image.
  If I try </p>
  
  <pre><code>equalizeHist_16U(&amp;img, 0, 50, 0, 50)
  </code></pre>
  
  <p>The image i get is very very bright
  And if I try</p>
  
  <pre><code>equalizeHist(&amp;img, 300, 319, 220, 239)
  </code></pre>
  
  <p>The image I get is very very dark</p>
  
  <p>I think I have made a mistake in the loops bounds but I can't find where !
  Maybe you have an idea ?</p>
  
  <p>Thank you in advance</p>