#include <iostream>
#include <fstream>
#include "dcmtk/dcmdata/dctk.h"
void exportRtDoseToCsv(const char* doseFilePath, const char* outputCsvPath) {
// Load the RT dose file
DcmFileFormat doseFile;
if (doseFile.loadFile(doseFilePath).good()) {
// Get the pixel data element
DcmElement* pixelDataElement = nullptr;
if (doseFile.getDataset()->findAndGetElement(DCM_PixelData, pixelDataElement).good()) {
// Get the pixel data
Uint16* pixelData = nullptr;
if (pixelDataElement->getUint16Array(pixelData).good()) {
// Get the number of rows, columns, and Frames
Uint16 rows = 0, columns = 0;
int32_t frames = 0;
if (doseFile.getDataset()->findAndGetUint16(DCM_Rows, rows).good() &&
doseFile.getDataset()->findAndGetUint16(DCM_Columns, columns).good() &&
doseFile.getDataset()->findAndGetSint32(DcmTagKey(0x0028, 0x0008), frames).good()) {
// Create the output CSV file
std::ofstream csvFile(outputCsvPath);
if (csvFile.is_open()) {
// Write the pixel data to the CSV file for each slice
for (Uint16 slice = 0; slice < frames; ++slice) {
csvFile << "Slice " << slice + 1 << std::endl;
for (Uint16 row = 0; row < rows; ++row) {
for (Uint16 column = 0; column < columns; ++column) {
csvFile << pixelData[(slice * rows * columns) + (row * columns) + column] << ",";
}
csvFile << std::endl;
}
csvFile << std::endl;
}
csvFile.close();
std::cout << "RT dose data has been successfully exported to a CSV file." << std::endl;
}
else {
std::cout << "Unable to create the output CSV file." << std::endl;
}
}
else {
std::cout << "Unable to retrieve rows, columns, and Frames information." << std::endl;
}
}
else {
std::cout << "Unable to retrieve the pixel data." << std::endl;
}
}
else {
std::cout << "Pixel data element not found." << std::endl;
}
}
else {
std::cout << "Unable to load the RT dose file." << std::endl;
}
}
int main() {
const char* doseFilePath = "D:\\钉钉下载\\eclipse-8.1.20-phantom-ent\\Original\\RD.1.2.246.352.71.7.2088656855.452404.20110921073629.dcm"; // Replace with the actual RT dose file path
const char* outputCsvPath = "D:\\dcmtktest\\output.csv"; // Replace with the output CSV file path
exportRtDoseToCsv(doseFilePath, outputCsvPath);
return 0;
}