有没有办法将 OpenCV Mat_ 与自定义 class 一起使用?

Is there a way to use OpenCV Mat_ with custom class?

我在 OpenCV 中有一些算法,我想用定点值表示重写它。我在这里找到了定点运算的 class:https://github.com/eteran/cpp-utilities。我想知道是否有一些优雅的方法可以使用带有固定 class 对象(或任何自定义 class)的 Mat_ 模板 class 作为 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

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

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