Reputation: 446
I've got a "mask" image, and I need to know if it has any zero values. I'd like to do something equivalent to:
bool HasMissingPixels(const cv::Mat& mask) {
return cv::countNonZero(image) < mask.rows * mask.cols;
}
It feels like wasting computation to find all (non-)zeros, when it could return true as soon as the first zero pixel is encountered. On the other hand, by iterating over the pixels myself I lose the advantage of OpenCV
's optimization, parallelization etc.
Is there a way to compute this faster?
Upvotes: 2
Views: 2006
Reputation: 11232
It usually makes sense to optimize for the worst case ("there is no zero value element in the array"), as then your system will have a predictable runtime behavior. Thus using countNonZero
or minMaxLoc
are good functions to achieve your goal here.
Whether an iterative approach that breaks early could be faster depends completely on the data ("are there zero values early in the matrix in the majority of the cases?"). You will find no such function in a library like OpenCV, as the library maintainer doesn't know your data and couldn't write code that optimizes for your custom data distribution.
Upvotes: 2