Opencv Mat篇

1:Mat 构造函数


You can do this using the << operator of Mat. Be aware that this only works for two dimensional matrices.
    * Mat() Constructor

        Mat M(2,2, CV_8UC3, Scalar(0,0,255));
cout << "M = " << endl << " " << M << endl << endl;

* Use C/C++ arrays and initialize via constructor

int sz[3] = {2,2,2};
Mat L(3,sz, CV_8UC(1), Scalar::all(0));
The upper example shows how to create a matrix with more than two dimensions. Specify its dimension, thenpass a pointer containing the size for each dimension and the rest remains the same.
       //创建了一个3维的矩阵,具体的是2X2X2的矩阵,类型和值和默认构造函数的相同。
    
* Create a header for an already existing IplImage pointer:
      
         IplImage* img = cvLoadImage("greatwave.png", 1);
    Mat mtx(img); // convert IplImage* -> Mat    

* Create() function:
M.create(4,4, CV_8UC(2));
cout << "M = "<< endl << " " << M << endl << endl;

* MATLAB style initializer: zeros(), ones(), eye(). Specify size and data type to use:
Mat E = Mat::eye(4, 4, CV_64F);
cout << "E = " << endl << " " << E << endl << endl;
Mat O = Mat::ones(2, 2, CV_32F);
cout << "O = " << endl << " " << O << endl << endl;
Mat Z = Mat::zeros(3,3, CV_8UC1);
cout << "Z = " << endl << " " << Z << endl << endl;

* For small matrices you may use comma separated initializers:
Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
cout << "C = " << endl << " " << C << endl << endl;

*Create a new header for an existing Mat object and clone() or copyTo() it.
Mat RowClone = C.row(1).clone();
cout << "RowClone = " << endl << " " << RowClone << endl << endl;

* Note: You can fill out a matrix with random values using the randu() function. You need to give the lower and upper
value for the random values:
Mat R = Mat(3, 2, CV_8UC3);
randu(R, Scalar::all(0), Scalar::all(255));


还有一种快速初始化数据的办法,如下:

[cpp]  view plain copy
  1. double m[3][3] = {{a, b, c}, {d, e, f}, {g, h, i}};  
  2. Mat M = Mat(3, 3, CV_64F, m).inv();  



2:数据访问方式 Mat mat ;float* lambda  = mat.ptr<float>(i)


得到指针 ,访问数据的方式是:lambda[0] , lambda[1]等. 


还是先看Mat的存储形式。Mat和Matlab里的数组格式有点像,但一般是二维向量,如果是灰度图,一般存放 <uchar>类型;如果是RGB彩色图,存放 <Vec3b>类型。
单通道灰度图数据存放格式:

多通道的图像中,每列并列存放通道数量的子列,如RGB三通道彩色图:

注意通道的顺序反转了:BGR。通常情况内存足够大的话图像的每一行是连续存放的,也就是在内存上图像的所有数据存放成一行,这中情况在访问时可以提供很大方便。可以用  isContinuous()函数来判断图像数组是否为连续的。

访问图像中的像素


高效的方法:C操作符[ ]

最快的是直接用C风格的内存访问操作符[]来访问:
Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
{
	// accept only char type matrices
	CV_Assert(I.depth() != sizeof(uchar));
	int channels = I.channels();
	int nRows = I.rows ;
	int nCols = I.cols* channels;
	if (I.isContinuous())
	{
		nCols *= nRows;
		nRows = 1;
	}
	int i,j;
	uchar* p;
	for( i = 0; i < nRows; ++i)
	{
		p = I.ptr<uchar>(i);
		for ( j = 0; j < nCols; ++j)
		{
			p[j] = table[p[j]];
		}
	}
	return I;
}

安全的方法:迭代器iterator

相比用指针直接访问可能出现越界问题,迭代器绝对是非常安全的方法:

Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table)
{
	// accept only char type matrices
	CV_Assert(I.depth() != sizeof(uchar));
	const int channels = I.channels();
	switch(channels)
	{
	case 1:
		{
			MatIterator_<uchar> it, end;
			for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)
				*it = table[*it];
			break;
		}
	case 3:
		{
			MatIterator_<Vec3b> it, end;
			for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it)
			{
				(*it)[0] = table[(*it)[0]];
				(*it)[1] = table[(*it)[1]];
				(*it)[2] = table[(*it)[2]];
			}
		}
	}
	return I;
}

这里我们只定义了一个迭代器,用了一个for循环,这是因为在OpenCV里迭代器会访问每一列然后自动跳到下一行,不用管在内存上是否isContinous。另外要注意的是在三通道图像中我们定义的是 <Vec3b>格式的迭代器,如果定义成uchar,则只能访问到B即蓝色通道的值。
这种方式虽然安全,但是挺慢的,一会儿就知道了。


更慢的方法:动态地址计算

这种方法在需要连续扫描所有点的应用时并不推荐,因为它更实用与随机访问。这种方法最基本的用途是访问任意的某一行某一列:
Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table)
{
	// accept only char type matrices
	CV_Assert(I.depth() != sizeof(uchar));
	const int channels = I.channels();
	switch(channels)
	{
	case 1:
		{
			for( int i = 0; i < I.rows; ++i)
				for( int j = 0; j < I.cols; ++j )
					I.at<uchar>(i,j) = table[I.at<uchar>(i,j)];
			break;
		}
	case 3:
		{
			Mat_<Vec3b> _I = I;

			for( int i = 0; i < I.rows; ++i)
				for( int j = 0; j < I.cols; ++j )
				{
					_I(i,j)[0] = table[_I(i,j)[0]];
					_I(i,j)[1] = table[_I(i,j)[1]];
					_I(i,j)[2] = table[_I(i,j)[2]];
				}
				I = _I;
				break;
		}
	}
	return I;
}


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

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

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

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

实验代码如下:

[cpp]  view plain copy
  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());  

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

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

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

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

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

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

[cpp]  view plain copy
  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());  

测试结果:

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值