开发者

opencv Mat datatype conversion and arithmetic

I am new to the opencv library and still confused about the C++ Mat class since 2.x.

The problems I faced:

  1. non-template version of class Mat is runtime typed, but there are no functions that can show the symbolic name of a Mat object, there is only a cv::Mat::type(), which returns integer.

  2. If a matrix is of CV_8U, or unsigned char type, then I do addition arithmetic...does it has the possibility of causing overflow? or opencv is smart enough to convert datatype and do so called "saturated arithmetic"? e.g.

    unsigned char a = 255; unsigned char b = a+a; // == 255

    operations of a+a:

    unsigned char b = (unsigned char) clipTo_0_255((float)a + (float)a));

  3. If I want to avoid subtle cv::Mat::at<_Tp>, most solutions suggest using derived template version of class Mat_<_Tp>. But _Tp must C++ primitive type and I don't know the mapping between primitive type and CvMat type system... (Maybe it is straightforward...)

  4. Is开发者_开发问答 opencv datatype conversion equivalent to C/C++ type casting? e.g.

    float a = 1.5; int b = (int) a; // == 1

    int a = 2; float b = static_cast<int>(a); // == 2.0


For both #2 and #4, you should read the OpenCV page on saturation arithmetic. In particular:

This “saturation” semantics (different from usual C language “wrapping” semantics, where lowest bits are taken, is implemented in every image processing function, from the simple cv::add to cv::cvtColor, cv::resize, cv::filter2D etc. It is not a new feature of OpenCV v2.x, it was there from very beginning. In the new version this special template operator is introduced to simplify implementation of this semantic in your own functions.

I unfortunately do not have any great solutions to your issues with the type system. Because all of the OpenCV library functions use the non-templated cv::Mat class, I personally have given up on using cv::Mat<T> class until the rest of the API is updated.

While the type system is fairly awkward, it becomes much easier to use as you grow accustom to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜