读取图像的像素值

时间:2016-09-23 03:47:35

标签: c++ image pixels gdcm

我正在尝试使用Grassroots DICOM(GDCM)库在我的简单c ++应用程序中读取DICOM文件中包含的图像的像素值。在阅读文件元数据时,我得到有关图片的以下信息:

   
Bits allocated: 16
Bits Stored: 16
High Bit: 15
Unsigned or signed: 1
Samples pr pixel: 1
Dimensions: 2
Dimension values: 256x256
Pixel Representation: 1
SamplesPerPixel: 1
ScalarType: INT16
PhotometricInterpretation: MONOCHROME2
Pixel buffer length: 131072

鉴于图像的分辨率为256x256且为MONOCHROME2类型,我预计像素缓冲区长度为256x256 = 65536个元素,但实际上长度为131072个元素。

如果我使用MATLAB来导入像素数据,我会得到0到850范围内的65536个值,其中0为黑色,850为白色。

当我查看我在c ++应用程序中从GDCM读数获得的像素缓冲区时,pixelbuffer是131072个元素,其中每个偶数索引元素在-128到+127范围内,并且每个奇数索引元素在0-范围内3。像这样:

Exerpt:    

PixelBuffer[120] = -35
PixelBuffer[121] = 0
PixelBuffer[122] = 51
PixelBuffer[123] = 2
PixelBuffer[124] = 71
PixelBuffer[125] = 2
PixelBuffer[126] = 9
PixelBuffer[127] = 2
PixelBuffer[128] = -80
PixelBuffer[129] = 2
PixelBuffer[130] = 87
PixelBuffer[131] = 3
PixelBuffer[132] = 121
PixelBuffer[133] = 3
PixelBuffer[134] = -27
PixelBuffer[135] = 2
PixelBuffer[136] = 27
PixelBuffer[137] = 2
PixelBuffer[138] = -111
PixelBuffer[139] = 1
PixelBuffer[140] = 75
PixelBuffer[141] = 1
PixelBuffer[142] = 103 

这种价值观的含义是什么意思?这是单色图像的某种典型像素表示吗?我一直在" googeling图像像素结构"和类似但无法找到我要找的东西。是否有一些资源可以帮助我理解这些值的排列以及它们与每个像素的相关性?

1 个答案:

答案 0 :(得分:1)

我使用此代码读取16位MONOCHROME2 Dicom文件:

byte[] signedData = new byte[2];
        List<int> tempInt = new List<int>();
        List<ushort> returnValue = new List<ushort>();

        for (i = 0; i < PixelBuffer.Length; ++i)
        {
            i1 = i * 2;
            signedData[0] = PixelBuffer[i1];
            signedData[1] = PixelBuffer[i1 + 1];
            short sVal = System.BitConverter.ToInt16(signedData, 0);

            int pixVal = (int)(sVal * rescaleSlope + rescaleIntercept);

            tempInt.Add(pixVal);
        }

        int minPixVal = tempInt.Min();
        SignedImage = false;
        if (minPixVal < 0) SignedImage = true;

        foreach (int pixel in tempInt)
        {
            ushort val;
            if (SignedImage)
                val = (ushort)(pixel - short.MinValue);
            else
            {
                if (pixel > ushort.MaxValue) val = ushort.MaxValue;
                else val = (ushort)(pixel);
            }

            returnValue.Add(val);
        }
相关问题