OpenCV Mat element types and their sizes

后端 未结 6 1756
滥情空心
滥情空心 2020-12-05 10:08

I\'m confused by the OpenCV Mat element types. This is from the docs:

There is a limited fixed set of primitive data types the library can operate on.
That i         


        
6条回答
  •  春和景丽
    2020-12-05 10:35

    While C++ doesn't define the size of an element, the question is hypothetical: for systems OpenCV is run on, the sizes are known. Given

    cv::Mat m(32,32,CV_32SC1, cv:Scalar(0));
    std::cout << "size of the element in bytes: " << m.depth() << std::endl;
    std::cout << "or " << m.step.p[ m.dims-1 ]/m.channels() << std::endl;
    

    So how can you be sure it is int?

    An attempt to call

    int pxVal = m.at(0,0);
    

    will

    CV_DbgAssert( elemSize()==sizeof(int) );
    

    Where the left hand is defined via the cv::Mat::flags -- in this example as the predefined depth of the CV_32SC1 equal to

    CV_DbgAssert( m.depth() == sizeof(int) )
    

    or

    CV_DbgAssert( 4 == sizeof(int) )
    

    So if you succeeded you are left only the endianness. And that was checked when the cvconfig.h was generated (by CMake).

    TL;DR, expect the types given in the header and you'll be fine.

提交回复
热议问题