title: Histogram peak identification and gauss fitting with minimal accumulated hight difference in c++
body: <p>I already asked a similar question some time ago in the following thread: <a href="https://stackoverflow.com/questions/1936107/fitting-a-gauss-curve-to-a-certain-histogram-peak-in-c">previous thread</a>. Until now I unfortunately couldn't entirely solve that issue and only worked around. Since it is difficult to include all the new information with the previous thread
  I post a refined and extended question with distinct context here and link it to the old thread.</p>
  
  <p>I am currently implementing an algorithm from a <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.102.1192" rel="nofollow noreferrer">paper</a> which extracts certain regions of a 3D data set by dynamically identifying value ranges in the data sets histogram.
  In a simplified way the method could be described as following:</p>
  
  <ol>
  <li>Find the highest peak in the histogram</li>
  <li>Fit a gaussian to the peak</li>
  <li>Using the value range defined by the gaussians mean (µ)+/- deviation(ϭ) certain regions
  of the histogram are identified, and the voxels (=3D pixels) of these regions are removed from the original histogram.</li>
  <li>As a result of the previous step a new highest peak should be revealed, based on which
  the steps 1-3 can be repeated. The steps are repeated until the data set histogram is empty.</li>
  </ol>
  
  <p>My questions relate to step 1 and 2 of the above description which is described as following in the paper: "The highest peak is identified and a Gaussian curve is fitted to its shape. The Gaussian is described by its midpoint µ, height h and deviation ϭ. The fitting process minimizes the accumulated height difference between the histogram and the middle part of the Gaussian. The error summation range is 
  µ+/ϭ? "<a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.102.1192" rel="nofollow noreferrer">1</a></p>
  
  <p>In the following I will ask my questions and add my reflections on them:</p>
  
  <ol>
  <li>How should I identify those bins of the total histogram which describe the highest peak? In order to identify its apex I simply run through the histogram and store the index of the bin with the highest frequency. But how far should the extend of the peak reach to the left and right of the highest bin. At the moment I simply go the the left and right of the highest bin for as long as the next bin is smaller as the previous one. However this is usually a very small range, since there occur creases (mini peaks) in the histogram. I already thought about smoothing the histogram. But I would have to that after each iteration since the subtraction of voxels (step 3 in the description above) can cause the histogram to contain creases again. And I am also worried that the repeated smoothing distorts the results. 
  Therefore I would like to ask whether there is an efficient way to detect the extend of a peak which is better than my current approach. There have been suggestions about mixture models and deconvolution in the <a href="https://stackoverflow.com/questions/1936107/fitting-a-gauss-curve-to-a-certain-histogram-peak-in-c">previous thread</a>. However are these methods really reasonable if the shape of the histogram constantly changes after each iteration?</li>
  <li>How can I fit a gauss curve to the identified peak so that the accumulated hight difference between the histogram and middle part of the gaussian is minimized?
  According to question one from the <a href="https://stackoverflow.com/questions/1936107/fitting-a-gauss-curve-to-a-certain-histogram-peak-in-c">previous thread</a> I fitted the curve to a given range of histogram bins by computing their mean and deviation (I hope this is correct?!). But how do I minimize the accumulated hight difference between the histogram and middle part of the gaussian from this point?</li>
  </ol>
  
  <p>Thank you for your help!
  Regards Marc</p>