我试图将mat img1的非零元素索引存储到向量vp1中,但它显示cv::Exception at memory location
错误。当垫子不包含任何非零元素时会发生这种情况。示例代码如下。从img中找到非零元素指示并存储在vp中是成功的,但是存储从img1到vp1的非零元素指示显示错误。任何帮助解决这个问题将不胜感激。我只想要Vector of point中的坐标,因为我的其余算法都是基于它运行的。
#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;
}
答案 0 :(得分:2)
此错误是因为cv :: Mat中没有非零元素。 我认为它已在更新版本中得到纠正。
虽然它增加了复杂性,但我给出了一个简单的解决方案(正如@berak在评论中解释的那样)
vector<Point> locations;
int count = countNonZero(binaryMat);
if(count < 0)
{
findNonZero(binaryMat,locations);
}