OpenCV:无法正确读取16位深度的单通道png图像

时间:2014-07-04 11:27:49

标签: image opencv image-processing png

我正在尝试读取单通道和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;

打印值始终为nos,小于255:

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位的值?

更新

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

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输出相比是错误的。

1 个答案:

答案 0 :(得分:2)

您正在使用Vec3b处理像素,这是将其整理为8位块。

使用Vec3s代替Vec3b。这将为您提供16位像素。这假设您有3通道的16位而不是16位灰度通道(我认为这是从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()));
}