#include <iostream>
#include <vector>
#include <cmath> // 添加头文件以使用 round 函数
#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dcfilefo.h"
#include "dcmtk/dcmdata/dctk.h"
#include "dcmtk/dcmrt/drtdose.h"
const double originalDoseGridSize = 2.5; // in mm
const double targetDoseGridSizeX = 1.074219; // in mm
const double targetDoseGridSizeY = 1.074219; // in mm
const double targetDoseGridSizeZ = 2.5; // in mm
typedef std::vector<std::vector<std::vector<Uint16>>> ThreeDimensionalArray;
bool resampleDoseGrid(const ThreeDimensionalArray& originalDoseData, ThreeDimensionalArray& resampledDoseData) {
double scaleX = originalDoseGridSize / targetDoseGridSizeX;
double scaleY = originalDoseGridSize / targetDoseGridSizeY;
double scaleZ = originalDoseGridSize / targetDoseGridSizeZ;
int resampledRows = static_cast<int>(std::ceil(originalDoseData[0].size() * scaleX)); // 向上取整
int resampledColumns = static_cast<int>(std::ceil(originalDoseData[0][0].size() * scaleY)); // 向上取整
int resampledFrames = originalDoseData.size();
resampledDoseData.resize(resampledFrames, std::vector<std::vector<Uint16>>(resampledRows, std::vector<Uint16>(resampledColumns)));
for (int f = 0; f < resampledFrames; ++f) {
for (int r = 0; r < resampledRows; ++r) {
for (int c = 0; c < resampledColumns; ++c) {
int originalR = static_cast<int>(r / scaleX);
int originalC = static_cast<int>(c / scaleY);
if (originalR < originalDoseData[f].size() && originalC < originalDoseData[f][originalR].size()) {
resampledDoseData[f][r][c] = originalDoseData[f][originalR][originalC];
}
else {
// Handle out-of-bounds access
// For example, you can assign a default value or skip this voxel
resampledDoseData[f][r][c] = 0; // Assigning 0 as default value
}
}
}
}
// 判断是否插值成功
return (resampledDoseData.size() == resampledFrames && resampledDoseData[0].size() == resampledRows && resampledDoseData[0][0].size() == resampledColumns);
}
int main() {
const char* doseFilePath = "D:\\dicom_All\\Original\\RD.1.2.246.352.71.7.2088656855.452404.20110921073629.dcm";
DcmFileFormat doseFileFormat;
if (doseFileFormat.loadFile(doseFilePath).bad()) {
std::cerr << "Error loading RT Dose file: " << doseFilePath << std::endl;
return 1;
}
DRTDoseIOD rtDose;
if (rtDose.read(*doseFileFormat.getDataset()).bad()) {
std::cerr << "Error reading RT Dose dataset." << std::endl;
return 1;
}
Uint16 rows, columns;
Sint32 frames;
if (rtDose.getRows(rows).bad() || rtDose.getColumns(columns).bad() || rtDose.getNumberOfFrames(frames).bad()) {
std::cerr << "Error getting dose grid size." << std::endl;
return 1;
}
std::cout << "Original Dose Grid Size:" << std::endl;
std::cout << "Frames: " << frames << ", Rows: " << rows << ", Columns: " << columns << std::endl;
ThreeDimensionalArray originalDoseData(frames, std::vector<std::vector<Uint16>>(rows, std::vector<Uint16>(columns)));
DcmElement* pixelDataElement = nullptr;
if (doseFileFormat.getDataset()->findAndGetElement(DCM_PixelData, pixelDataElement).bad()) {
std::cerr << "Error getting pixel data element." << std::endl;
return 1;
}
Uint16* pixelData = nullptr;
if (pixelDataElement->getUint16Array(pixelData).bad()) {
std::cerr << "Error getting pixel data." << std::endl;
return 1;
}
size_t index = 0;
for (int f = 0; f < frames; ++f) {
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < columns; ++c) {
originalDoseData[f][r][c] = pixelData[index++];
}
}
}
ThreeDimensionalArray resampledDoseData;
bool success = resampleDoseGrid(originalDoseData, resampledDoseData);
if (success) {
std::cout << "Dose grid resampling successful." << std::endl;
int resampledFrames = resampledDoseData.size();
int resampledRows = resampledDoseData[0].size();
int resampledColumns = resampledDoseData[0][0].size();
std::cout << "Resampled Dose Grid Size:" << std::endl;
std::cout << "Frames: " << resampledFrames << ", Rows: " << resampledRows << ", Columns: " << resampledColumns << std::endl;
// 打印插值后的像素数据
std::cout << "Resampled Pixel Data:" << std::endl;
for (int f = 0; f < resampledFrames; ++f) {
for (int r = 0; r < resampledRows; ++r) {
for (int c = 0; c < resampledColumns; ++c) {
//std::cout << resampledDoseData[f][r][c] << " ";
}
//std::cout << std::endl; // 打印完一行数据后换行
}
//std::cout << std::endl; // 打印完一帧数据后换行
}
}
else {
std::cerr << "Error resampling dose grid." << std::endl;
return 1;
}
return 0;
}
dose Pixel data插值,将原dose pixel插值为image的大小。
于 2024-04-02 14:29:55 首次发布