[英]Using cv::Mat1f as cv::Mat
I have this method: 我有这种方法:
static void WriteMatVect(const std::string& filename, const std::vector<cv::Mat>& mats);
... ...
void FileSystem::WriteMatVect(const std::string& filename, const std::vector<cv::Mat>& mats){
size_t size = mats.size();
FileSystem::saveArray(&size,1,filename);
if(mats.empty()){
std::cerr<<"error WriteMatVect: no cv::Mat in mats"<<std::endl;
return;
}
for(size_t i=0 ; i<mats.size() ; i++)
FileSystem::WriteMat(filename, mats[i], true);
}
Which is called passing a std::vector<cv::Mat1f>
as mats
. 这被称为传递
std::vector<cv::Mat1f>
作为mats
。 But this returns the following error: 但这返回以下错误:
../FileSystem.hpp:28:14: note: no known conversion for argument 2 from ‘std::vector<cv::Mat_<float> >’ to ‘const std::vector<cv::Mat>&’
A simple workaround could be changing WriteMatVect
signature using std::vector<cv::Mat1f>& mats
, but this would make WriteMatVect
too strict (it would work only with float matrices), while I would like to do it as general as generic as possible. 一个简单的解决方法是使用
std::vector<cv::Mat1f>& mats
更改WriteMatVect
签名,但这会使WriteMatVect
过于严格(仅适用于浮点矩阵),而我WriteMatVect
泛型一样通用尽可能。 The only solution that comes to my mind is using templates, so const std::vector<T> &mats
. 我想到的唯一解决方案是使用模板,因此
const std::vector<T> &mats
。 Any other solution? 还有其他解决方案吗?
The problem: I think you are mixing derived with base class: 问题:我认为您正在混合派生自基类的内容:
(see the compiler error) (请参阅编译器错误)
no known conversion for argument 2 from
std::vector<cv::Mat_<float> >
没有从
std::vector<cv::Mat_<float> >
进行参数2的已知转换
toconst std::vector<cv::Mat>&
到
const std::vector<cv::Mat>&
template<typename _Tp> class Mat_ : public Mat
{
public:
// ... some specific methods
// and
// no new extra fields
};
Solution 1: The template class Mat_
is derived from Mat
, so maybe you can change your code from vector<cv::Mat>
to vector<cv::Mat*>
and the argument from std::vector<cv::Mat1f>
to std::vector<cv::Mat*>
, the downcasting will be implicit when you push the matrix to the vectors. 解决方案1:模板类
Mat_
派生自Mat
,因此也许您可以将代码从vector<cv::Mat>
更改为vector<cv::Mat*>
,并将参数从std::vector<cv::Mat1f>
到std::vector<cv::Mat*>
,当您将矩阵推到向量时,向下转换将是隐式的。
Then your method will be: 然后您的方法将是:
static void WriteMatVect(const std::string& filename, const std::vector<cv::Mat*>& mats);
[EDITED] Solution 2: Another possible workaround (if you prefer not to change the calling method) is to slice the objects in the std::vector<cv::Mat1f> mats
, so use a std::vector<cv::Mat> mats
instead, for example when you push the objects: [已编辑]解决方案2:另一个可能的解决方法(如果您不希望不更改调用方法)是在
std::vector<cv::Mat1f> mats
切片对象,因此请使用std::vector<cv::Mat> mats
,例如,当您推动对象时:
cv::Mat1f matFelement;
std::vector<cv::Mat> mats
mats.push_back(matFelement); //object sliced form Mat1f to Mat
A Mat1f
is convertible to a Mat
, but a vector<Mat1f>
is not convertible to a vector<Mat>
. Mat1f
可转换为Mat
,但vector<Mat1f>
不可转换为vector<Mat>
。
A simple workaround is to copy your vector<Mat1f>
to the correct vector<Mat>
. 一个简单的解决方法是将
vector<Mat1f>
复制到正确的vector<Mat>
。 Remember that data are not copied, so it shouldn't be that slow. 请记住,数据不会被复制,因此它不会那么慢。
vector<Mat1f> v;
...
vector<Mat> u;
u.reserve(v.size());
for(const auto& m : v) { u.push_back(m); }
WriteMatVect(u);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.