如何从dicom文件中读取二进制数据
How to read binary data from dicom file?
如何从未压缩的DICOM文件中读取原始图像数据并将其转储到文件中。我只是将以下代码用于压缩文件。使用dcmtk库
dataSet->findAndGetElement(DCM_PixelData, element);
pixDataElem = OFstatic_cast(DcmPixelData*, element);
DcmPixelSequence *pixelSequence = NULL;
E_TransferSyntax tran_Syntax = EXS_Unknown;
const DcmRepresentationParameter *representation = NULL;
// Find the key that is needed to access the right representation of the data within DCMTK
pixDataElem->getOriginalRepresentationKey(tran_Syntax, representation);
//pixDataElem->getCurrentRepresentationKey(tran_Syntax, representation);
// Access original data representation and get result within pixel sequence
pixDataElem->getEncapsulatedRepresentation(tran_Syntax, representation, pixelSequence);
DcmPixelItem *pixelItem = NULL;
//Access the First frame by skipping the offset table...
pixelSequence->getItem(pixelItem, 1);
Uint8 *pixels = NULL;
pixDataElem = (DcmPixelData*)pixelItem;
pixDataElem->getUint8Array(pixels);
Uint8 *pixels = NULL;
pixDataElem->getUint8Array(pixels);
//Writing the Raw data to a file...
FILE *file;
file = fopen("D:\DicomImage.jpeg", "wb");
fwrite(pixels, sizeof(char), imageSize, file);
cout << "File write Completed and the File is closed Successfully" << endl;
如何使用dcmtk库从c++中有许多帧的未压缩文件中提取原始图像数据。。。。。?
基本上,您可以使用相同的代码,但不需要压缩(这实际上是更容易的情况…(
dataSet->findAndGetElement(DCM_PixelData, element);
pixDataElem = OFstatic_cast(DcmPixelData*, element);
Uint8 *pixels = NULL;
pixDataElem->getUint8Array(pixels);
//Writing the Raw data to a file...
FILE *file;
file = fopen("D:\DicomImage.raw", "wb");
// frameSize is the size of a single frame
fwrite(pixels + frameSize * frameIndex, sizeof(char), frameSize, file);
cout << "File write Completed and the File is closed Successfully" << endl;
(这是我想不出来的,所以不能保证完整性(
你得到的是原始的二进制数据。如果你想从中创建一个像JPG这样的图像文件,你需要相应的图像功能,尽管这与dcmtk无关。
如果您知道图像没有压缩,那么您可以使用Imebra:以这种方式访问第一帧的原始数据
imebra::DataSet loadedDataSet = imebra::CodecFactory::Load("pathToFileName);
size_t imageWidth = loadedDataSet.getUint32(imebra::TagId(imebra::tagId_t::Columns_0028_0011), 0);
size_t imageHeight = loadedDataSet.getUint32(imebra::TagId(imebra::tagId_t::Rows_0028_0010), 0);
size_t channels = loadedDataSet.getUint32(imebra::TagId(imebra::tagId_t::SamplesPerPixel_0028_0002), 0);
size_t allocatedBits = loadedDataSet.getUint32(imebra::TagId(imebra::tagId_t::BitsAllocated_0028_0100), 0);
size_t totalSizeBytes = (imageWidth * imageHeight * allocatedBits * channels + 7) / 8;
ReadingDataHandlerNumeric rawData = loadedDataSet.getReadingDataHandlerNumeric(TagId(PixelData_7FE0_0010), 0);
size_t dataSize(0);
const char* pMemory = rawData.data(&dataSize);
// Now pMemory points to the raw data, dataSize holds the memory size
如果您需要第二帧或者图像被压缩,那么您应该使用imebra::DataSet::getImage((,让imebra找到合适的内存区域并为您解压缩图像。
请注意,连续的未压缩图像不在字节边界上对齐,但第二帧的第一位可能在包含第一帧的最后一位的同一字节上。对于压缩图像,您可能需要处理指向包含图像的缓冲区的偏移表。
免责声明:我是Imebra的作者。
相关文章:
- 正在读取二进制文件(is_open)
- 如何从dicom文件中读取二进制数据
- C++用于从流中读取二进制值的任何标准库
- readsome() 适合在 Windows 上读取二进制数据吗?
- 读取二进制文件并解释为整数
- 读取二进制文件直到结束 c++
- 如何在标准c ++中流式传输/读取二进制文件的中间部分并写入另一个文件?
- 读取二进制文件的惯用C++17标准方法是什么
- 从末尾读取二进制文件
- C++逐个字符读取二进制文件字符
- 读取二进制文件大小的短自上周以来停止工作
- 读取二进制文件时出现问题
- 读取二进制文件时如何输出?
- C++:如何在线程中读取二进制文件并将其写入另一个线程?
- 在C 中读取二进制文件会返回意外值
- 尝试读取二进制文件会清除文件本身
- C 读取二进制文件
- 通过C 读取二进制文件的问题
- 用ifstream read()读取二进制的结构
- ifstream读取二进制文件在发行模式下工作,但在调试模式下不使用