c – 读取图像的像素值

我试图使用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

鉴于图像的分辨率为256×256且为MONOCHROME2类型,我预计像素缓冲区长度为256×256 = 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图像像素结构”和类似,但无法找到我要找的东西.是否有一些资源可以帮助我理解这些值的排列以及它们与每个像素的相关性?

最佳答案 我用这段代码读取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);
        }
点赞