findnonzero() 中存储向量<Point>中非零元素的坐标时出错



我试图将Mat imag1的非零元素索引存储到向量vp1中,但它显示了cv::Exception at memory location错误。当mat不包含任何非零元素时,就会发生这种情况。下面是示例代码。从img中找到非零元素索引并将其存储在vp中是成功的,但从im1到vp1存储非零元素索引会显示错误。任何帮助解决这个问题将不胜感激。我想要向量点的坐标,因为我的算法的其余部分是基于它运行的。

#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;
}

此错误是由于cv::Mat中没有非零元素。我想在更新的版本中已经更正了。

虽然它增加了复杂性,但我给出了一个简单的解决方案(正如@berak在评论中解释的那样)

vector<Point> locations;
int count = countNonZero(binaryMat);
if(count < 0)
{
    findNonZero(binaryMat,locations);
}

最新更新