From 021fc5db0afce1915a208fbc0eb7c269ebff3129 Mon Sep 17 00:00:00 2001 From: tbaudier Date: Thu, 10 Oct 2019 09:21:31 +0200 Subject: [PATCH] From Benoit P, use clitkDicomRTStruct2Image with image with direction cosine --- common/clitkDicomRTStruct2ImageFilter.cxx | 87 +++++++++++++------- common/clitkDicomRTStruct2ImageFilter.h | 22 ++--- common/clitkDicomRT_Contour.cxx | 39 +++++---- common/clitkDicomRT_Contour.h | 3 + common/clitkDicomRT_ROI.cxx | 9 ++- common/clitkDicomRT_ROI.h | 6 +- common/clitkDicomRT_StructureSet.cxx | 4 +- common/clitkDicomRT_StructureSet.h | 2 +- common/vvImage.cxx | 3 +- common/vvImage.h | 2 +- tools/clitkDicom2Image.cxx | 36 +++++++-- tools/clitkDicomRTStruct2Image.cxx | 99 +++++++++++++---------- tools/clitkDicomRTStruct2Image.ggo | 13 +-- vv/vvToolROIManager.cxx | 8 +- vv/vvToolROIManager.h | 2 +- 15 files changed, 204 insertions(+), 131 deletions(-) diff --git a/common/clitkDicomRTStruct2ImageFilter.cxx b/common/clitkDicomRTStruct2ImageFilter.cxx index 2e11fdc..81c478e 100644 --- a/common/clitkDicomRTStruct2ImageFilter.cxx +++ b/common/clitkDicomRTStruct2ImageFilter.cxx @@ -23,6 +23,7 @@ // clitk #include "clitkDicomRTStruct2ImageFilter.h" #include "clitkImageCommon.h" +#include "vvImageWriter.h" // vtk #include @@ -32,6 +33,7 @@ #include #include #include +#include //-------------------------------------------------------------------- @@ -107,13 +109,19 @@ void clitk::DicomRTStruct2ImageFilter::SetImage(vvImage::Pointer image) { if (image->GetNumberOfDimensions() != 3) { std::cerr << "Error. Please provide a 3D image." << std::endl; - exit(0); + exit(EXIT_FAILURE); } mSpacing.resize(3); mOrigin.resize(3); mSize.resize(3); mDirection.resize(3); - mTransformMatrix = image->GetTransform()[0]->GetMatrix(); + //mTransformMatrix = image->GetTransform()[0]->GetMatrix(); + mTransformMatrix = vtkSmartPointer::New(); + for(unsigned int i=0;i<4;i++) { + for(unsigned int j=0;j<4;j++) { + mTransformMatrix->SetElement(i,j,image->GetTransform()[0]->GetMatrix()->GetElement(i,j)); + } + } for(unsigned int i=0; i<3; i++) { mSpacing[i] = image->GetSpacing()[i]; mOrigin[i] = image->GetOrigin()[i]; @@ -132,7 +140,7 @@ void clitk::DicomRTStruct2ImageFilter::SetImageFilename(std::string f) itk::ImageIOBase::Pointer header = clitk::readImageHeader(f); if (header->GetNumberOfDimensions() < 3) { std::cerr << "Error. Please provide a 3D image instead of " << f << std::endl; - exit(0); + exit(EXIT_FAILURE); } if (header->GetNumberOfDimensions() > 3) { std::cerr << "Warning dimension > 3 are ignored" << std::endl; @@ -149,6 +157,18 @@ void clitk::DicomRTStruct2ImageFilter::SetImageFilename(std::string f) for(unsigned int j=0; j<3; j++) mDirection[i][j] = header->GetDirection(i)[j]; } + //cf. AddItkImage function in vvImage.txx + mTransformMatrix = vtkSmartPointer::New(); + mTransformMatrix->Identity(); + for(unsigned int i=0; i<3; i++) { + double tmp = 0; + for(unsigned int j=0; j<3; j++) { + mTransformMatrix->SetElement(i,j,mDirection[i][j]); + tmp -= mDirection[i][j] * mOrigin[j]; + } + tmp += mOrigin[i]; + mTransformMatrix->SetElement(i,3,tmp); + } } //-------------------------------------------------------------------- @@ -182,11 +202,11 @@ void clitk::DicomRTStruct2ImageFilter::Update() { if (!mROI) { std::cerr << "Error. No ROI set, please use SetROI." << std::endl; - exit(0); + exit(EXIT_FAILURE); } if (!ImageInfoIsSet()) { std::cerr << "Error. Please provide image info (spacing/origin) with SetImageFilename" << std::endl; - exit(0); + exit(EXIT_FAILURE); } // Get Mesh @@ -206,7 +226,7 @@ void clitk::DicomRTStruct2ImageFilter::Update() // Get bounds double *bounds=mesh->GetBounds(); - + /* //Change mOrigin, mSize and mSpacing with respect to the directions // Spacing is influenced by input direction std::vector tempSpacing; @@ -238,7 +258,7 @@ void clitk::DicomRTStruct2ImageFilter::Update() } mSize[i] = lrint(tempSize[i]); } - + */ // Compute origin std::vector origin; origin.resize(3); @@ -259,7 +279,18 @@ void clitk::DicomRTStruct2ImageFilter::Update() extend[i] = mSize[i]-1; } } - + //Apply the transform to the mesh + vtkSmartPointer outputLabelmapGeometryTransform = vtkSmartPointer::New(); + outputLabelmapGeometryTransform->SetMatrix(mTransformMatrix); + // Apparently the inverse is wrong... + //outputLabelmapGeometryTransform->Inverse(); + vtkSmartPointer transformPolyDataFilter = vtkSmartPointer::New(); +#if VTK_MAJOR_VERSION <= 5 + transformPolyDataFilter->SetInput(mesh); +#else + transformPolyDataFilter->SetInputData(mesh); +#endif + transformPolyDataFilter->SetTransform(outputLabelmapGeometryTransform); // Create new output image mBinaryImage = vtkSmartPointer::New(); #if VTK_MAJOR_VERSION <= 5 @@ -281,11 +312,7 @@ void clitk::DicomRTStruct2ImageFilter::Update() // Extrude vtkSmartPointer extrude=vtkSmartPointer::New(); -#if VTK_MAJOR_VERSION <= 5 - extrude->SetInput(mesh); -#else - extrude->SetInputData(mesh); -#endif + extrude->SetInputConnection(transformPolyDataFilter->GetOutputPort()); ///We extrude in the -slice_spacing direction to respect the FOCAL convention (NEEDED !) extrude->SetVector(0, 0, -mSpacing[2]); @@ -295,11 +322,7 @@ void clitk::DicomRTStruct2ImageFilter::Update() //http://www.nabble.com/Bug-in-vtkPolyDataToImageStencil--td23368312.html#a23370933 sts->SetTolerance(0); sts->SetInformationInput(mBinaryImage); -#if VTK_MAJOR_VERSION <= 5 - sts->SetInput(extrude->GetOutput()); -#else sts->SetInputConnection(extrude->GetOutputPort(0)); -#endif //sts->SetInput(mesh); vtkSmartPointer stencil=vtkSmartPointer::New(); @@ -316,22 +339,26 @@ void clitk::DicomRTStruct2ImageFilter::Update() stencil->ReverseStencilOn(); stencil->Update(); - /* - vtkSmartPointer w = vtkSmartPointer::New(); - w->SetInput(stencil->GetOutput()); - w->SetFileName("binary2.mhd"); - w->Write(); - */ - mBinaryImage->ShallowCopy(stencil->GetOutput()); + vvImage::Pointer vvBinaryImage = vvImage::New(); + vtkSmartPointer vvBinaryImageT = vtkSmartPointer::New(); + vvBinaryImageT->SetMatrix(mTransformMatrix); + vvBinaryImage->AddVtkImage(mBinaryImage, vvBinaryImageT); + if (mWriteOutput) { - typedef itk::Image ImageType; - typedef itk::VTKImageToImageFilter ConnectorType; - ConnectorType::Pointer connector = ConnectorType::New(); - connector->SetInput(GetOutput()); - connector->Update(); - clitk::writeImage(connector->GetOutput(), mOutputFilename); + //typedef itk::Image ImageType; + //typedef itk::VTKImageToImageFilter ConnectorType; + //ConnectorType::Pointer connector = ConnectorType::New(); + //connector->SetInput(GetOutput()); + //connector->Update(); + //clitk::writeImage(connector->GetOutput(), mOutputFilename); + vvImageWriter::Pointer writer = vvImageWriter::New(); + writer->SetInput(vvBinaryImage); + if (!vvBinaryImage->GetTransform().empty()) + writer->SetSaveTransform(true); + writer->SetOutputFileName(mOutputFilename); + writer->Update(); } } //-------------------------------------------------------------------- diff --git a/common/clitkDicomRTStruct2ImageFilter.h b/common/clitkDicomRTStruct2ImageFilter.h index f93cbb2..9ba0845 100644 --- a/common/clitkDicomRTStruct2ImageFilter.h +++ b/common/clitkDicomRTStruct2ImageFilter.h @@ -72,17 +72,17 @@ namespace clitk { //-------------------------------------------------------------------- -template -typename itk::Image::ConstPointer clitk::DicomRTStruct2ImageFilter::GetITKOutput() -{ - assert(mBinaryImage); - typedef itk::Image ConnectorImageType; - typedef itk::VTKImageToImageFilter ConnectorType; - typename ConnectorType::Pointer connector = ConnectorType::New(); - connector->SetInput(mBinaryImage); - connector->Update(); - return connector->GetOutput(); -} +//template +//typename itk::Image::ConstPointer clitk::DicomRTStruct2ImageFilter::GetITKOutput() +//{ +// assert(mBinaryImage); +// typedef itk::Image ConnectorImageType; +// typedef itk::VTKImageToImageFilter ConnectorType; +// typename ConnectorType::Pointer connector = ConnectorType::New(); +// connector->SetInput(mBinaryImage); +// connector->Update(); +// return connector->GetOutput(); +//} //-------------------------------------------------------------------- #endif // CLITKDICOMRT_TRUCT2IMAGEFILTER_H diff --git a/common/clitkDicomRT_Contour.cxx b/common/clitkDicomRT_Contour.cxx index 9a5042b..6bc6332 100644 --- a/common/clitkDicomRT_Contour.cxx +++ b/common/clitkDicomRT_Contour.cxx @@ -82,11 +82,7 @@ void clitk::DicomRT_Contour::UpdateDicomItem() double * p = mData->GetPoint(i); points[i*3] = p[0]; points[i*3+1] = p[1]; -#if VTK_MAJOR_VERSION <= 5 - points[i*3+1] = p[2]; -#else points[i*3+1] = p[2]-0.5; -#endif } // Get attribute @@ -96,7 +92,7 @@ void clitk::DicomRT_Contour::UpdateDicomItem() at.SetFromDataElement( contourdata ); // Set attribute - at.SetValues(&points[0], points.size(), false); + at.SetValues(&points[0], points.size()); DD(at.GetValues()[0]); DD("replace"); @@ -161,14 +157,10 @@ bool clitk::DicomRT_Contour::Read(gdcm::Item * item) double p[3]; p[0] = points[i*3]; p[1] = points[i*3+1]; -#if VTK_MAJOR_VERSION <= 5 - p[2] = points[i*3+2]; -#else p[2] = points[i*3+2]+0.5; -#endif mData->SetPoint(i, p); if (mZ == -1) mZ = p[2]; - if (p[2] != mZ) { + if (std::fabs(p[2] - mZ) > mTolerance) { DD(i); DD(p[2]); DD(mZ); @@ -212,14 +204,10 @@ bool clitk::DicomRT_Contour::Read(gdcm::SQItem * item) double p[3]; p[0] = points[i*3]; p[1] = points[i*3+1]; -#if VTK_MAJOR_VERSION <= 5 - p[2] = points[i*3+2]; -#else p[2] = points[i*3+2]+0.5; -#endif mData->SetPoint(i, p); if (mZ == -1) mZ = p[2]; - if (p[2] != mZ) { + if (std::fabs(p[2] - mZ) > mTolerance) { DD(i); DD(p[2]); DD(mZ); @@ -259,8 +247,17 @@ void clitk::DicomRT_Contour::SetTransformMatrix(vtkMatrix4x4* matrix) mTransformMatrix = matrix; } //-------------------------------------------------------------------- - - +//-------------------------------------------------------------------- +double clitk::DicomRT_Contour::GetTolerance() +{ + return mTolerance; +} +//-------------------------------------------------------------------- +void clitk::DicomRT_Contour::SetTolerance(double tol) +{ + mTolerance = tol; +} +//-------------------------------------------------------------------- //-------------------------------------------------------------------- void clitk::DicomRT_Contour::ComputeMeshFromDataPoints() { @@ -271,10 +268,10 @@ void clitk::DicomRT_Contour::ComputeMeshFromDataPoints() mMesh->SetPoints(mPoints); vtkIdType ids[2]; for (unsigned int idx=0 ; idxGetPoint(idx)[j]; - pointIn[3] = 1.0; + //double pointIn[4]; + //for (unsigned int j=0 ; j<3; ++j) + // pointIn[j] = mData->GetPoint(idx)[j]; + //pointIn[3] = 1.0; /*double pointOut[4]; mTransformMatrix->MultiplyPoint(pointIn, pointOut); std::cout << pointOut[0] << " " << pointOut[1] << " " << pointOut[2] << " " << pointOut[3] << std::endl; diff --git a/common/clitkDicomRT_Contour.h b/common/clitkDicomRT_Contour.h index ab6db0d..389bbdd 100644 --- a/common/clitkDicomRT_Contour.h +++ b/common/clitkDicomRT_Contour.h @@ -56,6 +56,8 @@ public: vtkPoints * GetPoints() {return mData;} double GetZ() const {return mZ;} void SetTransformMatrix(vtkMatrix4x4* matrix); + double GetTolerance(); + void SetTolerance(double tol); protected: @@ -70,6 +72,7 @@ protected: bool mMeshIsUpToDate; ///Z location of the contour double mZ; + double mTolerance; #if GDCM_MAJOR_VERSION >= 2 gdcm::Item * mItem; diff --git a/common/clitkDicomRT_ROI.cxx b/common/clitkDicomRT_ROI.cxx index d8af8c3..7a2d4fc 100644 --- a/common/clitkDicomRT_ROI.cxx +++ b/common/clitkDicomRT_ROI.cxx @@ -149,7 +149,7 @@ double clitk::DicomRT_ROI::GetForegroundValueLabelImage() const //-------------------------------------------------------------------- #if GDCM_MAJOR_VERSION >= 2 -bool clitk::DicomRT_ROI::Read(gdcm::Item * itemInfo, gdcm::Item * itemContour) +bool clitk::DicomRT_ROI::Read(gdcm::Item * itemInfo, gdcm::Item * itemContour, double tol) { //FATAL("Error : compile vv with itk4 + external gdcm"); // Keep dicom item @@ -211,6 +211,7 @@ bool clitk::DicomRT_ROI::Read(gdcm::Item * itemInfo, gdcm::Item * itemContour) { gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1 DicomRT_Contour::Pointer c = DicomRT_Contour::New(); + c->SetTolerance(tol); c->SetTransformMatrix(mTransformMatrix); bool b = c->Read(&j); if (b) { @@ -221,7 +222,7 @@ bool clitk::DicomRT_ROI::Read(gdcm::Item * itemInfo, gdcm::Item * itemContour) return true; } #else -void clitk::DicomRT_ROI::Read(std::map & rois, gdcm::SQItem * item) +void clitk::DicomRT_ROI::Read(std::map & rois, gdcm::SQItem * item, double tol) { // ROI number [Referenced ROI Number] mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str()); @@ -238,6 +239,7 @@ void clitk::DicomRT_ROI::Read(std::map & rois, gdcm::SQItem * int i=0; for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) { DicomRT_Contour::Pointer c = DicomRT_Contour::New(); + c->SetTolerance(tol); c->SetTransformMatrix(mTransformMatrix); bool b = c->Read(j); if (b) { @@ -505,7 +507,7 @@ void clitk::DicomRT_ROI::ComputeContoursFromImage() //-------------------------------------------------------------------- #if CLITK_USE_SYSTEM_GDCM == 1 -void clitk::DicomRT_ROI::Read(vtkSmartPointer & reader, int roiindex) +void clitk::DicomRT_ROI::Read(vtkSmartPointer & reader, int roiindex, double tol) { vtkRTStructSetProperties * p = reader->GetRTStructSetProperties(); @@ -528,6 +530,7 @@ void clitk::DicomRT_ROI::Read(vtkSmartPointer & reader, i // Get the contour mMesh = reader->GetOutput(roiindex); DicomRT_Contour::Pointer c = DicomRT_Contour::New(); + c->SetTolerance(tol); c->SetTransformMatrix(mTransformMatrix); c->SetMesh(mMesh); // FIXME no GetZ, not GetPoints mMeshIsUpToDate = true; diff --git a/common/clitkDicomRT_ROI.h b/common/clitkDicomRT_ROI.h index b417933..d7c5a98 100644 --- a/common/clitkDicomRT_ROI.h +++ b/common/clitkDicomRT_ROI.h @@ -80,14 +80,14 @@ public: // Read from DICOM RT STRUCT #if GDCM_MAJOR_VERSION >= 2 - bool Read(gdcm::Item * itemInfo, gdcm::Item * itemContour); + bool Read(gdcm::Item * itemInfo, gdcm::Item * itemContour, double tol); void UpdateDicomItem(); #else - void Read(std::map & rois, gdcm::SQItem * item); + void Read(std::map & rois, gdcm::SQItem * item, double tol); #endif #if CLITK_USE_SYSTEM_GDCM == 1 - void Read(vtkSmartPointer & reader, int roiindex); + void Read(vtkSmartPointer & reader, int roiindex, double tol); #endif protected: diff --git a/common/clitkDicomRT_StructureSet.cxx b/common/clitkDicomRT_StructureSet.cxx index a845ef5..4b8f6f4 100644 --- a/common/clitkDicomRT_StructureSet.cxx +++ b/common/clitkDicomRT_StructureSet.cxx @@ -317,7 +317,7 @@ void clitk::DicomRT_StructureSet::Write(const std::string & filename) //-------------------------------------------------------------------- -void clitk::DicomRT_StructureSet::Read(const std::string & filename) +void clitk::DicomRT_StructureSet::Read(const std::string & filename, double tol) { //Try to avoid to use extern GDCM library @@ -448,7 +448,7 @@ void clitk::DicomRT_StructureSet::Read(const std::string & filename) // Create the roi mROIs[nb] = DicomRT_ROI::New(); mROIs[nb]->SetTransformMatrix(mTransformMatrix); - mROIs[nb]->Read(mMapOfROIInfo[nb], mMapOfROIContours[nb]); + mROIs[nb]->Read(mMapOfROIInfo[nb], mMapOfROIContours[nb], tol); } return; diff --git a/common/clitkDicomRT_StructureSet.h b/common/clitkDicomRT_StructureSet.h index dff4d75..16018c9 100644 --- a/common/clitkDicomRT_StructureSet.h +++ b/common/clitkDicomRT_StructureSet.h @@ -60,7 +60,7 @@ public: typedef ROIMapContainer::const_iterator ROIConstIteratorType; void Print(std::ostream & os = std::cout) const; - void Read(const std::string & filename); + void Read(const std::string & filename, double tol); void SetTransformMatrix(vtkMatrix4x4* matrix); bool IsDicomRTStruct(const std::string & filename); void Write(const std::string & filename); diff --git a/common/vvImage.cxx b/common/vvImage.cxx index f75d321..3fe5df2 100644 --- a/common/vvImage.cxx +++ b/common/vvImage.cxx @@ -70,7 +70,7 @@ void vvImage::Reset() //-------------------------------------------------------------------- //-------------------------------------------------------------------- -void vvImage::AddVtkImage(vtkImageData* input) +void vvImage::AddVtkImage(vtkImageData* input, vtkSmartPointer transform) { // RP: 20/12/2011 // Note that we're simply adding a new image to the vector. @@ -90,6 +90,7 @@ void vvImage::AddVtkImage(vtkImageData* input) mImageDimension = 1; mVtkImages.push_back(input); + mTransform.push_back(transform); } //-------------------------------------------------------------------- diff --git a/common/vvImage.h b/common/vvImage.h index cbb0284..81c2219 100644 --- a/common/vvImage.h +++ b/common/vvImage.h @@ -47,7 +47,7 @@ public : void Init(); void Reset(); template void AddItkImage(TItkImageType *input); - void AddVtkImage(vtkImageData* input); + void AddVtkImage(vtkImageData* input, vtkSmartPointer transform); const std::vector& GetVTKImages(); vtkImageData* GetFirstVTKImageData(); int GetNumberOfDimensions() const; diff --git a/tools/clitkDicom2Image.cxx b/tools/clitkDicom2Image.cxx index 614c5d9..c2253d7 100644 --- a/tools/clitkDicom2Image.cxx +++ b/tools/clitkDicom2Image.cxx @@ -62,7 +62,11 @@ int main(int argc, char * argv[]) NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New(); nameGenerator->SetUseSeriesDetails(false); std::string folderName="."; +#ifdef _WIN32 + const size_t last_slash_idx = input_files[0].rfind('\\'); +#else const size_t last_slash_idx = input_files[0].rfind('/'); +#endif if (std::string::npos != last_slash_idx) folderName = input_files[0].substr(0, last_slash_idx); nameGenerator->SetInputDirectory(folderName); @@ -87,7 +91,7 @@ int main(int argc, char * argv[]) #endif for(unsigned int i=0; i= 2 gdcm::Reader hreader; hreader.SetFileName(input_files[i].c_str()); @@ -161,6 +165,16 @@ int main(int argc, char * argv[]) std::vector origin = theorigin[*sn]; std::vector instanceNumberSerie = instanceNumber[*sn]; std::vector files = seriesFiles[*sn]; + //Let's process the filenames -- it is mandatory for the line "if (tempFilename == files[i])" + for(unsigned int i=0; i sliceIndex(files.size()); //clitk::GetSortedIndex(locs, sliceIndex); //Look for files into GDCMSeriesFileNames, because it sorts files correctly and take the order @@ -169,10 +183,18 @@ int main(int argc, char * argv[]) int j(0); bool found(false); while (!found && jSetOutputOrigin(origin[0], origin[1], locs[sliceIndex[0]]); modifier->Update(); vvImage::Pointer focal_image = vvImage::New(); - focal_image->AddVtkImage(modifier->GetOutput()); + focal_image->AddVtkImage(modifier->GetOutput(), image->GetTransform()[0]); image = focal_image; } @@ -289,7 +311,11 @@ int main(int argc, char * argv[]) std::ostringstream name; std::vector directory = clitk::SplitFilename(args_info.output_arg); if (directory.size() == 2) +#ifdef _WIN32 + name << directory[0] << "\\" << *sn << "_" << directory[1]; +#else name << directory[0] << "/" << *sn << "_" << directory[1]; +#endif else name << *sn << "_" << args_info.output_arg; outfile = name.str(); diff --git a/tools/clitkDicomRTStruct2Image.cxx b/tools/clitkDicomRTStruct2Image.cxx index 3790ab7..a62cee0 100644 --- a/tools/clitkDicomRTStruct2Image.cxx +++ b/tools/clitkDicomRTStruct2Image.cxx @@ -22,6 +22,31 @@ #include "clitkDicomRTStruct2Image_ggo.h" #include "clitkIO.h" +//-------------------------------------------------------------------- +std::string outputFileName(clitk::DicomRT_ROI::Pointer roi, const args_info_clitkDicomRTStruct2Image& args_info) +{ + std::string name = roi->GetName(); + int num = roi->GetROINumber(); + name.erase(remove_if(name.begin(), name.end(), isspace), name.end()); + std::string n; + n = std::string(args_info.output_arg).append(clitk::toString(num)).append("_").append(name); + if (args_info.mha_flag) { + n=n.append(".mha"); + } + else if (args_info.nii_flag) { + n=n.append(".nii"); + } + else if (args_info.niigz_flag) { + n=n.append(".nii.gz"); + } + else { + n=n.append(".mhd"); + } + if (args_info.verbose_flag) { + std::cout << num << " " << roi->GetName() << " num=" << num << " : " << n << std::endl; + } + return n; +} //-------------------------------------------------------------------- int main(int argc, char * argv[]) { @@ -31,27 +56,33 @@ int main(int argc, char * argv[]) { // Read and display information clitk::DicomRT_StructureSet::Pointer s = clitk::DicomRT_StructureSet::New(); - s->Read(args_info.input_arg); + s->Read(args_info.input_arg, args_info.tolerance_arg); if (args_info.verboseFile_flag) { s->Print(std::cout); } - - // New filter to convert to binary image - clitk::DicomRTStruct2ImageFilter filter; - filter.SetCropMaskEnabled(args_info.crop_flag); - filter.SetImageFilename(args_info.image_arg); // Used to get spacing + origin - if (args_info.vtk_flag) { - filter.SetWriteMesh(true); - } - if (args_info.roiName_given) { - filter.SetROI(s->GetROIFromROIName(args_info.roiName_arg)); - filter.SetOutputImageFilename(args_info.output_arg); - filter.Update(); - } - else if (args_info.roi_given && args_info.roi_arg != -1) { - filter.SetROI(s->GetROIFromROINumber(args_info.roi_arg)); - filter.SetOutputImageFilename(args_info.output_arg); - filter.Update(); + if (args_info.roiName_given || (args_info.roi_given && args_info.roi_arg != -1)) { + clitk::DicomRT_ROI::Pointer roi; + if (args_info.roiName_given) { + roi = s->GetROIFromROIName(args_info.roiName_arg); + } + else if (args_info.roi_given && args_info.roi_arg != -1) { + roi = s->GetROIFromROINumber(args_info.roi_arg); + } + if (roi) { + // New filter to convert to binary image + clitk::DicomRTStruct2ImageFilter filter; + filter.SetCropMaskEnabled(args_info.crop_flag); + filter.SetImageFilename(args_info.image_arg); // Used to get spacing + origin + if (args_info.vtk_flag) { + filter.SetWriteMesh(true); + } + filter.SetROI(roi); + filter.SetOutputImageFilename(outputFileName(roi, args_info)); + filter.Update(); + } else { + std::cerr<<"No ROI with this name/id"<begin(); iter != rois->end(); iter++) { clitk::DicomRT_ROI::Pointer roi = iter->second; clitk::DicomRTStruct2ImageFilter filter; - std::string name = roi->GetName(); - int num = roi->GetROINumber(); - filter.SetROI(roi); filter.SetCropMaskEnabled(args_info.crop_flag); filter.SetImageFilename(args_info.image_arg); // Used to get spacing + origin if (args_info.vtk_flag) { filter.SetWriteMesh(true); } - name.erase(remove_if(name.begin(), name.end(), isspace), name.end()); - std::string n; - if (args_info.mha_flag) { - n = std::string(args_info.output_arg).append - (clitk::toString(num)).append - ("_").append - (name).append - (".mha"); - } - else { - n = std::string(args_info.output_arg).append - (clitk::toString(num)).append - ("_").append - (name).append - (".mhd"); - } - if (args_info.verbose_flag) { - std::cout << num << " " << roi->GetName() << " num=" << num << " : " << n << std::endl; - } - filter.SetOutputImageFilename(n); - filter.Update(); + filter.SetROI(roi); + filter.SetOutputImageFilename(outputFileName(roi, args_info)); + filter.Update(); } + } else { + std::cerr<<"No ROIs with this substring of ROI name"< transformMatrix = vtkSmartPointer::New(); transformMatrix = mCurrentImage->GetTransform()[0]->GetMatrix(); s->SetTransformMatrix(transformMatrix); - s->Read(filename); + s->Read(filename, tol); // Loop on selected struct std::vector list = selector.getSelectedItems(); @@ -428,7 +428,7 @@ void vvToolROIManager::OpenDicomImage(std::string filename) // Get image vvImage::Pointer binaryImage = vvImage::New(); - binaryImage->AddVtkImage(filter.GetOutput()); + binaryImage->AddVtkImage(filter.GetOutput(), mCurrentImage->GetTransform()[0]); // Add to gui AddImage(binaryImage, s->GetROIFromROINumber(list[i])->GetName(), "", 0, true); // "" = no filename diff --git a/vv/vvToolROIManager.h b/vv/vvToolROIManager.h index db630c6..6ca20e9 100644 --- a/vv/vvToolROIManager.h +++ b/vv/vvToolROIManager.h @@ -63,7 +63,7 @@ class vvToolROIManager: void SelectedImageHasChanged(vvSlicerManager *); void Open(); void OpenBinaryImage(QStringList & filenames); - void OpenDicomImage(std::string filaneme); + void OpenDicomImage(std::string filaneme, double tol=0); void SelectedItemChangedInTree(); void VisibleROIToggled(bool b); void VisibleContourROIToggled(bool b); -- 2.45.1