title: Add 1 to vector<unsigned char> value - Histogram in C++
body: <p>I guess it's such an easy question (I'm coming from Java), but I can't figure out how it works.</p>
  
  <p>I simply want to increment an vector element by one. The reason for this is, that I want to compute a histogram out of image values. But whatever I try I just can accomplish to assign a value to the vector. But not to increment it by one!</p>
  
  <p>This is my histogram function:</p>
  
  <pre><code>void histogram(unsigned char** image, int height,
          int width, vector&lt;unsigned char&gt;&amp; histogramArray) {
  
      for (int i = 0; i &lt; width; i++) {
          for (int j = 0; j &lt; height; j++) {
  
  
  //          histogramArray[1] = (int)histogramArray[1] + (int)1;
  // add histogram position by one if greylevel occured
              histogramArray[(int)image[i][j]]++;
          }
      }
  // display output
      for (int i = 0; i &lt; 256; i++) {
          cout &lt;&lt; "Position: " &lt;&lt; i &lt;&lt; endl;
          cout &lt;&lt; "Histogram Value: " &lt;&lt; (int)histogramArray[i] &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>But whatever I try to add one to the histogramArray position, it leads to just 0 in the output. I'm only allowed to assign concrete values like:</p>
  
  <pre><code>histogramArray[1] = 2;
  </code></pre>
  
  <p>Is there any simple and easy way? I though iterators are hopefully not necesarry at this point, because I know the exakt index position where I want to increment something.</p>
  
  <p>EDIT:
  I'm so sorry, I should have been more precise with my question, thank you for your help so far! The code above is working, but it shows a different mean value out of the histogram (difference of around 90) than it should. Also the histogram values are way different than in a graphic program - even though the image values are exactly the same! Thats why I investigated the function and found out if I set the histogram to zeros and then just try to increase one element, nothing happens! This is the commented code above:</p>
  
  <pre><code>for (int i = 0; i &lt; width; i++) {
              for (int j = 0; j &lt; height; j++) {
                  histogramArray[1]++;
      // add histogram position by one if greylevel occured
                  // histogramArray[(int)image[i][j]]++;
              }
          }
  </code></pre>
  
  <p>So the position 1 remains 0, instead of having the value height*width. Because of this, I think the correct calculation histogramArray[image[i][j]]++; is also not working properly.</p>
  
  <p>Do you have any explanation for this? This was my main question, I'm sorry.</p>
  
  <p>Just for completeness, this is my mean function for the histogram:</p>
  
  <pre><code>unsigned char meanHistogram(vector&lt;unsigned char&gt;&amp; histogram) {
      int allOccurences = 0;
      int allValues = 0;
      for (int i = 0; i &lt; 256; i++) {
          allOccurences += histogram[i] * i;
          allValues += histogram[i];
      }
      return (allOccurences / (float) allValues) + 0.5f;
  }
  </code></pre>
  
  <p>And I initialize the image like this:</p>
  
  <pre><code>unsigned char** image= new unsigned char*[width];
  for (int i = 0; i &lt; width; i++) {
  image[i] = new unsigned char[height];
  }
  </code></pre>
  
  <p>But there shouldn't be any problem with the initialization code, since all other computations work perfectly and I am able to manipulate and safe the original image. But it's true, that I should change width and height - since I had only square images it didn't matter so far.
  The Histogram is created like this and then the function is called like that:</p>
  
  <pre><code>vector&lt;unsigned char&gt; histogramArray(256);
  histogram(array, adaptedHeight, adaptedWidth, histogramArray);
  </code></pre>
  
  <p>So do you have any clue why this part histogramArray[1]++; don't increases my histogram?  histogramArray[1] remains 0 all the time! histogramArray[1] = 2; is working perfectly. Also histogramArray[(int)image[i][j]]++; seems to calculate something, but as I said, I think it's wrongly calculating.
  I appreciate any help very much! The reason why I used a 2D Array is simply because it is asked for. I like the 1D version also much more, because it's way simpler!</p>