title: Count the black pixels using OpenCV
body: <p>I'm working in <code>opencv 2.4.0</code> and <code>C++</code></p>
  
  <p>I'm trying to do an exercise that says I should load an RGB image, convert it to gray scale and save the new image. The next step is to make the grayscale image into a binary image and store that image. This much I have working. </p>
  
  <p>My problem is in counting the amount of black pixels in the binary image.</p>
  
  <p>So far I've searched the web and looked in the book. The method that I've found that seems the most useful is.</p>
  
  <pre><code>int TotalNumberOfPixels = width * height;
  int ZeroPixels = TotalNumberOfPixels - cvCountNonZero(cv_image);
  </code></pre>
  
  <p>But I don't know how to store these values and use them in <code>cvCountNonZero()</code>.  When I pass the the image I want counted from to this function I get an error.</p>
  
  <pre><code>int main()
  {
      Mat rgbImage, grayImage, resizedImage, bwImage, result;
  
      rgbImage = imread("C:/MeBGR.jpg");
      cvtColor(rgbImage, grayImage, CV_RGB2GRAY);
  
      resize(grayImage, resizedImage, Size(grayImage.cols/3,grayImage.rows/4),
             0, 0, INTER_LINEAR);
  
      imwrite("C:/Jakob/Gray_Image.jpg", resizedImage);
      bwImage = imread("C:/Jakob/Gray_Image.jpg");
      threshold(bwImage, bwImage, 120, 255, CV_THRESH_BINARY);
      imwrite("C:/Jakob/Binary_Image.jpg", bwImage);
      imshow("Original", rgbImage);
      imshow("Resized", resizedImage);
      imshow("Resized Binary", bwImage);
  
      waitKey(0);
      return 0;
  }
  </code></pre>
  
  <p>So far this code is very basic but it does what it's supposed to for now. Some adjustments will be made later to clean it up :)</p>