OpenCV不同类型Mat的at方法访问元素时该如何确定模板函数的typename
2015-05-04 20:38
288 查看
自从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' }; };
我是个新手,如果说的不对还希望路过的大大不要笑话。
比如:
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' }; };
我是个新手,如果说的不对还希望路过的大大不要笑话。
相关文章推荐
- opencv2.x新数据结构cv::Mat访问元素的方法
- opencv2.x新数据结构cv::Mat访问元素的方法
- OpenCV:Mat元素访问方法、性能、代码复杂度以及安全性分析
- OpenCV:Mat元素访问方法、性能、代码复杂度以及安全性分析
- opencv Mat 类型数据传递,出现访问冲突,有解决方法,但不知原因
- OpenCV:Mat元素访问方法、性能
- OpenCV:Mat元素访问方法、性能、代码复杂度以及安全性分析
- opencv2.x新数据结构cv::Mat访问元素的方法
- OpenCV:Mat元素访问方法、演出、代码的复杂性和安全性分析
- 一个list中包含不同的type时如何遍历?页面如何接收?Jquery调用后台方法?
- Ubuntu 编译opencv出错:‘GstMiniObjectClass’ does not name a type 解决方法
- cv::Mat访问元素的方法
- 如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较
- OpenCV几种访问cv::Mat数据的方法
- 21. SpringMVC_如何确定目标方法POJO类型参数(无写笔记)
- opencv Mat 显示不同数据类型图像
- Exception:两个类具有相同的 XML 类型名称,请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称
- 确定 OpenCV 矩阵元素的数据类型
- 学习openCV之Mat类型访问每个像素
- Opencv 访问Mat中像素的值的快速方法