使用cv :: Mat1f作为cv :: Mat

时间:2022-10-26 23:01:55

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 &mats更改WriteMatVect签名,但这会使WriteMatVect过于严格(它只能用于浮动矩阵),而我想像通用那样做泛型尽可能。我想到的唯一解决方案是使用模板,所以const std :: vector &mats。还有其他方法吗?

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> >
to const 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 更改为vector 和std :: vector 中的参数对于std :: 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 垫子中的对象,所以使用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> >
to const 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 更改为vector 和std :: vector 中的参数对于std :: 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 垫子中的对象,所以使用std :: vector 垫,例如当您按下对象时:

cv::Mat1f matFelement;
std::vector<cv::Mat> mats
mats.push_back(matFelement); //object sliced form Mat1f to Mat