gpt4 book ai didi

c++ - 如何在 C++ 中读取 dicom 像素

转载 作者:行者123 更新时间:2023-11-28 05:36:44 25 4
gpt4 key购买 nike

我想解析 DICOM 文件以进行一些像素处理。我尝试了 DCMTK 库,但它对我不起作用。我想要一些非常简单的东西,比如 C++ 中的轻量级跨平台库,因为我只想读取 DICOM 文件。

如有任何建议,我们将不胜感激。

最佳答案

您可以使用 ImageMagick 读取 DICOM 文件,它是免费的和跨平台的,通常安装在 Linux 发行版上,可用于 OSX 和 Windows。

版本 6.x 的示例如下...

////////////////////////////////////////////////////////////////////////////////
// sample.cpp
// Mark Setchell
//
// ImageMagick Magick++ sample code
//
// Compile with:
// g++ sample.cpp -o sample $(Magick++-config --cppflags --cxxflags --ldflags --libs)
////////////////////////////////////////////////////////////////////////////////
#include <Magick++.h>
#include <iostream>

using namespace std;
using namespace Magick;

int main(int argc,char **argv)
{
// Initialise ImageMagick library
InitializeMagick(*argv);

// Create Image object and read in DICOM image
Image image("sample.dcm");

// Get dimensions
int w = image.columns();
int h = image.rows();
cout << "Dimensions: " << w << "x" << h << endl;

PixelPacket *pixels = image.getPixels(0, 0, w, h);

for(int y=0; y<h; y++){
for(int x=0; x<w; x++){
Color color = pixels[w * y + x];
cout << x << "," << y << ":" << color.redQuantum() << "/" << color.greenQuantum() << "/" << color.blueQuantum() << endl;
}
}
}

示例输出

Dimensions: 512x512
0,0:0/0/0
1,0:0/0/0
2,0:0/0/0
3,0:0/0/0
4,0:0/0/0
5,0:0/0/0
6,0:0/0/0
7,0:0/0/0
8,0:0/0/0
9,0:0/0/0
10,0:0/0/0
11,0:0/0/0
12,0:0/0/0
13,0:0/0/0
14,0:0/0/0
15,0:0/0/0
16,0:0/0/0
17,0:0/0/0
18,0:0/0/0
19,0:0/0/0
20,0:0/0/0
21,0:0/0/0
22,0:0/0/0
23,0:0/0/0
24,0:0/0/0
25,0:0/0/0
...
...
260,18:80/80/80
261,18:144/144/144
262,18:192/192/192
263,18:80/80/80
264,18:32/32/32
265,18:144/144/144
...
...

如果你想使用版本 7.x,请参阅 Eric 的技术 here .

或者,在终端的命令行中,您可以将文件转换为原始 8 位 RGB 二进制数据,如下所示:

# Convert 512x512 image to 8-bit RGB binary file
convert sample.dcm -depth 8 rgb:image.bin

ls -l image.bin
-rw-r--r-- 1 mark staff 786432 30 Jun 15:29 image.bin

您有望从文件大小中看出图像现在​​为 786,432 字节,即每个 512x512 像素 3 个字节,因此您可以直接将数据读入 C++ 程序,知道您将获得:

RGB RGB RGB RGB ... RGB

或者,在终端的命令行中,您可以将图像数据转储为十六进制:

convert sample.dcm -depth 8 txt: | more

示例输出

# ImageMagick pixel enumeration: 512,512,65535,gray
0,0: (0,0,0) #000000 gray(0)
1,0: (0,0,0) #000000 gray(0)
2,0: (0,0,0) #000000 gray(0)

关于c++ - 如何在 C++ 中读取 dicom 像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38119376/

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