【问题标题】:OpenCV: Unable to correctly read a single channel png image with 16 bit depthOpenCV:无法正确读取 16 位深度的单通道 png 图像
【发布时间】:2014-08-25 17:35:00
【问题描述】:

我正在尝试读取具有单通道和 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的编号:

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

【问题讨论】:

    标签: image opencv image-processing png


    【解决方案1】:

    您正在使用Vec3b 处理像素,这是将其分割成 8 位块。

    使用Vec3s 而不是Vec3b。这将为您提供 16 位像素。这假设您有 3 个 16 位通道而不是 1 个 16 位灰度通道(我认为您将从 PNG 获得),在这种情况下,您可以通过 at&lt;ushort&gt;(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()));
    }
    

    【讨论】:

    • 那么解决办法是什么?
    • 其实原图是单通道16位图,所以Vec3S不行
    • 能否请您将我使用的代码 sn-p 代替 vec3b 向前。我在单通道中的图像。
    • 这给出了 20 个相同的 40092 值,虽然这对 16 位图像有效,但如果我与 MATLAB 输出比较是错误的
    • 确实 40092 是您在 MATLAB 输出中看到的值之一。但是为什么所有的价值观都是这样来的呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 2012-05-27
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 2015-12-13
    • 2018-02-11
    相关资源
    最近更新 更多