title: Finding the local maxima/peaks and minima/valleys of histograms
body: <p>Ok, so I have a histogram (represented by an array of ints), and I'm looking for the best way to find local maxima and minima. Each histogram should have 3 peaks, one of them (the first one) probably much higher than the others.</p>
  
  <p>I want to do several things: </p>
  
  <ol>
  <li><p>Find the first "valley" following the first peak (in order to get rid of the first peak altogether in the picture)</p></li>
  <li><p>Find the optimum "valley" value in between the remaining two peaks to separate the picture</p>
  
  <p>I already know how to do step 2 by implementing a variant of Otsu.
  But I'm struggling with step 1 </p></li>
  <li><p>In case the valley in between the two remaining peaks is not low enough, I'd like to give a warning.</p></li>
  </ol>
  
  <p>Also, the image is quite clean with little noise to account for</p>
  
  <p>What would be the brute-force algorithms to do steps 1 and 3? I could find a way to implement Otsu, but the brute-force is escaping me, math-wise. As it turns out, there is more documentation on doing methods like otsu, and less on simply finding peaks and valleys. I am not looking for anything more than whatever gets the job done (i.e. it's a temporary solution, just has to be implementable in a reasonable timeframe, until I can spend more time on it)</p>
  
  <p>I am doing all this in c#</p>
  
  <p>Any help on which steps to take would be appreciated! 
  Thank you so much! </p>
  
  <p>EDIT: some more data:</p>
  
  <p>most histogram are likely to be like the first one, with the first peak representing background.</p>
  
  <p><img src="https://i.stack.imgur.com/bbblD.png" alt="Histogram"></p>
  
  <p><img src="https://i.stack.imgur.com/gYryZ.png" alt="Histogram 2"></p>