c++ vtdcm對於壓縮模式文件圖片的讀取
//獲取dcm資料結構,一下影象資料僅針對單通道8位元組資料
DcmFileFormat fileformat;
OFCondition oc = fileformat.loadFile(dcmPath.c_str()); //讀取Dicom影象
if (!oc.good()) //判斷Dicom檔案是否讀取成功
{
std::cout << "file Load error" << std::endl;
errorInfo = "file Load error";
return false;
}
DcmDataset* dataset = fileformat.getDataset();
E_TransferSyntax xfer = dataset->getOriginalXfer(); //得到傳輸語法
const char* transferSyntax = NULL;
fileformat.getMetaInfo()->findAndGetString(DCM_TransferSyntaxUID, transferSyntax); //獲得傳輸語法字串
string losslessTransUID = "1.2.840.10008.1.2.4.70";
string lossTransUID = "1.2.840.10008.1.2.4.51";
string losslessP14 = "1.2.840.10008.1.2.4.57";
string lossyP1 = "1.2.840.10008.1.2.4.50";
string lossyRLE = "1.2.840.10008.1.2.5";
if (transferSyntax == losslessTransUID || transferSyntax == lossTransUID || transferSyntax == losslessP14 || transferSyntax == lossyP1)
{
DJDecoderRegistration::registerCodecs();
dataset->chooseRepresentation(EXS_LittleEndianExplicit, NULL); //對壓縮的影象畫素進行解壓
DJDecoderRegistration::cleanup();
}
else if (transferSyntax == lossyRLE)
{
DcmRLEDecoderRegistration::registerCodecs();
dataset->chooseRepresentation(EXS_LittleEndianExplicit, NULL);
DcmRLEDecoderRegistration::cleanup();
}
else
{
dataset->chooseRepresentation(xfer, NULL);
}
接下去有2種獲取幀資料的方法:
1,直接按照索引去資料集陣列中獲取
DcmElement* element = NULL;
OFCondition result = dataset->findAndGetElement(DCM_PixelData, element); //獲取影象資料
if (result.bad() || element == NULL)
return false;
Uint8* image_data = nullptr;
result = element->getUint8Array(image_data); //獲取8位的影象資料
//fsIndex 需要獲取的圖片幀索引
cv::Mat dst(m_height, m_width, CV_8UC1, cv::Scalar::all(0));
unsigned char* data = nullptr;
for (int i = 0; i < m_height; i++)
{
data = dst.ptr<unsigned char>(i);
for (int j = 0; j < m_width; j++)
{
unsigned char pxData = (unsigned char)(image_data[i * m_width + j + fsIndex]);
}
2,轉換成圖片資料結構再獲取每一個畫素的值
unsigned short m_width; //獲取影象的窗寬高
unsigned short m_height;
dataset->findAndGetUint16(DCM_Rows, m_height);
dataset->findAndGetUint16(DCM_Columns, m_width);
xfer = dataset->getCurrentXfer(); //得到新的傳輸語法,對於壓縮格式的dcm檔案讀取很重要
DicomImage* m_dcmImage = new DicomImage((DcmObject*)dataset, xfer); //利用dataset生成DicomImage,需要上面的解壓方法;
int framecount(m_dcmImage->getFrameCount()); //獲取這個檔案包含的影象的幀數
int nHeight = m_dcmImage->getHeight();
int nWidth = m_dcmImage->getWidth();
for (int k = 0; k < framecount; k++)
{
unsigned char* pixelData = (unsigned char*)(m_dcmImage->getOutputData(8, k, 0));
cv::Mat dst(nHeight, nWidth, CV_8UC1, cv::Scalar::all(0));
uchar* data = nullptr;
for (int i = 0; i < nHeight; i++)
{
data = dst.ptr<uchar>(i);
for (int j = 0; j < nWidth; j++)
{
data[j] = *(pixelData + i * nWidth + j);
}
}
}