image - OpenCV:无法正确读取 16 位深度的单 channel png 图像

标签 image opencv image-processing png

我正在尝试读取具有单 channel 和 16 位深度的 png 图像。随后我在一条线上找到像素值:

//read the image data in the file "MyPic.JPG" and store it in 'img'
Mat img = imread("sir.png",  CV_LOAD_IMAGE_ANYDEPTH); 

//Iterate through the line along which Intensity profile is required 
LineIterator it(img, Point(1,1), Point(20,20), 8);

vector<Vec3b> buf;   

for(int i=0; i<it.count; i++)
{
    buf.push_back( Vec3b(*it) );
    it++;
}
//print pixel value on the selected line
cerr << Mat(buf) << endl;

打印的值总是小于 255 的 nos:

156, 156, 164; 153, 153, 152; 154, 154, 139; 179, 179, 180; 182, 182, 176; 2
208, 167; 144, 144, 163; 204, 204, 206; 180, 180, 187; 174, 174, 170; 150, 1
162; 154, 154, 170; 157, 157, 181; 181, 181, 159; 164, 164, 152; 130, 130, 1
166, 166, 181; 153, 153, 170; 153, 153, 176; 180, 180, 198]

这意味着它实际上是以 8 位而不是 16 位读取此图像。

我在 MATLAB 中验证了我的图像,我可以看到对 16 位有效的值,例如:

d=imread('sir.png');
buf = improfile( d, [1 20], [1 20] )

输出如下:

65535 40092 39321 39578 46003 46774 53456 37008 52428 46260 44718 38550 39578 40349 46517 42148 33410 42662 39321 39321

为什么我在 opencv 中的结果与实际不同?如何正确读取图像以使其以 16 位显示值?

更新

根据 Roger 的回答,我更新了我的代码:

LineIterator it(img, Point(1,1), Point(20,20), 8);
vector<ushort> buf;

for(int i = 0; i < it.count; i++)
{
    buf.push_back(img.at<ushort>(it.pos()));
}

cerr << Mat(buf) << endl;

但是现在我所有的像素值都变成了 40092,这与 MATLAB 输出相比是错误的。

最佳答案

您正在使用 Vec3b为了处理像素,这是将其切碎成 8 位 block 。

使用 Vec3s而不是 Vec3b .这将为您提供 16 位像素。这假设您有 3 个 16 位 channel 而不是一个 16 位灰度 channel (我认为您将从 PNG 中获得),在这种情况下您将通过 at<ushort>(y,x) 访问。例如。

LineIterator it(img, Point(1,1), Point(20,20), 8);

vector<ushort> buf;

for(int i = 0; i < it.count; i++, it++)
{
    buf.push_back(img.at<ushort>(it.pos()));
}

关于image - OpenCV:无法正确读取 16 位深度的单 channel png 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24573273/

相关文章:

image - 在网站中使用 PNG 图像

sql-server - 将 Picturebox 中的图像与 SQL Image 数据类型进行比较

image - 带图像的 NSButton - 如何获取图像 subview ?

linux - 在ubuntu 14.04上安装opencv时出错

python - 为什么 cv2.projectPoints 的行为不如我预期?

c++ - OpenCV - 加速 3x3 补丁的 SSD 计算

image - 谷歌流量和 CDN

python - Opencv:TypeError:轮廓不是numpy数组,也不是标量

swift - 为什么具有透明背景的 NSImage 得到的 RGB 值为 (0,0,0)?

c++ - 如何使用 OpenCV 在目标的位置、比例和旋转发生变化时检测目标的中心