Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Counting black & white pixels with a threshold

I use ROI to work with a part of image that i am interested in, and now i want to count black & white pixels in it. Although something as simple as:

 int count_black = 0;
    int count_white = 0;
    for( int y = 0; y < imgROI.rows; y++ ) {
      for( int x = 0; x < imgROI.cols; x++ ) {
          if ( imgROI.at<cv::Vec3b>(y,x) == cv::Vec3b(255,255,255) ) {
            count_white++;
          }
          else if ( imgROI.at<cv::Vec3b>(y,x) == cv::Vec3b(0,0,0) ) {
            count_black++;
          }
        }
    }

wont work, since there is no complete white or black one's. Only various shades of gray. Is there a way to use some threshold to sort them in black or white counters? Or maybe function to increase contrast and convert them into absolute white or black colors?