00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __freIntensityImageMediaController_h
00023 #define __freIntensityImageMediaController_h
00024
00025 #include "freImageMediaControllerBase.h"
00026 #include "freTransformFunctionMedia.h"
00027 #include "freImageIO.h"
00028
00029 #include "itkWarpImageFilter.h"
00030 #include "itkResampleImageFilter.h"
00031
00032 namespace FREE
00033 {
00034
00040 freControllerIDMacro(IntensityImageMediaControllerBase, "IntensityImageMediaBase");
00041 template <class TInternalPixel, class TExternalPixel, unsigned int VDimension>
00042 class IntensityImageMediaControllerBase : public ImageMediaControllerBase< itk::Image< TInternalPixel, VDimension > >
00043 {
00044 public:
00045 typedef itk::Image< TInternalPixel, VDimension > ComponentType;
00046 typedef ComponentType ImageType;
00047 typedef ImageMediaControllerBase<ComponentType> Superclass;
00048 typedef typename Superclass::ComponentPointer ComponentPointer;
00049 typedef typename Superclass::GenericMediaPointer GenericMediaPointer;
00050
00051 itkTypeMacro(IntensityImageMediaControllerBase, ImageMediaControllerBase);
00052
00053 DeclareParameterMacro(RescaleImage);
00054 DeclareParameterMacro(RescaleOutputRange);
00055
00056 IntensityImageMediaControllerBase()
00057 {
00058 this->UpdateControllerID(ControllerID::IntensityImageMediaControllerBase);
00059 this->m_Description = "Base class for media containing intensity images.";
00060 };
00061
00062
00063 virtual GenericMediaPointer ComputeActualizedMedia(GenericMediaType* pGenericMedia,
00064 GenericMediaType* pGenericTransformationField) const
00065 {
00066 if (!pGenericMedia) throwCtrlExceptionMacro("","Passed media is NULL.");
00067 if (!pGenericTransformationField) throwCtrlExceptionMacro("","Passed transform field is NULL.");
00068
00069 typename ImageType::Pointer smpCurrentImage;
00070 try
00071 {
00072 smpCurrentImage = dynamic_cast<ImageType*>(pGenericMedia);
00073 }
00074 catchAllNPassMacro("Error. Media that should be actualized seems not to be of correct type.");
00075 if (smpCurrentImage.IsNull()) throwCtrlExceptionMacro("","Media has wrong type. Cannot be casted.");
00076
00077 typedef typename ImageTypes<VDimension>::TransformationFieldType TransformationFieldType;
00078 typename TransformationFieldType::Pointer smpTransfromField = static_cast< TransformationFieldType*>(pGenericTransformationField);
00079
00080 typedef itk::WarpImageFilter< ImageType, ImageType, TransformationFieldType > WarpFilterType;
00081
00082 typename WarpFilterType::Pointer smpWarpFilter = WarpFilterType::New();
00083
00084 smpWarpFilter->SetInput(smpCurrentImage);
00085 smpWarpFilter->SetOutputSpacing(smpCurrentImage->GetSpacing());
00086 smpWarpFilter->SetOutputOrigin(smpCurrentImage->GetOrigin());
00087 smpWarpFilter->SetDeformationField(smpTransfromField);
00088 smpCurrentImage = smpWarpFilter->GetOutput();
00089 smpWarpFilter->Update();
00090
00091 GenericMediaPointer smpResult = smpCurrentImage.GetPointer();
00092
00093 return smpResult;
00094 };
00095
00096 virtual GenericMediaPointer ComputeActualizedMediaByFunction(GenericMediaType* pGenericMedia,
00097 GenericMediaType* pGenericTransformationFunction) const
00098 {
00099 if (!pGenericMedia) throwCtrlExceptionMacro("","Passed media is NULL.");
00100 if (!pGenericTransformationFunction) throwCtrlExceptionMacro("","Passed transform function is NULL.");
00101
00102 typename ImageType::Pointer smpCurrentImage;
00103 try
00104 {
00105 smpCurrentImage = dynamic_cast<ImageType*>(pGenericMedia);
00106 }
00107 catchAllNPassMacro("Error. Media that should be actualized seems not to be of correct type.");
00108 if (smpCurrentImage.IsNull()) throwCtrlExceptionMacro("","Media has wrong type. Cannot be casted.");
00109
00110 typedef TransformFunctionMedia<ScalarType, VDimension, VDimension> TransformationFunctionType;
00111 typename TransformationFunctionType::Pointer smpTransfromFunction = static_cast< TransformationFunctionType*>(pGenericTransformationFunction);
00112
00113 typedef itk::ResampleImageFilter< ImageType, ImageType, ScalarType > ResampleFilterType;
00114
00115 typename ResampleFilterType::Pointer smpResampleFilter = ResampleFilterType::New();
00116
00117 smpResampleFilter->SetInput(smpCurrentImage);
00118 smpResampleFilter->SetOutputParametersFromImage(smpCurrentImage);
00119 smpResampleFilter->SetOutputStartIndex(smpCurrentImage->GetLargestPossibleRegion().GetIndex());
00120 smpResampleFilter->SetTransform(smpTransfromFunction->GetTransform());
00121 smpCurrentImage = smpResampleFilter->GetOutput();
00122 smpResampleFilter->Update();
00123
00124 GenericMediaPointer smpResult = smpCurrentImage.GetPointer();
00125
00126 return smpResult;
00127 };
00128
00129 virtual bool SaveMedia(std::string sMediaPath, GenericMediaType* pMedia) const
00130 {
00131 typedef ImageWriter<TInternalPixel, TExternalPixel, VDimension> ImageWriteType;
00132
00133 ImageWriteType imageWriter;
00134 imageWriter.SetFileName(sMediaPath);
00135 imageWriter.SetRescaleImage(true);
00136 imageWriter.SetRescaleMinimum(0);
00137 imageWriter.SetRescaleMaximum(255);
00138 imageWriter.SetInput( static_cast<ComponentType*>(pMedia));
00139 imageWriter.Update();
00140
00141 return true;
00142 };
00143
00149 virtual bool SaveMedia(SessionComponentCache* pCache) const
00150 {
00151 if (!pCache) throwCtrlExceptionMacro("", "Cannot save media. Passed session cache is NULL.");
00152
00153 if (!pCache->SetupIsAssigned()) throwCtrlExceptionMacro("", "Cannot save media. Passed session cache has no setup.");
00154 if (!pCache->ComponentIsAssigned()) throwCtrlExceptionMacro("","Error. Image media is missing in cache.");
00155
00156 this->NotifyProgress(1,"Saving image media",pCache);
00157
00158 ComponentSetup* pComponentSetup = pCache->Setup();
00159
00160 bool bRescaleImage;
00161 double dOutputMinimum;
00162 double dOutputMaximum;
00163 int iSource;
00164 std::string sFile;
00165
00166 try
00167 {
00168 SessionAccessor::GetParameterValue(pCache,cParam_RescaleImage,bRescaleImage);
00169 SessionAccessor::GetParameterValue(pCache,cParam_RescaleOutputRange,dOutputMinimum);
00170 SessionAccessor::GetParameterValue(pCache,cParam_RescaleOutputRange,dOutputMaximum,1);
00171 SessionAccessor::GetParameterValue(pCache,Superclass::cParam_MediaFile,sFile);
00172 SessionAccessor::GetParameterValue(pCache,Superclass::cParam_ImageSource,iSource);
00173 }
00174 catchAllNPassMacro("Error while retrieving parameter values.");
00175
00176 if (iSource!=1)
00177 {
00178 this->NotifyProgress(1,"Image source is no file, cannot save media to a file",pCache);
00179 return false;
00180 }
00181
00182 std::stringstream strm;
00183 strm << "Saving parameter: filename: "<<sFile<<"; rescale image: "<< bRescaleImage;
00184 if (bRescaleImage) strm <<"(min: "<<dOutputMinimum<<"; max: "<<dOutputMaximum<<")";
00185 this->NotifyProgress(1,strm.str(),pCache);
00186
00187 GenericComponentType::Pointer smpMedia = DirectSessionComponentAccessor::GetComponent(pCache);
00188
00189 typedef ImageWriter<TInternalPixel, TExternalPixel , VDimension> ImageWriterType;
00190
00191 ImageWriterType imageWriter;
00192 imageWriter.SetFileName(sFile);
00193 imageWriter.SetRescaleImage(bRescaleImage);
00194 imageWriter.SetRescaleMinimum(dOutputMinimum);
00195 imageWriter.SetRescaleMaximum(dOutputMaximum);
00196 imageWriter.SetInput( static_cast<ComponentType*>(smpMedia.GetPointer()));
00197 imageWriter.Update();
00198
00199 this->NotifyProgress(1,"Saving finished",pCache);
00200
00201 return true;
00202 };
00203
00204 virtual bool LoadMedia(SessionComponentCache* pCache) const
00205 {
00206 if (!pCache) throwCtrlExceptionMacro("", "Cannot load media. Passed session cache is NULL.");
00207
00208 if (!pCache->SetupIsAssigned()) throwCtrlExceptionMacro("", "Cannot load media. Passed session cache has no setup.");
00209
00210 this->NotifyProgress(1,"Loading image media",pCache);
00211
00212 ComponentSetup* pComponentSetup = pCache->Setup();
00213
00214 bool bRescaleImage;
00215 double dOutputMinimum = 0;
00216 double dOutputMaximum = 0;
00217 int iSource;
00218 std::string sFile;
00219
00220 ComponentPointer component;
00221
00222 try
00223 {
00224 SessionAccessor::GetParameterValue(pCache,cParam_RescaleImage,bRescaleImage);
00225 if (bRescaleImage)
00226 {
00227 SessionAccessor::GetParameterValue(pCache,cParam_RescaleOutputRange,dOutputMinimum);
00228 SessionAccessor::GetParameterValue(pCache,cParam_RescaleOutputRange,dOutputMaximum,1);
00229 }
00230 SessionAccessor::GetParameterValue(pCache,Superclass::cParam_MediaFile,sFile);
00231 SessionAccessor::GetParameterValue(pCache,Superclass::cParam_ImageSource,iSource);
00232 }
00233 catchAllNPassMacro("Error while retrieving parameter values.");
00234
00235 std::stringstream strm;
00236 strm << "Loading parameter: filename: "<<sFile<<"; rescale image: "<< std::boolalpha << bRescaleImage;
00237 if (bRescaleImage) strm <<"(min: "<<dOutputMinimum<<"; max: "<<dOutputMaximum<<")";
00238 this->NotifyProgress(1,strm.str(),pCache);
00239
00240 if (iSource==1)
00241 {
00242 typedef ImageReader<TExternalPixel, TInternalPixel, VDimension> ImageReaderType;
00243
00244 ImageReaderType imageReader;
00245 imageReader.SetFileName(sFile);
00246 imageReader.SetForceIsoVoxel(false);
00247 imageReader.SetRescaleImage(bRescaleImage);
00248 imageReader.SetRescaleMinimum(dOutputMinimum);
00249 imageReader.SetRescaleMaximum(dOutputMaximum);
00250
00251 component = (ComponentType*)(imageReader.GetOutput());
00252 }
00253 else if (iSource==2)
00254 {
00255 if (!MakeCallback(0,&component,pCache)) throwCtrlExceptionMacro("","Unable to retreave the image by callback.");
00256 if (component.IsNull()) throwCtrlExceptionMacro("","No image specified by callback.");
00257 }
00258
00259 DirectSessionComponentAccessor::SetComponent(component,pCache);
00260
00261 this->ActualizeMediaValidityTag("media",pCache);
00262 this->NotifyProgress(1,"Loading finished",pCache);
00263
00264 return true;
00265 };
00266
00267 protected:
00268
00269 virtual void SetStatisticEntryMainComponent(StatisticEntry& rStatisticEntry,
00270 ComponentType* pMainComponent,
00271 SessionComponentCache* pMainComponentCache,
00272 SessionInfo* pSessionInfo,
00273 StatisticDictionary& rDictionary) const
00274 {
00275 Superclass::SetStatisticEntryMainComponent(rStatisticEntry, pMainComponent, pMainComponentCache, pSessionInfo, rDictionary);
00276
00277 std::string sIDPath = pMainComponentCache->GetIDPath();
00278 StatisticValueDefinition* pEntry = rDictionary.GetValueDefinitionByName(sIDPath,"RescaleImage");
00279
00280 if (!pEntry)
00281 {
00282 pEntry = rDictionary.AddValueDefinition(sIDPath,"RescaleImage","Media was rescaled");
00283 rDictionary.AddValueDefinition(sIDPath,"OutputMinimum","Intensity minimum after rescaling");
00284 rDictionary.AddValueDefinition(sIDPath,"OutputMaximum","Intensity maximum after rescaling");
00285 }
00286
00287 bool bRescaleImage;
00288 double dOutputMinimum;
00289 double dOutputMaximum;
00290
00291 try
00292 {
00293 SessionAccessor::GetParameterValue(pMainComponentCache,cParam_RescaleImage,bRescaleImage);
00294 SessionAccessor::GetParameterValue(pMainComponentCache,cParam_RescaleOutputRange,dOutputMinimum);
00295 SessionAccessor::GetParameterValue(pMainComponentCache,cParam_RescaleOutputRange,dOutputMaximum,1);
00296 }
00297 catchAllNPassMacro("Error while retrieving parameter values.");
00298
00299 rStatisticEntry.AddValue(Convert::ToStr(bRescaleImage),pEntry->GetRefID());
00300 rStatisticEntry.AddValue(Convert::ToStr(dOutputMinimum),pEntry->GetRefID()+1);
00301 rStatisticEntry.AddValue(Convert::ToStr(dOutputMaximum),pEntry->GetRefID()+2);
00302 };
00303
00304 virtual void GenerateProfile(CtrlProfile::ControllerProfile& profile,
00305 const SessionComponentCache* pComponentCache,
00306 bool bRegardOldSetup) const
00307 {
00308 Superclass::GenerateProfile(profile, pComponentCache, bRegardOldSetup);
00309
00310
00311 profile.Parameters().AddParameter(cParam_RescaleImage,Parameter::PVTBool,cParamDsc_RescaleImage,1,"false");
00312
00313 if (bRegardOldSetup)
00314 {
00315
00316 bool bRescale = false;
00317 if (pComponentCache->Setup()->Parameters().GetParameterValue(cParam_RescaleImage, bRescale))
00318 {
00319 if (bRescale)
00320 {
00321 profile.Parameters().AddParameter(cParam_RescaleOutputRange, Parameter::PVTInteger, cParamDsc_RescaleOutputRange,2,"0");
00322 }
00323 }
00324 }
00325 };
00326
00327 };
00328
00329 template <class TInternalPixel, class TExternalPixel, unsigned int VDimension>
00330 const char* const IntensityImageMediaControllerBase<TInternalPixel, TExternalPixel, VDimension> :: cParam_RescaleImage= "RescaleImage";
00331 template <class TInternalPixel, class TExternalPixel, unsigned int VDimension>
00332 const char* const IntensityImageMediaControllerBase<TInternalPixel, TExternalPixel, VDimension> :: cParamDsc_RescaleImage = "Rescales the image intensities with in the range defined by RescaleInputRange and RescaleOutputRange.";
00333 template <class TInternalPixel, class TExternalPixel, unsigned int VDimension>
00334 const char* const IntensityImageMediaControllerBase<TInternalPixel, TExternalPixel, VDimension> :: cParam_RescaleOutputRange = "RescaleOutputRange";
00335 template <class TInternalPixel, class TExternalPixel, unsigned int VDimension>
00336 const char* const IntensityImageMediaControllerBase<TInternalPixel, TExternalPixel, VDimension> :: cParamDsc_RescaleOutputRange = "First value is the lowest value that should be rescales, second the highest value.";
00337
00338
00344 freControllerIDMacro(IntensityImage2DMediaController, "Intensity Image 2D Media");
00345 class IntensityImage2DMediaController : public IntensityImageMediaControllerBase< InternalImagePixelType, LoadingPixelType, 2>
00346 {
00347 public:
00348 typedef IntensityImageMediaControllerBase<InternalImagePixelType, LoadingPixelType, 2> Superclass;
00349 typedef Superclass::ComponentType ComponentType;
00350
00351 itkTypeMacro(IntensityImage2DMediaController, IntensityImageMediaControllerBase);
00352
00353
00354 IntensityImage2DMediaController();
00355 };
00356
00362 freControllerIDMacro(IntensityImage3DMediaController, "Intensity Image 3D Media");
00363 class IntensityImage3DMediaController : public IntensityImageMediaControllerBase< InternalImagePixelType, LoadingPixelType, 3>
00364 {
00365 public:
00366 typedef IntensityImageMediaControllerBase<InternalImagePixelType, LoadingPixelType, 3> Superclass;
00367 typedef Superclass::ComponentType ComponentType;
00368
00369 itkTypeMacro(IntensityImage3DMediaController, IntensityImageMediaControllerBase);
00370
00371 IntensityImage3DMediaController();
00372 };
00373
00374 }
00375
00376 #endif