如何填充矩阵与零在OpenCV? [英] How to fill Matrix with zeros in OpenCV?

查看:1099
本文介绍了如何填充矩阵与零在OpenCV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码导致异常。为什么?

  #include< opencv2 / core / core.hpp> 
#include< iostream>

using namespace cv;
using namespace std;

void main(){

try {
Mat m1 = Mat(1,1,CV_64F,0);
m1.at< double>(0,0)= 0;
}
catch(cv :: Exception& e){
cerr<< e.what()< endl;
}

}

错误如下:

  OpenCV错误:断言失败(dims <= 2&&&&&(无符号)i0 < size.p [0]&&(unsigned)(i1 * DataType <_Tp> :: channels)<(unsigned)(size.p [1] * channels())& (size_t)<< 28)| 0x8442211)>((DataType< _Tp> :: depth)&((1≤j3
)-1) 15)== elemSize1())未知函数,文件%OPENCV_DIR%\build\include\opencv2\core\mat.hpp,第537行

UPDATE



如果跟踪此代码,我看到构造函数行调用构造函数

  inline Mat :: Mat(int _rows,int _cols,int _type,void * _data,size_t _step)

为什么?因为最后一个参数是可选的,并且数据指针应该是可选的,因为这个参数是可选的。在某处适当:

  // inline Mat :: Mat(int _rows,int _cols,int _type,void * _data,size_t _step)
double mydata [1];
Mat m1 = Mat(1,1,CV_64F,mydata);
m1.at< double>(0,0)= 0;

但是最好直接使用这个基于模板的构造函数:

  // inline Mat :: Mat(int _rows,int _cols,int _type,const Scalar& _s)
Mat m1 = Mat CV_64F,cvScalar(0。));

//或甚至
Mat m1 = Mat(1,1,CV_64F,double(0));


The code below causes an exception. Why?

#include <opencv2/core/core.hpp>
#include <iostream>

using namespace cv;
using namespace std;

void main() {

    try {
        Mat m1 = Mat(1,1, CV_64F, 0);
        m1.at<double>(0,0) = 0;
    }
    catch(cv::Exception &e) {
        cerr << e.what() << endl;
    }

}

Error is follows:

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3
) - 1))*4) & 15) == elemSize1()) in unknown function, file %OPENCV_DIR%\build\include\opencv2\core\mat.hpp, line 537

UPDATE

If tracing this code, I see that constructor line calls the constructor

inline Mat::Mat(int _rows, int _cols, int _type, void* _data, size_t _step)

Why? This prototype has 5 parameters, while I am providing 4 arguments.

解决方案

Because the last parameter is optional and also the data pointer should point somewhere appropriate:

//inline Mat::Mat(int _rows, int _cols, int _type, void* _data, size_t _step)
double mydata[1];
Mat m1 = Mat(1,1, CV_64F, mydata); 
m1.at<double>(0,0) = 0;

But better do it directly with this template-based constructor:

//inline Mat::Mat(int _rows, int _cols, int _type, const Scalar& _s)
Mat m1 = Mat(1,1, CV_64F, cvScalar(0.));

//or even
Mat m1 = Mat(1,1, CV_64F, double(0));

这篇关于如何填充矩阵与零在OpenCV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆