Search code examples
c++opencvfixed-point

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


I have some algorithm in OpenCV and I want to rewrite it using fixed point value representation. I found class for fixed point arithmetic here: https://github.com/eteran/cpp-utilities. I'd like to know if there's some elegant way to use Mat_ template class with Fixed class objects (or any custom class) as contents of Mat. When I use:

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

I'm getting following errors:

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

Solution

  • As it was mentioned in comments, I needed to define template specialization:

    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)
        };
    };
    

    So far, it seems to work fine. For example, I can write:

    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;
    

    And this will output:

    0.0
    2.375