findnonzero() 中存储向量<Point>中非零元素的坐标时出错
Error in findnonzero() while storing the coordinates of non zero elements in vector<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);
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 使用函数"remove"删除重复元素
- 具有最大子序列大小的序列,每个元素都相同
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将元素添加到数组的线程安全函数?
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 我想访问std::unique_ptr中的一个特定元素
- C 使用坐标结构的访问阵列元素
- C++:带有坐标的矢量.如何删除重复项以及它们之间的所有元素
- findnonzero() 中存储向量<Point>中非零元素的坐标时出错