分享

OpenCv cv::Mat类用法

 学海无涯GL 2013-04-15

1、使用准备:

using  namespace  cv;

2、Mat的声明

Mat m=Mat(rows, cols, type);

Mat m=Mat(Size(width,height), type);

Mat A=Mat(3,4,CV_32FC1);
Mat B=Mat(4,3,CV_32FC1);

3、Mat赋值

vector<Point3f>v;//suppose it is already full

Mat m1=Mat(v,true);//boolean value true is necessary in order to copy data from v to m1

CvMat *p1=====??

Mat m2=Mat(p1);

4、Mat之间运算

MatC=2*A*B;

Mat C=C.inv();//Now C is its own inverse matrix

Mat D=A.t();//D is the transposed matrix of A

Mat a=Mat(4,1, CV_32FC3);//a is 4x1, 3 channels

Mat b=a.reshape(1);//b is 4x3, 1 channel

5、单通道Mat元素读写

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

6、多通道Mat元素读写

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;

 

7.选取Mat上指定区域方法

  Mat src; Rect rect;

  Mat dst = src(rect); 或者Mat dst(src,rect);

 

注意:cv::Mat A;

A.row(i) = A.row(j); // 错误

A.row(i) = A.row(j) + 0; // 正确


OpenCV cv::Mat类操作

首先还是要感谢箫鸣朋友在我《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. for (int i=0; i<1000; i++)
  4. {
  5. for (int j=0; j<1000; j++)
  6. {
  7. img1.at<float>(i,j) = 3.2f;
  8. }
  9. }
  10. t = (double)getTickCount() - t;
  11. printf("in %gms\n", t*1000/getTickFrequency());
  12. /
  13. t = (double)getTickCount();
  14. Mat img5(1000, 1000, CV_32F);
  15. float *pData1;
  16. for (int i=0; i<1000; i++)
  17. {
  18. pData1=img5.ptr<float>(i);
  19. for (int j=0; j<1000; j++)
  20. {
  21. pData1[j] = 3.2f;
  22. }
  23. }
  24. t = (double)getTickCount() - t;
  25. printf("in %gms\n", t*1000/getTickFrequency());
  26. t = (double)getTickCount();
  27. Mat img6(1000, 1000, CV_32F);
  28. float *pData2;
  29. Size size=img6.size();
  30. if(img2.isContinuous())
  31. {
  32. size.width = size.width*size.height;
  33. size.height = 1;
  34. }
  35. size.width*=img2.channels();
  36. for(int i=0; i<size.height; i++)
  37. {
  38. pData2 = img6.ptr<float>(i);
  39. for(int j=0; j<size.width; j++)
  40. {
  41. pData2[j] = saturate_cast<float>(3.2f);
  42. }
  43. }
  44. t = (double)getTickCount() - t;
  45. printf("in %gms\n", t*1000/getTickFrequency());
 



 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多