Opencv2.x New Data Structure CV: mat access Element Method

Source: Internet
Author: User

After opencv2.0, the API functions of the C ++ interface have become a trend. The commonly used data structures of iplimage and cvmat have also faded out, and the new data structure CV: mat has become a new favorite. Currently, in China, the new data structure CV: mat has few materials. New data structure CV: the access (read and access) method of Mat elements is still unclear for many beginners.

Now, we will give you some comprehensive information.

// 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 // corresponding cv_8sc3 --- vec3s // corresponding cv_16uc3 --- vec3wmat mat_cv_8uc3 = imread (filename, imread_color); For (size_t nrow = 0; nrow <strong; nrow ++) {for (size_t ncol = 0; ncol <mat_cv_8uc3.cols; NC Ol ++) {vec3i BGR = mat_cv_8uc3.at <vec3b> (nrow, ncol); // use vec3b to perform 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; 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 ;}

Reference: 1

All images:

Result:

(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) (239) (231, 228, 232) (239, 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) (244, 128,130,223) (191,192,212) (212,212,212) (213,213,213) (201,201,211) (142,142,210) (225, 238) (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) (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) (216, 234) (234, 224) (217,218,227, 241,241,241)
(245,245,245) (236,238,245) (128,139,248) (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) (217, 232) (229, 217) (229,229,229)
(245,245,245) (168,178,244) (250, 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) (205, 223) (228, 220) (166,168,211)
(229,231,243) (98,107,246) (248, 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) (215, 226) (217, 200)
(184,190,244) (247, 245) (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) (212, 223) (215, 195)
(143,151,242) (245, 241) (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) (214, 221) (212, 190)
(106,114,244) (243, 239) (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) (216, 220) (209, 183)
(113,120,239) (238, 239) (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) (219, 203) (173)
(146,150,229) (229, 236) (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) (218, 194) (165)
(191,194,232) (216, 228) (229, 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) (214) (178, 165)
(238,238,240) (208, 214) (227, 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) (224, 201) (159, 146,144,179)
(244,244,244) (182,183,221) (195, 211) (225, 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) (217, 174) (161, 197,197,197)
(245,245,245) (240,240,240) (122,118,201) (185, 206) (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) (191, 160) (181,181,189) (211,211,211)
(245,245,245) (244,244,244) (235,235,235) (116,107,190) (174, 196) (217, 128,122,219) (224,225,232) (242,242,242) (245,245,245) (245,245,245) (232,232,243) (132,120,229) (197, 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) (178, 201) (216, 121,112,223) (141,134,228) (117,105,225) (212, 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) (183) (189, 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.