简体   繁体   中英

Get the pixel values from cv::Mat as numbers?

Hello,

I'm quite new to OpenCV and C++ so I really don't know what is going on here. I searched for a way to access the pixel values of a Mat image (of type CV_8UC3) and found this:

Vec3b bgrPixel = foo.at<Vec3b>(Point(x, y));
//bgrPixel.val[0] is B, bgrPixel.val[1] is G, bgrPixel.val[2] is R

But this outputs chars instead of ints, when I do cout << bgrPixel.val[0] << endl; for instance, it gives some really strange characters:

r
Ç
è
Ç
~
ö
É
è
ÿ
×
×
;
2
#
2
/
+
'
"
)
%
-
*

How can I get the pixel values as ints? Or is there a simpler way to access them, eg by converting the Mat to a regular array (like you would in Python)?

Thanks for the answers in advance!

Use: cout << (int)(bgrPixel.val[0]) << endl;

bgrPixel.val[0] is of type unsigned char , and it is printed as character.

Casting from unsigned char to int solves the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM