The program to count number of white and black pixels in a binary image is not working.Here I paste the code i used.Please help me with a solution.
int count_white=0;
int count_black=0;
for (int y = 0; y < image.rows; y++) {
for (int x = 0; x < image.cols; x++) {
if (image.at<cv::Vec3b>(y, x) == cv::Vec3b(255, 255, 255)) {
count_white++;
}
else if (image.at<cv::Vec3b>(y, x) == cv::Vec3b(0, 0, 0)) {
count_black++;
}
}
}
what does
image.type()
return ?(unlikely, that your binary image is Vec3b (or CV_8uC3))
Sorry i did not get you
at<someType>(y,x)
, you must make sure, that the image has exactly that type, so check, don't guess !Can you share how to obtain your binary image? Even though, I totally agree with berak's comment above; your loop works perfectly with appropriate image.
cv::Mat Q(q.size(), q.type()); cv::threshold(q, Q, 100, 255, cv::THRESH_BINARY);