title: C++ Stretching an equalized image
body: <p>From an (2)equalized image I have to create a (3).</p>
  
  <ol>
  <li>Original image: <a href="http://i.imgur.com/X5MKF6z.jpg" rel="nofollow">http://i.imgur.com/X5MKF6z.jpg</a></li>
  <li>Equalized image : <a href="http://i.imgur.com/oFBVUJp.png" rel="nofollow">http://i.imgur.com/oFBVUJp.png</a></li>
  <li>Equalized and Stretch image: <a href="http://i.imgur.com/V7jeaRQ.png" rel="nofollow">http://i.imgur.com/V7jeaRQ.png</a></li>
  </ol>
  
  <p>With OpenCV I could have used equalizeHist() that does both equalization and stretching. </p>
  
  <p>So without using OPENCV, how can I do stretching from an equalization image. The equalization part is done below.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  #include &lt;opencv/highgui.h&gt;
  #include &lt;cstring&gt;
  using std::cout;
  using std::cin;
  using std::endl;
  
  using namespace cv;
  
  void imhist(Mat image, int histogram[])
  {
  
      // initialize all intensity values to 0
      for (int i = 0; i &lt; 256; i++)
      {
          histogram[i] = 0;
      }
  
      // calculate the no of pixels for each intensity values
      for (int y = 0; y &lt; image.rows; y++)
          for (int x = 0; x &lt; image.cols; x++)
              histogram[(int)image.at&lt;uchar&gt;(y, x)]++;
  
  }
  
  void cumhist(int histogram[], int cumhistogram[])
  {
      cumhistogram[0] = histogram[0];
  
      for (int i = 1; i &lt; 256; i++)
      {
          cumhistogram[i] = histogram[i] + cumhistogram[i - 1];
      }
  }
  
  int main()
  {
      // Load the image
      Mat image = imread("y1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
  
      // Generate the histogram
      int histogram[256];
      imhist(image, histogram);
  
  
      // Caluculate the size of image
      int size = image.rows * image.cols;
      float alpha = 255.0 / size;
  
      // Calculate the probability of each intensity
      float PrRk[256];
      for (int i = 0; i &lt; 256; i++)
      {
          PrRk[i] = (double)histogram[i] / size;
      }
  
      // Generate cumulative frequency histogram
      int cumhistogram[256];
      cumhist(histogram, cumhistogram);
  
      // Scale the histogram
      int Sk[256];
      for (int i = 0; i &lt; 256; i++)
      {
          Sk[i] = cvRound((double)cumhistogram[i] * alpha);
      }
  
      // Generate the equlized image
      Mat new_image = image.clone();
  
      for (int y = 0; y &lt; image.rows; y++)
          for (int x = 0; x &lt; image.cols; x++)
              new_image.at&lt;uchar&gt;(y, x) = saturate_cast&lt;uchar&gt;(Sk[image.at&lt;uchar&gt;(y, x)]);
      //////////////////////////////////////////
  
      // // Generate the histogram stretched image
      Mat str_image = new_image.clone();
  
      //for (int a = 0; a &lt; str_image.rows; a++)
      //  for (int b = 0; b &lt; str_image.cols; b++)
  
      // Display the original Image
      namedWindow("Original Image");
      imshow("Original Image", image);
  
      // Display equilized image
      namedWindow("Equalized Image");
      imshow("Equalized Image", new_image);
  
  
      waitKey();
      return 0;
  }
  </code></pre>