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