OpenCV输出奇数像素值 [英] OpenCV outputing odd pixel values
问题描述
我想使用C ++对图像进行处理,并且一直在使用OpenCV.但是,在尝试分别访问每个像素时,我有些卡住了.我可以使用以下命令很好地输出整个灰度图像:
I want to do processing on images with C++ and have been using OpenCV. I've gotten a bit stuck though when it comes to trying to access each pixel individually. I can output the whole gray scaled image just fine using:
cout<<图片;
并获得预期值的输出,如下所示:
and get the expected output of values like so:
[143, 147, 164, 177, 177, 185, 196, 195, 185, 186, 178, 190, 178, 163, 183...
但是当我尝试使用以下命令一次输出一个像素时:
But when I try to output one pixel at a time using:
for (int y = 0; y < image.rows; ++y) {
for (int x = 0;x < image.cols; ++x) {
std::cout<<image.at<double>(y, x)<<" ";
}
cout << endl;}
我的输出是一堆像这样的大数字:
my output is a bunch of large numbers like these:
-2.98684e+18 -1.21685e-83 -1.91543e-113 -1.8525e-59 -2.73052e-127 -2.08731e-35 -3.72066e-103 ...
对我遗失或做错的事情有什么想法吗?
Any thoughts as to what I am missing or doing wrong?
推荐答案
如果要打印图像的所有像素,只需使用:
If you want to print all pixels of the image, you can simply use:
cout << image << endl;
如果要打印像素,请使用:
If you want to print a pixel, use:
cout << image.at<Vec3b>(y, x) << endl; // for color image
或
cout << (int) image.at<uchar>(y, x) << endl; // for gray-scale image
请注意,在最后一行中,需要将其强制转换为 int
.有关详细信息,请查看为什么"cout"对于"unsigned char"来说很奇怪?.
Note that, in the last line, casting it to int
is needed. For details, check out Why "cout" works weird for "unsigned char"?.
这篇关于OpenCV输出奇数像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!