Read DICOM Image property (Spacing, transform matrix) into ExtendMedicalImageProperties
This commit is contained in:
@@ -10,6 +10,9 @@
|
||||
#include <vtkNew.h>
|
||||
#include <unordered_map>
|
||||
#include <algorithm>
|
||||
#include <vtkMath.h>
|
||||
#include <vtkMatrix4x4.h>
|
||||
|
||||
struct DICOMTag{
|
||||
Uint16 group;
|
||||
Uint16 element;
|
||||
@@ -70,6 +73,7 @@ void DICOMDirectoryHelper::getDirectoryProperties(const char * rootPath, DICOMFi
|
||||
|
||||
void DICOMDirectoryHelper::getFileProperty(const std::string &path, DICOMFileMap& result) {
|
||||
DcmFileFormat file;
|
||||
// read success!
|
||||
if (file.loadFile(path).good()) {
|
||||
DcmDataset *dataset = file.getDataset();
|
||||
std::string SeriesUID;
|
||||
@@ -80,6 +84,7 @@ void DICOMDirectoryHelper::getFileProperty(const std::string &path, DICOMFileMap
|
||||
dataset->findAndGetSint32(DcmTagKey(0x0020, 0x0012), AcquisitionNumber);
|
||||
long InstanceNumber = 0;
|
||||
dataset->findAndGetSint32(DcmTagKey(0x0020, 0x0013), InstanceNumber);
|
||||
// series be firstly loaded
|
||||
if (!result.count(SeriesUID)) {
|
||||
ExtendMedicalImageProperties *seriesProperty = ExtendMedicalImageProperties::New();
|
||||
|
||||
@@ -116,7 +121,7 @@ void DICOMDirectoryHelper::getFileProperty(const std::string &path, DICOMFileMap
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0028, 0x1051), WindowWidth);
|
||||
seriesProperty->AddWindowLevelPreset(WindowWidth, WindowCenter);
|
||||
|
||||
double Spacing[3] = {0.0, 0.0, 0.0};
|
||||
double Spacing[3] = {0.0, 0.0, 1.0};
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0028, 0x0030), Spacing[0], 0);
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0028, 0x0030), Spacing[1], 1);
|
||||
seriesProperty->SetSpacing(Spacing);
|
||||
@@ -172,7 +177,6 @@ void DICOMDirectoryHelper::Update() {
|
||||
auto files = (*iterator)->GetFileNames();
|
||||
std::vector<DICOMFileRefValue> vector;
|
||||
for (auto f : pair.second) {
|
||||
// printf("%s\r\n",f.second.FilePath.c_str());
|
||||
vector.emplace_back(std::move(f.second));
|
||||
}
|
||||
std::sort(vector.begin(), vector.end(), [](const DICOMFileRefValue &v1, const DICOMFileRefValue &v2) {
|
||||
@@ -182,10 +186,9 @@ void DICOMDirectoryHelper::Update() {
|
||||
(v1.InstanceNumber < v2.InstanceNumber));
|
||||
});
|
||||
std::for_each(vector.begin(), vector.end(),[=](auto v){
|
||||
// printf("%s\r\n",v.FilePath.c_str());
|
||||
files->emplace_back(std::move(v.FilePath));
|
||||
});
|
||||
|
||||
readImageProperty(*iterator);
|
||||
}
|
||||
}
|
||||
SeriesCount = seriesProperties.size();
|
||||
@@ -215,4 +218,96 @@ void DICOMDirectoryHelper::Clear() {
|
||||
SeriesCount = 0;
|
||||
}
|
||||
|
||||
void DICOMDirectoryHelper::readImageProperty(ExtendMedicalImageProperties* property) {
|
||||
std::vector<std::string> *files = property->GetFileNames();
|
||||
DcmFileFormat file1;
|
||||
// read success!
|
||||
std::cout<<files->at(0)<<std::endl;
|
||||
if (file1.loadFile(files->at(0)).good()) {
|
||||
DcmDataset *dataset = file1.getDataset();
|
||||
|
||||
std::string SeriesUID;
|
||||
dataset->findAndGetOFString(DcmTagKey(0x0020, 0x000e), SeriesUID);
|
||||
long SeriesNumber = 0;
|
||||
dataset->findAndGetSint32(DcmTagKey(0x0020, 0x0011), SeriesNumber);
|
||||
long AcquisitionNumber = 0;
|
||||
dataset->findAndGetSint32(DcmTagKey(0x0020, 0x0012), AcquisitionNumber);
|
||||
long InstanceNumber = 0;
|
||||
dataset->findAndGetSint32(DcmTagKey(0x0020, 0x0013), InstanceNumber);
|
||||
std::cout<<"file1:"<<SeriesUID<<", "<<SeriesNumber<<", "<<AcquisitionNumber<<", "<<InstanceNumber<<std::endl;
|
||||
|
||||
double Spacing[3] = {0.0, 0.0, -1.0};
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0028, 0x0030), Spacing[0], 0);
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0028, 0x0030), Spacing[1], 1);
|
||||
property->SetSpacing(Spacing);
|
||||
|
||||
double orientation[6] = {.0, .0, .0, .0, .0, .0};
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0020, 0x0037), orientation[0], 0);
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0020, 0x0037), orientation[1], 1);
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0020, 0x0037), orientation[2], 2);
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0020, 0x0037), orientation[3], 3);
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0020, 0x0037), orientation[4], 4);
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0020, 0x0037), orientation[5], 5);
|
||||
property->SetDirectionCosine(orientation);
|
||||
|
||||
double position[3] = {.0, .0, .0};
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0020, 0x0032), position[0], 0);
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0020, 0x0032), position[1], 1);
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0020, 0x0032), position[2], 2);
|
||||
property->SetPosition(position);
|
||||
}
|
||||
double *d = property->GetDirectionCosine();
|
||||
double xVector[3] = {d[0], d[1], d[2]};
|
||||
double yVector[3] = {d[3], d[4], d[5]};
|
||||
double zVector[3] = {0, 0, 0};
|
||||
vtkMath::Cross(xVector, yVector, zVector);
|
||||
auto matrix = property->GetModelToWorldMatrix();
|
||||
matrix->Identity();
|
||||
matrix->SetElement( 0,0, xVector[0]);
|
||||
matrix->SetElement( 1,0, xVector[1]);
|
||||
matrix->SetElement( 2,0, xVector[2]);
|
||||
matrix->SetElement( 0,1, yVector[0]);
|
||||
matrix->SetElement( 1,1, yVector[1]);
|
||||
matrix->SetElement( 2,1, yVector[2]);
|
||||
matrix->SetElement( 0,2, zVector[0]);
|
||||
matrix->SetElement( 1,2, zVector[1]);
|
||||
matrix->SetElement( 2,2, zVector[2]);
|
||||
|
||||
vtkMatrix4x4::Invert(matrix, property->GetWorldToModelMatrix() );
|
||||
matrix = property->GetWorldToModelMatrix();
|
||||
DcmFileFormat file2;
|
||||
std::cout<<files->at(1)<<std::endl;
|
||||
|
||||
// read success!
|
||||
if (file2.loadFile(files->at(1)).good()) {
|
||||
DcmDataset *dataset = file2.getDataset();
|
||||
std::string SeriesUID;
|
||||
dataset->findAndGetOFString(DcmTagKey(0x0020, 0x000e), SeriesUID);
|
||||
std::string SeriesDes;
|
||||
dataset->findAndGetOFString(DcmTagKey(0x0008, 0x103e), SeriesDes);
|
||||
long SeriesNumber = 0;
|
||||
dataset->findAndGetSint32(DcmTagKey(0x0020, 0x0011), SeriesNumber);
|
||||
long AcquisitionNumber = 0;
|
||||
dataset->findAndGetSint32(DcmTagKey(0x0020, 0x0012), AcquisitionNumber);
|
||||
long InstanceNumber = 0;
|
||||
dataset->findAndGetSint32(DcmTagKey(0x0020, 0x0013), InstanceNumber);
|
||||
double position[3] = {.0, .0, .0};
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0020, 0x0032), position[0], 0);
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0020, 0x0032), position[1], 1);
|
||||
dataset->findAndGetFloat64(DcmTagKey(0x0020, 0x0032), position[2], 2);
|
||||
double position0[3] ={property->GetPosition()[0], property->GetPosition()[1] , property->GetPosition()[2]};
|
||||
double zNormal[4] = { position[0] - position0[0],
|
||||
position[1] - position0[1],
|
||||
position[2] - position0[2], 1.0};
|
||||
double space2 = sqrt(vtkMath::Distance2BetweenPoints(position, property->GetPosition()));
|
||||
vtkMath::Normalize(zNormal);
|
||||
double Spacing[3] = {.0, .0, .0};
|
||||
property->GetSpacing(Spacing);
|
||||
double zNormal2[4] = {.0,.0,.0,1.0};
|
||||
matrix->MultiplyPoint(zNormal, zNormal2);
|
||||
Spacing[2] = zNormal2[2]<0?(-1.0*space2):space2;
|
||||
property->SetSpacing(Spacing);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -55,6 +55,7 @@ private:
|
||||
DICOMFileMap &result);
|
||||
|
||||
void getFileProperty(const std::string &path, DICOMFileMap &result);
|
||||
void readImageProperty(ExtendMedicalImageProperties* property);
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user