有没有办法在自定义类中使用OpenCV Mat_?

时间:2016-07-06 20:13:08

标签: c++ opencv fixed-point

我在OpenCV中有一些算法,我想用定点值表示法重写它。我在这里找到了定点算术类:https://github.com/eteran/cpp-utilities。我想知道是否有一些优雅的方法将Mat_ template类与Fixed类对象(或任何自定义类)一起用作Mat的内容。当我使用时:

cv::Mat_<cv::Vec<Fixed<12, 4>, 3>> num;

我遇到以下错误:

Error   C2039   'value' : is not a member of 'cv::DataDepth<numeric::Fixed<0x0c,0x04>>' opencv_hog  D:\libs\x64\opencv_2_4_13\build\include\opencv2\core\core.hpp   1134
Error   C2065   'value' : undeclared identifier opencv_hog  D:\libs\x64\opencv_2_4_13\build\include\opencv2\core\core.hpp   1134
Error   C2039   'fmt' : is not a member of 'cv::DataDepth<numeric::Fixed<0x0c,0x04>>'   opencv_hog  D:\libs\x64\opencv_2_4_13\build\include\opencv2\core\core.hpp   1135    
Error   C2065   'fmt' : undeclared identifier   opencv_hog  D:\libs\x64\opencv_2_4_13\build\include\opencv2\core\core.hpp   1135    
Error   C2056   illegal expression  opencv_hog  D:\libs\x64\opencv_2_4_13\build\include\opencv2\core\core.hpp   1135

1 个答案:

答案 0 :(得分:0)

正如评论中提到的,我需要定义模板专业化:

template<size_t I, size_t F> class cv::DataType<Fixed<I, F>>
{
public:
    typedef Fixed<I, F> value_type;
    typedef Fixed<I, F> work_type;
    typedef Fixed<I, F> channel_type;
    enum {
        channels = 1,
        depth = I + F,
        type = CV_MAKETYPE(depth, channels)
    };
};

到目前为止,似乎工作正常。例如,我可以写:

cv::Mat_<Fixed<13, 3>> fixed = cv::Mat_<Fixed<13, 3>>::zeros(10, 10);
fixed(1, 1) = 2.4;
std::cout << fixed(0, 0) << std::endl;
std::cout << fixed(1, 1) << std::endl;

这将输出:

0.0
2.375