Error in findnonzero () while saving coordinates of nonzero elements in the <Point> vector
I tried to store the indices of nonzero elements of Mat img1 in the vp1 vector, but it shows an error cv::Exception at memory location. This happens when the mat does not contain a nonzero element. The following is sample code. where detecting nonzero elements from img and saving to vp is successful, but saving nonzero elements pointing from img1 to vp1 shows an error. Any help to solve this problem would be greatly appreciated. I want the coordinates in the point vector to be obtained only because the rest of the algorithm is based on it.
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main() {
Mat img(10, 10, CV_8U, Scalar::all(0));
img.at<uchar>(0,2)=1;
vector<Point> vp;
findNonZero(img, vp);
Mat img1(10, 10, CV_8U, Scalar::all(0));
vector<Point> vp1;
findNonZero(img1, vp1);
return 0;
}
+4