[英]OpenCV vector of vectors into cv::Mat
I have certain values in an std::vector<std::vector<double>>
structure of n rows and m cols which I would like to convert into an equivalent cv::Mat object. 我在n行和m col的
std::vector<std::vector<double>>
结构中有某些值,我想将其转换为等效的cv :: Mat对象。 This is the code I have been using but I'm getting an error: 这是我一直在使用的代码,但出现错误:
dctm is a local argument which is defined as: std::vector<std::vector<double>>
dctm是一个局部参数,定义为:
std::vector<std::vector<double>>
cv::Mat dctmat = cvCreateMat(dctm.size(), dctm[0].size(), CV_16SC1);
for (size_t i = 0; i < dctm.size(); i++) {
for (size_t j = 0; j < dctm[i].size(); j++) {
dctmat.at<double>(i, j) = dctm[i][j];
}
}
dctmat
has a type CV_16SC1
which means matrix of signed short. dctmat
的类型为CV_16SC1
,表示带符号的short矩阵。 But you try to access it with dctmat.at<double>(i, j)
which is incoherent. 但是您尝试使用不连贯的
dctmat.at<double>(i, j)
访问它。 Either define your matrix as CV_64FC1
or access it with dctmat.at<short>(i, j)
(first solution is better because you have a vector of double
). 将您的矩阵定义为
CV_64FC1
或使用dctmat.at<short>(i, j)
(第一个解决方案更好,因为您有double
的向量)。
Here is my templated version of conversion routines 这是我转换例程的模板化版本
routines 例程
template<typename _Tp> static cv::Mat toMat(const vector<vector<_Tp> > vecIn) {
cv::Mat_<_Tp> matOut(vecIn.size(), vecIn.at(0).size());
for (int i = 0; i < matOut.rows; ++i) {
for (int j = 0; j < matOut.cols; ++j) {
matOut(i, j) = vecIn.at(i).at(j);
}
}
return matOut;
}
template<typename _Tp> static vector<vector<_Tp> > toVec(const cv::Mat_<_Tp> matIn) {
vector<vector<_Tp> > vecOut(matIn.rows);
for (int i = 0; i < matIn.rows; ++i) {
vecOut[i].resize(matIn.cols);
for (int j = 0; j < matIn.cols; ++j) {
vecOut[i][j] = matIn.at<_Tp>(i, j);
}
}
return vecOut;
}
Here in the Opencv documentation you can found this: 这里 OpenCV的文档中,你可以发现这一点:
Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP)
Mat::Mat(Size size, int type, void* data, size_t step=AUTO_STEP)
...
then, you can use like this: 然后,您可以这样使用:
Mat dctmat(dctm.size(), dctm[0].size(), CV_16SC1, dctm.data());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.