Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Count pixels

Hi,

Below my implementation of function countPixels. Does exist better OpenCv/matrix way? cv::countNonZero works only for on channel image.

int countPixels(const cv::Mat &image, cv::Scalar color) {
  int result = 0;
  for (int y = 0; y < image.rows; y++) {
    const cv::Vec4b *rowImage = image.ptr<cv::Vec4b>(y);
    for (int x = 0; x < image.cols; x++) {
      if (rowImage[x][0] == color[0] && rowImage[x][1] == color[1] &&
          rowImage[x][2] == color[2] && rowImage[x][3] == color[3]) {
        ++result;
      }
    }
  }
  return result;
}

Thanks Mariusz