opencv之mat元素访问

  • 单通道mat元素访问:使用 img.at<float>(row,col)
 1 #include <stdlib.h>

 2 #include <stdio.h>

 3 #include <opencv/cv.h>

 4 #include <opencv/highgui.h>

 5 #include <opencv2/opencv.hpp>

 6 

 7 using namespace std;

 8 using namespace cv;

 9 int main()

10 {

11     Mat img(3, 4, CV_32FC1, Scalar_<float>(12.625));

12     

13     cout<<img<<endl;

14     cout<<endl;

15     /*cout << "dims:" << img.dims << endl;   

16     cout << "rows:" << img.rows << endl;   

17     cout << "cols:" << img.cols << endl;   

18     cout << "channels:" << img.channels() << endl;

19     

20     cout << "elemSize:" << img.elemSize() << endl;  

21     cout << "elemSize1:" << img.elemSize1() << endl;*/

22 

23     

24 

25     //元素的遍历:

26     for(int row = 0; row < img.rows; row++)

27     {

28         for(int col = 0;col < img.cols; col++)

29         {

30             cout<<img.at<float>(row,col)<<" ";

31         }

32         cout<<endl;

33     }

34     

35     system("pause");

36     return 0;

37 }

备注1:创建图像Mat时候可以用到

 Mat img(3, 4, CV_32FC1, Scalar_<float>(12.625));

3乘4的图片~

Mat_<uchar>对应的是CV_8U,

Mat_<char>对应的是CV_8S,

Mat_<int>对应的是CV_32S,

Mat_<float>对应的是CV_32F,

Mat_<double>对应的是CV_64F,对应的数据深度如下:


Scalar_<**>的**处填上对应的类型!!
CV_8U
- 8-bit unsigned integers ( 0..255 ) CV_8S - 8-bit signed integers ( -128..127 ) CV_16U - 16-bit unsigned integers ( 0..65535 ) CV_16S - 16-bit signed integers ( -32768..32767 ) CV_32S - 32-bit signed integers ( -2147483648..2147483647 ) CV_32F - 32-bit ?oating-point numbers ( -FLT_MAX..FLT_MAX, INF, NAN ) CV_64F - 64-bit ?oating-point numbers ( -DBL_MAX..DBL_MAX, INF, NAN )

备注2

因为单通道,所以上例访问元素时候对应写成
img.at<float>(row,col)
...

 

  • 多通道mat元素访问:
 1 #include <stdlib.h>

 2 #include <stdio.h>

 3 #include <opencv/cv.h>

 4 #include <opencv/highgui.h>

 5 #include <opencv2/opencv.hpp>

 6 

 7 using namespace std;

 8 using namespace cv;

 9 int main()

10 {

11     Mat img(3, 4, CV_64FC2, Scalar_<double>(12.625,3.141592653));

12     //Mat_(Vec)

13     cout<<img<<endl;

14     cout<<endl;

15     /*cout << "dims:" << img.dims << endl;   

16     cout << "rows:" << img.rows << endl;   

17     cout << "cols:" << img.cols << endl;   

18     cout << "channels:" << img.channels() << endl;

19     

20     cout << "elemSize:" << img.elemSize() << endl;  

21     cout << "elemSize1:" << img.elemSize1() << endl;*/

22 

23 

24     //元素的遍历:

25     for(int row = 0; row < img.rows; row++)

26     {

27         for(int col = 0;col < img.cols; col++)

28         {

29             cout<<img.at<Vec2d>(row,col)[0]<<" "<<img.at<Vec2d>(row,col)[1]<<"\t";

30         }

31         cout<<endl;

32     }

33 

34 

35     system("pause");

36     return 0;

37 }

 

 

备注3:创建图像Mat时候,注意点同上~

 

备注4:使用at时img.at<**>(row,col)的**处对应选项~

typedef Vec<uchar, 2> Vec2b;

typedef Vec<uchar, 3> Vec3b;

typedef Vec<uchar, 4> Vec4b;



typedef Vec<short, 2> Vec2s;

typedef Vec<short, 3> Vec3s;

typedef Vec<short, 4> Vec4s;



typedef Vec<ushort, 2> Vec2w;

typedef Vec<ushort, 3> Vec3w;

typedef Vec<ushort, 4> Vec4w;



typedef Vec<int, 2> Vec2i;

typedef Vec<int, 3> Vec3i;

typedef Vec<int, 4> Vec4i;

typedef Vec<int, 6> Vec6i;

typedef Vec<int, 8> Vec8i;



typedef Vec<float, 2> Vec2f;

typedef Vec<float, 3> Vec3f;

typedef Vec<float, 4> Vec4f;

typedef Vec<float, 6> Vec6f;



typedef Vec<double, 2> Vec2d;

typedef Vec<double, 3> Vec3d;

typedef Vec<double, 4> Vec4d;

typedef Vec<double, 6> Vec6d;

 题外话:

我一次Mat img(3,4,CV8S,~)....而访问时候,用的img.at<int>(i,j)出现访存错误,我的本意是用CV32S,不对应就会出现访存错误。。。

你可能感兴趣的:(opencv)