OpenCV不同类型Mat的at方法访问元素时该如何确定模板函数的typename

自从OpenCV推出了Mat后越来越像是Matlab了,使用起来方便了很多,但是,在用at方法访问Mat时,如何选用合适的typename类型来访问相应的Mat元素是个头疼的问题。

比如:


int Height = 100;

int Width = 100;

Mat  ImageMat8U(Height, Width, CV_8UC1);

for(int i=0; i<Height; i++)

{

for(int j=0; j<Width;j++)

{

ImageMat8U.at<uchar>(i, j) = (uchar)0; //编译可以通过,运行也正确

ImageMat8U.at<int>(i, j) = (uchar)0; //编译可以通过,运行时会发生asser错误

}

}


通常如果选错了typename,编译仍然可以通过,但是运行时总会发生assert错误,原因是无法通过depth检查。这里对不同类型的Mat使用at方法时对应的typename做个总结:

CV_8U: bool或者uchar 
CV_8S: schar或者char
CV_16U: ushort
CV_16S: short 
CV_32S: int或者unsigned
CV_32F: float
CV_64F: double

实际上数据depth的检查涉及到了一个模板类:


template<typename _Tp> class CV_EXPORTS DataDepth {};


这个类的特化就指定了Mat类型与typename类型之间的对应关系:


template<> class DataDepth<bool> { public: enum { value = CV_8U, fmt=(int)'u' }; };
template<> class DataDepth<uchar> { public: enum { value = CV_8U, fmt=(int)'u' }; };
template<> class DataDepth<schar> { public: enum { value = CV_8S, fmt=(int)'c' }; };
template<> class DataDepth<char> { public: enum { value = CV_8S, fmt=(int)'c' }; };
template<> class DataDepth<ushort> { public: enum { value = CV_16U, fmt=(int)'w' }; };
template<> class DataDepth<short> { public: enum { value = CV_16S, fmt=(int)'s' }; };
template<> class DataDepth<int> { public: enum { value = CV_32S, fmt=(int)'i' }; };
// this is temporary solution to support 32-bit unsigned integers
template<> class DataDepth<unsigned> { public: enum { value = CV_32S, fmt=(int)'i' }; };
template<> class DataDepth<float> { public: enum { value = CV_32F, fmt=(int)'f' }; };
template<> class DataDepth<double> { public: enum { value = CV_64F, fmt=(int)'d' }; };
template<typename _Tp> class DataDepth<_Tp*> { public: enum { value = CV_USRTYPE1, fmt=(int)'r' }; };


我是个新手,如果说的不对还希望路过的大大不要笑话。









  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值