mat数组元素的访问

1 通道为1时

首先还是要感谢箫鸣朋友在我《OpenCV学习笔记(四十)——再谈OpenCV数据结构Mat详解》的留言,告诉我M.at<float>(3, 3)在Debug模式下运行缓慢,推荐我使用M.ptr<float>(i)此类方法。这不禁勾起了我测试一下的冲动。下面就为大家奉上我的测试结果。

我这里测试了三种操作Mat数据的办法,套用流行词,普通青年,文艺青年,为啥第三种我不叫2b青年,大家慢慢往后看咯。

普通青年的操作的办法通常是M.at<float>(i, j)

文艺青年一般会走路线M.ptr<float>( i )[ j ]

暴力青年通常直接强制使用我第40讲提到的M.data这个指针

实验代码如下:

  1. t = (double)getTickCount();  
  2. Mat img1(1000, 1000, CV_32F);  
  3.   
  4. for (int i=0; i<1000; i++)  
  5. {  
  6.     for (int j=0; j<1000; j++)  
  7.     {  
  8.         img1.at<float>(i,j) = 3.2f;  
  9.     }  
  10. }  
  11. t = (double)getTickCount() - t;  
  12. printf("in %gms\n", t*1000/getTickFrequency());  
  13. //***************************************************************   
  14. t = (double)getTickCount();  
  15. Mat img2(1000, 1000, CV_32F);  
  16.   
  17. for (int i=0; i<1000; i++)  
  18. {  
  19.     for (int j=0; j<1000; j++)  
  20.     {  
  21.         img2.ptr<float>(i)[j] = 3.2f;  
  22.     }  
  23. }  
  24. t = (double)getTickCount() - t;  
  25. printf("in %gms\n", t*1000/getTickFrequency());  
  26. //***************************************************************   
  27. t = (double)getTickCount();  
  28. Mat img3(1000, 1000, CV_32F);  
  29. float* pData = (float*)img3.data;  
  30.   
  31. for (int i=0; i<1000; i++)  
  32. {  
  33.     for (int j=0; j<1000; j++)  
  34.     {  
  35.         *(pData) = 3.2f;  
  36.         pData++;  
  37.     }  
  38. }  
  39. t = (double)getTickCount() - t;  
  40. printf("in %gms\n", t*1000/getTickFrequency());  
  41. //***************************************************************   
  42. t = (double)getTickCount();  
  43. Mat img4(1000, 1000, CV_32F);  
  44.   
  45. for (int i=0; i<1000; i++)  
  46. {  
  47.     for (int j=0; j<1000; j++)  
  48.     {  
  49.         ((float*)img3.data)[i*1000+j] = 3.2f;  
  50.     }  
  51. }  
  52. t = (double)getTickCount() - t;  
  53. printf("in %gms\n", t*1000/getTickFrequency());  
	t = (double)getTickCount();
	Mat img1(1000, 1000, CV_32F);
	
	for (int i=0; i<1000; i++)
	{
		for (int j=0; j<1000; j++)
		{
			img1.at<float>(i,j) = 3.2f;
		}
	}
	t = (double)getTickCount() - t;
	printf("in %gms\n", t*1000/getTickFrequency());
	//***************************************************************
	t = (double)getTickCount();
	Mat img2(1000, 1000, CV_32F);

	for (int i=0; i<1000; i++)
	{
		for (int j=0; j<1000; j++)
		{
			img2.ptr<float>(i)[j] = 3.2f;
		}
	}
	t = (double)getTickCount() - t;
	printf("in %gms\n", t*1000/getTickFrequency());
	//***************************************************************
	t = (double)getTickCount();
	Mat img3(1000, 1000, CV_32F);
	float* pData = (float*)img3.data;

	for (int i=0; i<1000; i++)
	{
		for (int j=0; j<1000; j++)
		{
			*(pData) = 3.2f;
			pData++;
		}
	}
	t = (double)getTickCount() - t;
	printf("in %gms\n", t*1000/getTickFrequency());
	//***************************************************************
	t = (double)getTickCount();
	Mat img4(1000, 1000, CV_32F);

	for (int i=0; i<1000; i++)
	{
		for (int j=0; j<1000; j++)
		{
			((float*)img3.data)[i*1000+j] = 3.2f;
		}
	}
	t = (double)getTickCount() - t;
	printf("in %gms\n", t*1000/getTickFrequency());

最后两招可以都看成是暴力青年的方法,因为反正都是指针的操作,局限了各暴力青年手段就不显得暴力了。

在Debug、Release模式下的测试结果分别为:

测试结果
 DebugRelease
普通青年139.06ms2.51ms
文艺青年66.28ms2.50ms
暴力青年14.95ms2.28ms
暴力青年25.11ms1.37ms

根据测试结果,我觉得箫铭说的是很可信的,普通青年的操作在Debug模式下果然缓慢,他推荐的文艺青年的路线确实有提高。值得注意的是本来后两种办法确实是一种比较2b青年的做法,因为at操作符或者ptr操作符,其实都是有内存检查的,防止操作越界的,而直接使用data这个指针确实很危险。不过从速度上确实让人眼前一亮,所以我不敢称这样的青年为2b,尊称为暴力青年吧。

不过在Release版本下,几种办法的速度差别就不明显啦,都是很普通的青年。所以如果大家最后发行程序的时候,可以不在意这几种操作办法的,推荐前两种哦,都是很好的写法,操作指针的事还是留给大神们用吧。就到这里吧~~

补充:箫铭又推荐了两种文艺青年的处理方案,我也随便测试了一下,先贴代码,再贴测试结果:

  1. /*********加强版********/  
  2. t = (double)getTickCount();  
  3. Mat img5(1000, 1000, CV_32F);  
  4. float *pData1;  
  5. for (int i=0; i<1000; i++)   
  6. {   
  7.     pData1=img5.ptr<float>(i);  
  8.     for (int j=0; j<1000; j++)   
  9.     {   
  10.         pData1[j] = 3.2f;   
  11.     }   
  12. }   
  13. t = (double)getTickCount() - t;  
  14. printf("in %gms\n", t*1000/getTickFrequency());  
  15. /*******终极版*****/  
  16. t = (double)getTickCount();  
  17. Mat img6(1000, 1000, CV_32F);  
  18. float *pData2;  
  19. Size size=img6.size();  
  20. if(img2.isContinuous())  
  21. {  
  22.     size.width = size.width*size.height;  
  23.     size.height = 1;  
  24. }  
  25. size.width*=img2.channels();  
  26. for(int i=0; i<size.height; i++)  
  27. {  
  28.     pData2 = img6.ptr<float>(i);  
  29.     for(int j=0; j<size.width; j++)  
  30.     {  
  31.         pData2[j] = saturate_cast<float>(3.2f);  
  32.     }  
  33. }  
  34. t = (double)getTickCount() - t;  
  35. printf("in %gms\n", t*1000/getTickFrequency());  
	/*********加强版********/
	t = (double)getTickCount();
	Mat img5(1000, 1000, CV_32F);
	float *pData1;
	for (int i=0; i<1000; i++) 
	{ 
		pData1=img5.ptr<float>(i);
		for (int j=0; j<1000; j++) 
		{ 
			pData1[j] = 3.2f; 
		} 
	} 
	t = (double)getTickCount() - t;
	printf("in %gms\n", t*1000/getTickFrequency());
	/*******终极版*****/
	t = (double)getTickCount();
	Mat img6(1000, 1000, CV_32F);
	float *pData2;
	Size size=img6.size();
	if(img2.isContinuous())
	{
		size.width = size.width*size.height;
		size.height = 1;
	}
	size.width*=img2.channels();
	for(int i=0; i<size.height; i++)
	{
		pData2 = img6.ptr<float>(i);
		for(int j=0; j<size.width; j++)
		{
			pData2[j] = saturate_cast<float>(3.2f);
		}
	}
	t = (double)getTickCount() - t;
	printf("in %gms\n", t*1000/getTickFrequency());

测试结果:

 DebugRelease
加强版文艺青年5.74ms2.43ms
终极版文艺青年40.12ms2.34ms
我的测试结果感觉这两种方案只是锦上添花的效果,也使大家的操作有了更多的选择,但感觉在速度上并没有数量级的提升,再次感谢箫铭对我blog的支持。后来箫铭说saturate_cast才把速度降下来,我很同意,就不贴上去测试结果了。但我查看资料了解了一下 saturate_cast的作用。 可以看成是类型的强制转换,比如对于saturate_cast<uchar>来说,就是把数据转换成8bit的0~255区间,负值变成0,大于255的变成255。如果是浮点型的数据,变成round最近的整数,还是很有用处的函数,推荐大家在需要的时候尝试。

 

Mat a=Mat(4,3, CV_32FC1);

floatelem_a=a.at<float>(i,j);//access element aij, with i from 0 to rows-1 and j from 0 to cols-1

Point p=Point(x,y);

floatelem_a=a.at<float>(p);//Warning: y ranges from 0 to rows-1 and x from 0 to cols-1

 

 

2 通道为2时 或是多通道时

 

template<typename _Tp> _Tp& at(int y,int x);                // cxcore.hpp (868)
template<typename _Tp>const _Tp& at(int y,int x)const;    // cxcore.hpp (870)
template<typename _Tp> _Tp& at(Point pt);                    // cxcore.hpp (869)
template<typename _Tp>const _Tp& at(Point pt)const;        // cxcore.hpp (871)
// defineded in cxmat.hpp (454-468)
typedefVec<float,2>Vec2f;// cxcore.hpp (254)

// we can access the element like this :
Mat m(Size(3,3), CV_32FC2 );
Vec2f& elem = m.at<Vec2f>( row , col );// or m.at<Vec2f>( Point(col,row) );
elem[0]=1212.0f;
elem[1]=326.0f;
float c1 = m.at<Vec2f>( row , col )[0];// or m.at<Vec2f>( Point(col,row) );
float c2 = m.at<Vec2f>( row , col )[1];
m.at<Vec2f>( row, col )[0]=1986.0f;
m.at<Vec2f>( row, col )[1]=326.0f;

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值