countNonZero
Usage
Counts the number of non-zero pixels in a matrix or image to quickly obtain the occurrences of a specific feature.
Interface
1 | int countNonZero(InputArray src); |
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
src |
Input image. |
Not null, single channel |
Input |
Return Values
- Success: KP_CV_SUCCESS
- Failure: an error code
Error Codes
Error Code |
Description |
|---|---|
INVALID_PARAM_MSG |
The input image is null. |
NOT_SUPPORT_MSG |
The type of the input image is not supported. |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <opencv2/opencv.hpp> #include <iostream> int main() { // Create a 5x5 binary image. cv::Mat src = (cv::Mat_<uint8_t>(5, 5) << 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0); // Count the number of non-zero pixels. int count = cv::countNonZero(src); std::cout << "Number_of_non-zero_pixels: " << count << std::endl; return 0; } |
Output:
1 | Number of non-zero pixels: 8 |
Parent topic: Interface Definition