findNonZero
import { findNonZero } from '@banou/opencv-wasm'Use after await initOpenCV(). See the initialization and named imports guide.
Returns the list of locations of non-zero pixels
Given a binary matrix (likely returned from an operation such as threshold(), compare(), >, ==, etc, return all of the non-zero indices as a cv::Mat or std::vectorcv::Point (x,y) For example:
cv::Mat binaryImage; // input, binary image
cv::Mat locations; // output, locations of non-zero pixels
cv::findNonZero(binaryImage, locations);
// access pixel coordinates
Point pnt = locations.at<Point>(i);
or
cv::Mat binaryImage; // input, binary image
vector<Point> locations; // output, locations of non-zero pixels
cv::findNonZero(binaryImage, locations);
// access pixel coordinates
Point pnt = locations[i];
The function do not work with multi-channel arrays. If you need to find non-zero elements across all the channels, use Mat::reshape first to reinterpret the array as single-channel. Or you may extract the particular channel using either extractImageCOI, or mixChannels, or split.
Note: - CV_16F/CV_16BF/CV_Bool/CV_64U/CV_64S/CV_32U are not supported for src.
- If only count of non-zero array elements is important,
countNonZerois helpful. - If only whether there are non-zero elements is important,
hasNonZerois helpful.
See: countNonZero, hasNonZero
findNonZero(src: Mat, idx: Mat): void;srcsingle-channel array
idxOutput destination, filled by the native operation. the output array, type of cv::Mat or std::vector<Point>, corresponding to non-zero indices in the input
These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.