title: Multi otsu(multi-thresholding) with openCV
body: <p>I am trying to carry out multi-thresholding with otsu. The method I am using currently is actually via maximising the between class variance, I have managed to get the same threshold value given as that by the OpenCV library. However, that is just via running otsu method once.</p>
  
  <p>Documentation on how to do multi-level thresholding or rather recursive thresholding is rather limited. Where do I do after obtaining the original otsu's value? Would appreciate some hints, I been playing around with the code, adding one external for loop, but the next value calculated is always 254 for any given image:(</p>
  
  <p>My code if need be:</p>
  
  <pre><code>//compute histogram first
  cv::Mat imageh; //image edited to grayscale for histogram purpose
  //imageh=image; //to delete and uncomment below;
  cv::cvtColor(image, imageh, CV_BGR2GRAY);
  
  int histSize[1] = {256}; // number of bins
  float hranges[2] = {0.0, 256.0}; // min andax pixel value
  const float* ranges[1] = {hranges};
  int channels[1] = {0}; // only 1 channel used
  
  cv::MatND hist;
  // Compute histogram
  calcHist(&amp;imageh, 1, channels, cv::Mat(), hist, 1, histSize, ranges);
  
  IplImage* im = new IplImage(imageh);//assign the image to an IplImage pointer
  IplImage* finalIm = cvCreateImage(cvSize(im-&gt;width, im-&gt;height), IPL_DEPTH_8U, 1);
  double otsuThreshold= cvThreshold(im, finalIm, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU );
  
  cout&lt;&lt;"opencv otsu gives "&lt;&lt;otsuThreshold&lt;&lt;endl;
  
  int totalNumberOfPixels= imageh.total();
  cout&lt;&lt;"total number of Pixels is " &lt;&lt;totalNumberOfPixels&lt;&lt; endl;
  
  
  float sum = 0;
  for (int t=0 ; t&lt;256 ; t++) 
  {
      sum += t * hist.at&lt;float&gt;(t);
  }
  cout&lt;&lt;"sum is "&lt;&lt;sum&lt;&lt;endl;
  
  float sumB = 0; //sum of background
  int wB = 0; // weight of background
  int wF = 0; //weight of foreground
  
  float varMax = 0;
  int threshold = 0;
  
  //run an iteration to find the maximum value of the between class variance(as between class variance shld be maximise)
  for (int t=0 ; t&lt;256 ; t++) 
  {
         wB += hist.at&lt;float&gt;(t);               // Weight Background
         if (wB == 0) continue;
  
         wF = totalNumberOfPixels - wB;                 // Weight Foreground
         if (wF == 0) break;
  
         sumB += (float) (t * hist.at&lt;float&gt;(t));
  
         float mB = sumB / wB;            // Mean Background
         float mF = (sum - sumB) / wF;    // Mean Foreground
  
         // Calculate Between Class Variance
         float varBetween = (float)wB * (float)wF * (mB - mF) * (mB - mF);
  
         // Check if new maximum found
         if (varBetween &gt; varMax) {
            varMax = varBetween;
            threshold = t;
         }
  }
  
         cout&lt;&lt;"threshold value is: "&lt;&lt;threshold;
  </code></pre>