opencv2.0以后c++接口的API函数成为一种趋势,常用的IplImage与cvMat数据结构也淡出,新数据结构cv::Mat成为新宠。目前国内,新数据结构cv::Mat的资料还是比较少的。新数据结构cv::Mat元素的访问(读取、存取)的方法对很多初学者来还是很不明了的。
现在综合一些资料,在此抛砖引玉。
//http://blog.csdn.net/moc062066
//20111128
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc,char** argv)
{
const char* filename = "google.png";
//CV_8UC3
//相应的CV_8SC3---Vec3s
//相应的CV_16UC3---Vec3w
Mat mat_CV_8UC3 = imread(filename,IMREAD_COLOR);
for( size_t nrow = 0; nrow < mat_CV_8UC3.rows; nrow++)
{
for(size_t ncol = 0; ncol < mat_CV_8UC3.cols; ncol++)
{
Vec3i bgr = mat_CV_8UC3.at<Vec3b>(nrow,ncol);//用Vec3b也行
cout << "("<<bgr.val[0]<<","
<<bgr.val[1]<<","
<<bgr.val[2]<<")";
}
cout << endl;
}
for( size_t nrow = 0; nrow < mat_CV_8UC3.rows; nrow++)
{
uchar* data = mat_CV_8UC3.ptr<uchar>(nrow);
for(size_t ncol = 0; ncol < mat_CV_8UC3.cols * mat_CV_8UC3.channels(); ncol++)
{
cout << int( data[ncol] ) ;
}
cout << endl;
}
//------CV_8UC1----------start---
//
Mat mat_CV_8UC1 = imread(filename,IMREAD_GRAYSCALE);
for( size_t nrow = 0; nrow < mat_CV_8UC1.rows; nrow++)
{
for(size_t ncol = 0; ncol < mat_CV_8UC1.cols; ncol++)
{
uchar val = mat_CV_8UC1.at<uchar>(nrow,ncol);
//
cout << (int(val) > 200 ? 1 :0) ;//cout<<int(val)<< endl ;
}
cout << endl ;
}
cout << endl;
for ( size_t row = 0 ; row < mat_CV_8UC1.rows ; ++row)
{
uchar* ptr = mat_CV_8UC1.ptr<uchar>(row);
for ( size_t col = 0 ; col < mat_CV_8UC1.cols ; ++col)
{
cout << ( int(ptr[col]) > 200 ? 1 :0) ;//cout<<int(val)<< endl ;
}
cout << "\n" ;
}
cout << endl;
MatIterator_<uchar> it = mat_CV_8UC1.begin<uchar>(), it_end = mat_CV_8UC1.end<uchar>();
for(int cnt = 1; it != it_end; ++it)
{
cout << ( int(*it) > 200 ? 1 : 0) ;
if( (cnt++ % mat_CV_8UC1.cols) ==0 )
cout << endl;
}
//------CV_8UC1----------end---
return 0;
}
参考:1
所有图像:
结果:
(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)
(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)
(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)
(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)
(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(223,226,244)(189,195,244)(169,173,241)(169,174,240)(189,193,240)(216,217,239)(242,242,242)(244,244,244)(245,245,245)(245,245,245)(245,245,245)(245,245,245)
(245,245,245)(245,245,245)(245,245,245)(245,245,245)(242,243,244)(190,197,245)(115,122,246)(79,81,239)(86,85,231)(85,84,228)(64,57,232)(65,56,239)(80,75,242)(117,116,236)(177,179,233)(238,238,238)(243,243,243)(245,245,245)(245,245,245)
(245,245,245)(245,245,245)(245,245,245)(225,228,244)(134,145,248)(65,67,244)(128,130,223)(191,192,212)(212,212,212)(213,213,213)(201,201,211)(142,142,210)(59,45,225)(67,53,238)(76,64,238)(112,106,228)(217,218,231)(241,241,241)(245,245,245)
(245,245,245)(245,245,245)(223,227,244)(118,129,248)(65,67,243)(154,158,213)(207,207,207)(215,215,215)(226,226,226)(231,231,231)(232,232,232)(227,227,227)(174,175,212)(53,34,216)(62,45,234)(66,48,234)(92,82,224)(217,218,227)(241,241,241)
(245,245,245)(236,238,245)(128,139,248)(77,81,248)(106,107,213)(201,201,201)(216,216,216)(233,233,233)(242,242,242)(244,244,244)(245,245,245)(243,243,243)(236,236,236)(143,142,209)(46,22,217)(58,39,232)(55,30,229)(102,94,217)(229,229,229)
(245,245,245)(168,178,244)(91,99,250)(56,51,236)(154,155,196)(209,209,209)(232,232,232)(243,243,243)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(244,244,244)(232,232,235)(70,54,205)(49,25,223)(52,29,228)(47,19,220)(166,168,211)
(229,231,243)(98,107,246)(75,80,248)(54,44,222)(176,177,195)(220,220,220)(240,240,240)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(243,243,243)(139,136,216)(46,19,215)(49,25,226)(47,17,217)(94,84,200)
(184,190,244)(75,78,247)(63,62,245)(56,44,213)(186,187,197)(226,226,226)(243,243,243)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(202,203,231)(47,22,212)(48,21,223)(46,17,215)(55,31,195)
(143,151,242)(63,62,245)(57,53,241)(55,41,213)(185,186,199)(228,228,228)(243,243,243)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(233,233,239)(66,48,214)(48,19,221)(46,17,212)(42,13,190)
(106,114,244)(56,51,243)(55,47,239)(48,31,217)(169,170,199)(227,227,227)(243,243,243)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(244,244,244)(98,86,216)(47,17,220)(44,15,209)(39,9,183)
(113,120,239)(51,40,238)(53,43,239)(47,29,221)(146,147,200)(224,224,224)(242,242,242)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(116,106,221)(47,16,219)(43,13,203)(38,10,173)
(146,150,229)(49,32,229)(51,39,236)(48,30,225)(113,111,203)(219,219,219)(239,239,239)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(121,113,224)(46,15,218)(42,11,194)(47,26,165)
(191,194,232)(46,26,216)(49,33,228)(49,31,229)(69,58,214)(209,210,212)(236,236,236)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(116,105,225)(46,13,214)(38,7,178)(83,72,165)
(238,238,240)(85,73,208)(46,23,214)(49,30,227)(48,27,224)(161,163,207)(230,230,230)(244,244,244)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(93,79,224)(43,10,201)(34,5,159)(146,144,179)
(244,244,244)(182,183,221)(42,16,195)(45,22,211)(48,25,225)(82,71,217)(219,219,219)(239,239,239)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(216,217,239)(55,26,217)(38,6,174)(81,70,161)(197,197,197)
(245,245,245)(240,240,240)(122,118,201)(40,12,185)(45,19,206)(48,22,223)(126,124,216)(230,230,230)(243,243,243)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(122,112,227)(41,7,191)(61,43,160)(181,181,189)(211,211,211)
(245,245,245)(244,244,244)(235,235,235)(116,107,190)(38,11,174)(43,14,196)(47,18,217)(128,122,219)(224,225,232)(242,242,242)(245,245,245)(245,245,245)(232,232,243)(132,120,229)(43,9,197)(75,60,171)(179,179,192)(207,207,207)(229,229,229)
(245,245,245)(245,245,245)(243,243,243)(232,232,232)(158,156,199)(67,51,170)(38,10,178)(43,12,201)(63,41,216)(121,112,223)(141,134,228)(117,105,225)(65,39,212)(56,31,193)(131,125,191)(201,201,203)(210,210,210)(228,228,228)(240,240,240)
(245,245,245)(245,245,245)(245,245,245)(243,243,243)(234,234,234)(219,219,220)(162,160,195)(120,113,182)(85,74,183)(71,53,189)(82,67,196)(115,105,200)(153,149,202)(201,201,209)(210,210,210)(218,218,218)(232,232,232)(241,241,241)(244,244,244)
(245,245,245)(245,245,245)(245,245,245)(245,245,245)(244,244,244)(239,239,239)(232,232,232)(223,223,223)(216,216,216)(211,211,211)(211,211,211)(214,214,214)(217,217,217)(222,222,222)(230,230,230)(237,237,237)(243,243,243)(245,245,245)(245,245,245)
(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(245,245,245)(244,244,244)(241,241,241)(237,237,237)(234,234,234)(233,233,233)(234,234,234)(236,236,236)(240,240,240)(243,243,243)(245,245,245)(245,245,245)(245,245,245)(245,245,245)
245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245
245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245
245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245
245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245245
245245245245245245245245245245245245245245245245245245245245245223226244189195244169173241169174240189193240216217239242242242244244244245245245245245245245245245245245245
245245245245245245245245245245245245242243244190197245115122246798123986852318584228645723265562398075242117116236177179233238238238243243243245245245245245245
2452452452452452452452452452252282441341452486567244128130223191192212212212212213213213201201211142142210594522567532387664238112106228217218231241241241245245245
24524524524524524522322724411812924865672431541582132072072072152152152262262262312312312322322322272272271741752125334216624523466482349282224217218227241241241
245245245236238245128139248778124810610721320120120121621621623323323324224224224424424424524524524324324323623623614314220946222175839232553022910294217229229229
245245245168178244919925056512361541551962092092092322322322432432432452452452452452452452452452452452452442442442322322357054205492522352292284719220166168211
22923124398107246758024854442221761771952202202202402402402452452452452452452452452452452452452452452452452452452432432431391362164619215492522647172179484200
1841902447578247636224556442131861871972262262262432432432452452452452452452452452452452452452452452452452452452452452452022032314722212482122346172155531195
1431512426362245575324155412131851861992282282282432432432452452452452452452452452452452452452452452452452452452452452452332332396648214481922146172124213190
106114244565124355472394831217169170199227227227243243243245245245245245245245245245245245245245245245245245245245245245244244244988621647172204415209399183
113120239514023853432394729221146147200224224224242242242245245245245245245245245245245245245245245245245245245245245245245245245116106221471621943132033810173
146150229493222951392364830225113111203219219219239239239245245245245245245245245245245245245245245245245245245245245245245245245121113224461521842111944726165
191194232462621649332284931229695821420921021223623623624524524524524524524524524524524524524524524524524524524524524524524524511610522546132143871788372165
238238240857320846232144930227482722416116320723023023024424424424524524524524524524524524524524524524524524524524524524524524593792244310201345159146144179
244244244182183221421619545222114825225827121721921921923923923924524524524524524524524524524524524524524524524524524521621723955262173861748170161197197197
2452452452402402401221182014012185451920648222231261242162302302302432432432452452452452452452452452452452452452452452451221122274171916143160181181189211211211
2452452452442442442352352351161071903811174431419647182171281222192242252322422422422452452452452452452322322431321202294391977560171179179192207207207229229229
245245245245245245243243243232232232158156199675117038101784312201634121612111222314113422811710522565392125631193131125191201201203210210210228228228240240240
245245245245245245245245245243243243234234234219219220162160195120113182857418371531898267196115105200153149202201201209210210210218218218232232232241241241244244244
245245245245245245245245245245245245244244244239239239232232232223223223216216216211211211211211211214214214217217217222222222230230230237237237243243243245245245245245245
245245245245245245245245245245245245245245245245245245244244244241241241237237237234234234233233233234234234236236236240240240243243243245245245245245245245245245245245245
1111111111111111111
1111111111111111111
1111111111111111111
1111111111111111111
1111111110011111111
1111110000000001111
1111000011100000111
1110001111110000011
1100011111111000001
1000011111111100000
1000011111111100000
1000011111111110000
0000011111111110000
0000011111111110000
0000011111111110000
0000011111111110000
1000011111111110000
1000001111111110000
1000001111111110000
1100000111111100001
1110000011111000011
1111000000000001111
1111110000000111111
1111111111111111111
1111111111111111111
1111111111111111111
1111111111111111111
1111111111111111111
1111111111111111111
1111111110011111111
1111110000000001111
1111000011100000111
1110001111110000011
1100011111111000001
1000011111111100000
1000011111111100000
1000011111111110000
0000011111111110000
0000011111111110000
0000011111111110000
0000011111111110000
1000011111111110000
1000001111111110000
1000001111111110000
1100000111111100001
1110000011111000011
1111000000000001111
1111110000000111111
1111111111111111111
1111111111111111111
1111111111111111111
1111111111111111111
1111111111111111111
1111111111111111111
1111111110011111111
1111110000000001111
1111000011100000111
1110001111110000011
1100011111111000001
1000011111111100000
1000011111111100000
1000011111111110000
0000011111111110000
0000011111111110000
0000011111111110000
0000011111111110000
1000011111111110000
1000001111111110000
1000001111111110000
1100000111111100001
1110000011111000011
1111000000000001111
1111110000000111111
1111111111111111111
1111111111111111111