From: http://blog.csdn.net/a390641326/article/details/8686501
// Because I have already come out of version 2.4.3 When I come into contact with opencv (this note has been updated to version 2.4.4), so I will directly learn the method of version 2. Since C ++ is used for updating and adding classes, it is necessary to make it easier to use // and the C ++ class structure, so you do not have to worry too much about the allocated memory, the corresponding memory space will be released during class analysis. // you will not pay attention to the method of C interface before 1.0. The Chinese version of 1.0 contains a lot of Chinese information. Please search for it by yourself
1. At Method
Use the at template function to access the pixels of a column in a row
For example:
Img1.at <CV: vec3b> (J, I)
Access to various channels:
Img1.at <CV: vec3b> (J, I) [0] Channel B
Img1.at <CV: vec3b> (J, I) [1] G Channel
Img1.at <CV: vec3b> (J, I) [2] r Channel
2. PTR Method
You can use the PTR template to obtain the header pointer of the row data.
Uchar * Data = img1.ptr <uchar> (j); // get the row J pointer (address for storing row data)
Access channels of the I pixel:
In essence, it is implemented based on the pointer and the Image structure.
Data [I * img1.nchanles + 0]
Data [I * img1.nchanles + 1]
Data [I * img1.nchanles + 2]
3. iterator method (iterator)
The iterator method is similar to the one-dimensional array mode. It uses the iterator to conveniently operate pixels.
CV: mat _ <CV: vec3b >:: iterator it = img1.begin <CV: vec3b> (); CV: mat _ <CV: vec3b> :: iterator it_end = img1.end <CV: vec3b> ();
Access pixel:
(* It) [0] Channel B
(* It) [1] G Channel
(* It) [2] r Channel
It ++
The following code demonstrates how to read all the data from the image in the preceding three steps,
In order to display less data, we made a 9x8 image.
Images and code are packaged and sent to resources. If you are interested, download them (1 point) and click the open link.
Mat img1 = imread ("D: \ demo1.bmp"); // 9*8 namedwindow ("Demo"); imshow ("Demo", img1); int I, J; // Method 1: At method cout <"Method 1: At method" <Endl; cout <"---------------------------------------------" <Endl; For (j = 0; j (J, I) [0]; // channel B // cout <"; cout <(INT) img1.at <CV: vec3b> (J, I) [1]; // G channel // cout <"; cout <(INT) img1.at <CV: vec3 B> (J, I) [2]; // R channel // cout <""; cout <"\ t" ;}cout <Endl ;} //////////////////////////////////////// /// // cout <"-------------------------------------------------------------------" <Endl; // Method 2: PTR method cout <"Method 2: PTR method" <Endl; cout <"---------------------------------------------" <Endl; For (j = 0; j (j); // obtain the row pointer (address for storing row data) for (I = 0; I :: iterator it = img1.begin <CV: vec3b> (); CV: mat _ <CV: vec3b> :: iterator it_end = img1.end <CV: vec3b> (); for (I = 0; it! = It_end; it ++) // similar to a one-dimensional array {cout <(INT) (* It) [0]; // bcout <(INT) (* It) [1]; // gcout <(INT) (* It) [2]; // rcout <"\ t"; I ++; if (I> 0 & I % img1.cols = 0) {cout <Endl; //} cout <"---------------------------------------------" <Endl; waitkey (0 ); return 0;