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
. But this returns the following error:
这被称为传递std :: vector
../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. The only solution that comes to my mind is using templates, so const std::vector<T> &mats
. Any other solution?
一个简单的解决方法可能是使用std :: vector
2 个解决方案
#1
0
A Mat1f
is convertible to a Mat
, but a vector<Mat1f>
is not convertible to a vector<Mat>
.
Mat1f可转换为Mat,但向量
A simple workaround is to copy your vector<Mat1f>
to the correct 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);
#2
0
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> >
toconst std::vector<cv::Mat>&
参数2从std :: vector
>转换为const std :: vector &
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
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 matFelement;
std::vector<cv::Mat> mats
mats.push_back(matFelement); //object sliced form Mat1f to Mat
#1
0
A Mat1f
is convertible to a Mat
, but a vector<Mat1f>
is not convertible to a vector<Mat>
.
Mat1f可转换为Mat,但向量
A simple workaround is to copy your vector<Mat1f>
to the correct 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);
#2
0
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> >
toconst std::vector<cv::Mat>&
参数2从std :: vector
>转换为const std :: vector &
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
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 matFelement;
std::vector<cv::Mat> mats
mats.push_back(matFelement); //object sliced form Mat1f to Mat