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:
../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?
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>&
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.
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:
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>
.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.