gpt4 book ai didi

c++ - 读取图像的像素值

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:15:16 32 4
gpt4 key购买 nike

我正在尝试使用 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 读数中获得的像素缓冲区时,像素缓冲区是 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

这种值排列意味着什么?这是单色图像的某种典型像素表示吗?我一直在“谷歌搜索图像像素结构”和类似的但找不到我要找的东西。是否有一些资源可以帮助我理解这种值的排列方式以及它们与每个像素的关联方式?

最佳答案

我使用这段代码读取 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);
}

关于c++ - 读取图像的像素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39652261/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com