my suggestion
int countPixels(const cv::Mat &image, cv::Scalar color) {
Mat binary_image;
cv::inRange(image, color, color, binary_image);
return cv::countNonZero(binary_image);
}
you can test and compare with the code below
my test results seems like:
pixel count : 602 times passed in seconds: 0.00797911
pixel count : 602 times passed in seconds: 0.0123889
pixel count : 527 times passed in seconds: 0.0125029
pixel count : 527 times passed in seconds: 0.0215335
pixel count : 252 times passed in seconds: 0.0145191
pixel count : 252 times passed in seconds: 0.0210186
the pixel counts same ( as i changed your code to calculate BGR image ) but performance comparison says my suggestion is better.
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int countPixels1(const cv::Mat &image, cv::Scalar color) {
int result = 0;
for (int y = 0; y < image.rows; y++) {
const cv::Vec3b *rowImage = image.ptr<cv::Vec3b>(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]) {
++result;
}
}
}
return result;
}
int countPixels(const cv::Mat &image, cv::Scalar color) {
Mat binary_image;
cv::inRange(image, color, color, binary_image);
return cv::countNonZero(binary_image);
}
int main(int argc, char* argv[])
{
Mat input;
cv::VideoCapture capture;
capture.open(0);
while (true){
capture.read(input);
imshow("test", input);
waitKey(30);
Scalar color = Scalar(10, 10, 10);
double t = (double)getTickCount();
cout << "pixel count : " << countPixels(input, color);
t = ((double)getTickCount() - t) / getTickFrequency();
cout << " times passed in seconds: " << t << endl;
t = (double)getTickCount();
cout << "pixel count : " << countPixels1(input, color);
t = ((double)getTickCount() - t) / getTickFrequency();
cout << " times passed in seconds: " << t << endl << endl;
}
return 0;
}