如何将 vector<...> 转换为 cv::Mat?
How to convert vector<...> to cv::Mat?
具有vector<Descriptor> m_keyDescs
描述符指定为:
Descriptor(float x, float y, vector<double> const& f)
{
xi = x;
yi = y;
fv = f;
}
推送方式:
m_keyDescs.push_back(Descriptor(descxi, descyi, fv));
如何将此矢量转换为cv::Mat?
我试过
descriptors_scene = cv::Mat(m_keyDescs).reshape(1);
项目调试没有错误,但当它运行时,在我的mac上的Qt Creator中出现错误:
测试意外退出单击"重新打开"以再次打开应用程序。
您无法将手动定义类的向量直接转换为Mat。例如,OpenCV不知道将每个元素放在哪里,并且元素甚至不是相同的变量类型(第三个甚至不是单个元素,因此它不可能是Mat中的元素)。但是,例如,可以将int或float的向量直接转换为Mat。请在此处查看答案中的更多信息。
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
class Descriptor {
public:
float xi;
float yi;
vector< double > fv;
Descriptor(float x, float y, vector<double> const& f) :
xi(x), yi(y), fv(f){}
};
int main(int argc, char** argv) {
vector<Descriptor> m_keyDescs;
for (int i = 0; i < 10; i++) {
vector<double> f(10, 23);
m_keyDescs.push_back(Descriptor(i+3, i+5, f));
}
Mat_<Descriptor> mymat(1, m_keyDescs.size(), &m_keyDescs[0], sizeof(Descriptor));
for (int i = 0; i < 10; i++) {
Descriptor d = mymat(0, i);
cout << "xi:" << d.xi << ", yi:" << d.yi << ", fv:[";
for (int j = 0; j < d.fv.size(); j++)
cout << d.fv[j] << ", ";
cout << "]" << endl;
}
}
相关文章:
- 将CHW格式的浮点向量转换为cv::Mat
- C++:从GPU内存(cudaMemcpy2D)获取BGR图像(cv::Mat)
- 选择基于另一个垫子的非零像素的cv::Mat的一部分?
- 将 cv::mat 转换为 QImage
- 将 cv::Mat 转换为 std::vector 的通用函数
- 在 QML VideoOutput 中将 cv::mat 显示为 QVideoFrame
- 如何将 cv::Mat 转换为 vtkImageData?
- 如何将 vtkImageData 转换为 cv::Mat?
- 如何总结所有cv::Mat元素
- Qt-工作线程崩溃时将cv::Mat转换为QImage
- 在 cv::Mat 中生成 2D 坐标网格
- 将 cv::Mat 转换为无符号的 int 像素
- OpenCV 分段错误(核心转储)在使用 cv::Mat::at 时
- 如何将多个 cv::Mat 从 c++ dll 传递到 opencvsharp Mat c#?
- 如何在不复制数据的情况下将 cv::Mat 转换为 2d 标准::矢量
- 如何使用 (*mpORBextractorLeft)(im,cv::Mat(),mvKeys,mDescriptors)
- 从数组创建 Mat 时,'cv::Mat' 和 'int' 类型不兼容
- 如何将 cv::mat 对象从 python 模块传递到 c++ 函数并返回返回的 cv::mat 类型为对象?
- 移动语义和 cv::Mat
- OpenCV 错误:断言在 cv::Mat 行 522 中失败