学习OpenCV3: DataType「Complexf」无法访问成员type和depth

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 >’
Mat m = Mat::eye(rows, cols, DataType::type);
p70.cpp:18:33: error: ‘depth’ is not a member of ‘cv::DataType<cv::Complex >’
cout << DataType::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;
}

运行结果

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值