将 OpenCV-Matrix 转换为矢量
Cast OpenCV-Matrix to vector
本文关键字:转换 OpenCV-Matrix 更新时间:2023-10-16
如何将OpenCV矩阵转换为尽可能多的OpenCV类型的std::vector
(例如cv::Mat_<T>
,cv::Matx<T,M,N>
(?以下内容似乎不起作用,因为cv::Matx
没有begin
方法:
template <class T>
inline void foo(const T& data)
{
using value_type = typename T::value_type;
std::vector<value_type> v(data.begin(), data.end());
}
在@rafix07的建议下,我找到了解决方案:
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
template <class... T>
struct make_void
{
using type = void;
};
template <class... T>
using void_t = typename make_void<T...>::type;
template <class E, class = void>
struct has_iterator_interface : std::false_type
{
};
template <class E>
struct has_iterator_interface<E, void_t<decltype(std::declval<E>().begin())>>
: std::true_type
{
};
template <class T, typename = void>
struct as_vector
{
inline static auto get(const T& t)
{
using value_type = typename T::value_type;
cv::Mat_<value_type> s(t, false);
return std::vector<value_type>(s.begin(), s.end());
}
};
template <class T>
struct as_vector<T, typename std::enable_if_t<has_iterator_interface<T>::value>>
{
inline static auto get(const T& t)
{
using value_type = typename T::value_type;
return std::vector<value_type>(t.begin(), t.end());
}
};
int main()
{
cv::Mat_<double> A(2, 3, 0.0);
A(0, 0) = 0.0;
A(0, 1) = 1.0;
A(0, 2) = 2.0;
A(1, 0) = 3.0;
A(1, 1) = 4.0;
A(1, 2) = 5.0;
cv::Matx<double, 3, 4> B;
B(0, 0) = 0.0;
B(0, 1) = 1.0;
B(0, 2) = 2.0;
B(1, 0) = 3.0;
B(1, 1) = 4.0;
B(1, 2) = 5.0;
std::vector<double> a = as_vector<decltype(A)>::get(A);
std::vector<double> b = as_vector<decltype(B)>::get(B);
}
但是,我希望有更简单的东西。
相关文章:
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 如何将 c++ get 函数代码转换为 opencv 算法中使用的 python
- 将 OpenCV 2 中的ANN_MLP代码转换为 OpenCV 3 代码
- OpenCV - 如何将灰度转换为彩色(CV_8UC3)?
- 将 OpenCV-Matrix 转换为矢量
- 将 opencv 从 2.4.13 转换为 3.3.1 时由 cv::resize() 引起的运行错误
- 从 16UC3 到 8UC3 的高性能 OpenCV 矩阵转换
- HALCON到OpenCV失真系数的转换
- 将节点数组(可变长度)转换为常量浮点**以调用opencv.calcHist
- 如何将垫子转换为 int?(c++,gpu,OpenCV)
- 如何将OpenCV图像数据从Python转换为C++?
- 在 Eigen::Matrix 中转换 dolfin::Matrix
- Emgucv 如何将 FileNode 转换为 Matrix
- 错误 C2679:二进制'=':未找到采用类型 'IplImage *' 的右侧操作数的运算符(或没有可接受的转换) OpenCV
- 如何转换OpenCV垫卤化物图像和回来
- 转换OpenCV灰度垫Caffe blob
- 通过强制转换OpenCV UMat的句柄实例化OpenCL cl_mem
- 图像格式转换opencv
- 相机校准和坐标转换(OpenCV)
- Android OpenCV -转换OpenCV c++代码到Android,以检测QR码对齐方块