title: Estimate average brightness of a grayscale picture with opencv
body: <p>I have a grayscale picture, and I would to transform it to black and white only. But for that, I need to calculate the right threshold, and I would like that threshold to be equal to the average brightness of the picture.</p>
  
  <p>So, I was wondering how I could calculate that threshold with OpenCV. Is there a method existing in the framework to do that easily ?</p>
  
  <p>I wanted to add every value of brightness (between 0 and 255) for every pixel, then divide the sum by the number of pixel itself, but the method I found to access those datas is really slow (.at(i,j)[k] for a rgb picture). But my picture is in grayscale, and I would like it to be quite fast, so it can be run on an iPhone.</p>