1、背景
#include <iostream>
using namespace std;
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
int rows = 5, cols = 5;
Mat m = Mat::eye(rows, cols, DataType<Complexf>::type);
for (size_t i = 0; i < rows; ++i)
{
for (size_t j = 0; j < cols; ++j)
{
cout << m.at<Complexf>(i, j).re << "," << m.at<Complexf>(i, j).im << " ";
}
cout << endl;
}
cout << endl
<< DataType<Complexf>::depth << endl;
return 0;
}
p70.cpp: In function 'int main()':
p70.cpp:9:54: error: 'type' is not a member of 'cv::DataType<cv::Complex<float> >'
Mat m = Mat::eye(rows, cols, DataType<Complexf>::type);
p70.cpp:18:33: error: 'depth' is not a member of 'cv::DataType<cv::Complex<float> >'
cout << DataType<Complexf>::depth << endl;
2、解决方法
打开DataType的定义可发现,DataType< Complex<_Tp> >确实含有成员type和depth,但其要求OPENCV_TRAITS_ENABLE_DEPRECATED
已定义。
template<typename _Tp> class DataType< Complex<_Tp> >
{
public:
typedef Complex<_Tp> value_type;
typedef value_type work_type;
typedef _Tp channel_type;
enum { generic_type = 0,
channels = 2,
fmt = DataType<channel_type>::fmt + ((channels - 1) << 8)
#ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
,depth = DataType<channel_type>::depth
,type = CV_MAKETYPE(depth, channels)
#endif
};
typedef Vec<channel_type, channels> vec_type;
};
方法1:
故可在打开include/opencv2/core/traist.hpp
文件,将// #define OPENCV_TRAITS_ENABLE_DEPRECATED
改为#define OPENCV_TRAITS_ENABLE_DEPRECATED
方法2:
按depth和type的定义修改程序,即将DataType<Complexf>::depth
修改为DataType<float>::depth
,将DataType<Complexf>::type
修改为CV_MAKETYPE(DataType<float>::depth, 2)
。
#include <iostream>
using namespace std;
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
int rows = 5, cols = 5;
Mat m = Mat::eye(rows, cols, CV_MAKETYPE(DataType<float>::depth, 2));
for (size_t i = 0; i < rows; ++i)
{
for (size_t j = 0; j < cols; ++j)
{
cout << m.at<Complexf>(i, j).re << "," << m.at<Complexf>(i, j).im << " ";
}
cout << endl;
}
cout << endl
<< DataType<float>::depth << endl;
return 0;
}