MeVisLabToolboxReference
FMEwork/ITK/Sources/ITK/MLITK/ITKSupport/mlITKFilterSupport.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00005 
00010 //----------------------------------------------------------------------------------
00011 #ifndef __mlITKFilterSupport_H
00012 #define __mlITKFilterSupport_H
00013 
00015 // Include dll-specific settings.
00016 #include "mlInitSystemITKSupport.h"
00017 
00019 #ifndef __mlITKSupportToolFunctions_H
00020 #include "mlITKSupportToolFunctions.h"
00021 #endif
00022 
00023 ML_START_NAMESPACE
00024 
00025 //----------------------------------------------------------------------------
00027 //----------------------------------------------------------------------------
00028 inline void testFunc_processDataWithITKFilterAndFillOutSubImg() {}
00029 
00030 //---------------------------------------------------------------------------
00046 //---------------------------------------------------------------------------
00047 template <typename FILTER_TYPE, typename OIMAGE_TYPE>
00048 bool processDataWithITKFilterAndFillOutSubImg(typename FILTER_TYPE::Pointer &filterPointer, 
00049                                               int                           outIndex, 
00050                                               int                           usedFilteringDim, 
00051                                               const ImageVector             &shift,
00052                                               const SubImageBox             &outImgBox,
00053                                               SubImage                      &outSubImg, 
00054                                               Module                        &module)
00055 {
00056   ML_TRACE_IN("template processDataWithITKFilterAndFillOutSubImg()");
00057 
00058   // Test and debug function call.
00059   testFunc_processDataWithITKFilterAndFillOutSubImg();
00060 
00061   bool okay=true;
00062   try {
00063 
00064     // Get filter output.
00065     typename OIMAGE_TYPE::Pointer filterOutput = filterPointer->GetOutput(outIndex);
00066 
00067     // Be sure that extent of output page does not reach outside the output image 
00068     // to avoid that undefined regions are requested. In contrast to the ML ITK 
00069     // does not permit that.
00070     SubImageBox outBox(outSubImg.getBox().intersect(outImgBox));
00071 
00072     // Shift the valid image region which is to be calculated to ITK coordinates.
00073     outBox.translate(shift);
00074 
00075     // Set region to be calculated.
00076     typename OIMAGE_TYPE::RegionType itkOutBox = ITKRegionFromMLSubImgBox<OIMAGE_TYPE>(outBox);
00077     filterOutput->SetRequestedRegion(itkOutBox);
00078 
00079     // Process requested output region image buffer.
00080     filterPointer->Update();
00081 
00082     // Get output image of the filter and pointer to its data.
00083     typename OIMAGE_TYPE::PixelType* dataPtr = filterOutput->GetBufferPointer();
00084 
00085     // Get region of output image and build an ML SubImageBox from it.
00086     SubImageBox outSubImgBox = MLSubImgBoxFromITKRegion<OIMAGE_TYPE>(filterOutput->GetBufferedRegion());
00087 
00088     // Shift processed ITK output region to ML coordinates.
00089     outSubImgBox.translate(-shift);
00090 
00091     // Here we need to correct the position of the outSubimgBox in the case that 
00092     // we have images which are higher dimensional than the dimension of the 
00093     // compiled ITK filter. We need to place the box at the original high dimensional
00094     // position to be able to copy it back to the output subimage box.
00095     for (MLint d=usedFilteringDim; d<6; ++d){
00096       // Copy higher dimensional components of both corners from outBox.
00097       // This will probably not work if filters project to lower dimensions,
00098       // which, however, is not supported in other code fragments either.
00099       outSubImgBox.v1[d] = outBox.v1[d];
00100       outSubImgBox.v2[d] = outBox.v2[d];
00101     }
00102 
00103     // Convert and copy the ITK buffer to the ML subimage.
00104     copyITKDataBufferToMLSubImg(dataPtr, sizeof(ITKML_TYPENAME OIMAGE_TYPE::PixelType), outSubImgBox, outSubImg);
00105   }
00106   catch( itk::ExceptionObject & e ){
00107     // Handle error.
00108     postITKException(e, &module, ML_ERROR, "Leaving output subimage unprocessed.");
00109     okay=false;
00110   }
00111   return okay;
00112 }
00113 
00114 //----------------------------------------------------------------------------
00116 //----------------------------------------------------------------------------
00117 inline void testFunc_determineImageFilterInRegionTN() {}
00118 
00119 //----------------------------------------------------------------------------
00131 //----------------------------------------------------------------------------
00132 template <typename FILTER_TYPE, typename IN_IMAGE_TYPE>
00133 SubImageBox determineImageFilterInRegionTN(typename FILTER_TYPE::Pointer &filterPointer,
00134                                            Module                        &module, 
00135                                            MLint                          inIndex, 
00136                                            const SubImageBox             &outBox, 
00137                                            MLint                          outIndex,
00138                                            MLint                          numActiveInputs)
00139 {      
00140   ML_TRACE_IN("template determineImageFilterInRegionTN(6 args)");                 
00141 
00142   // Test and debug function call.
00143   testFunc_determineImageFilterInRegionTN();
00144 
00145   // Check for active input - on inactve inputs we simply return an empty box,
00146   // because there data requests do not make sense.
00147   if (inIndex >= numActiveInputs){
00148     return SubImageBox();
00149   }
00150 
00151   if(!filterPointer){
00152     MLPrintAndNotify(ML_ERROR, "ML_","", "ITK Filter is NULL.",
00153                      "", "Returning empty box",                                               
00154                      __FILE__, __LINE__, ML_UNKNOWN_EXCEPTION);  
00155     return SubImageBox();    
00156   }
00157 
00158   // For each input get input image region as box. All other are left empty.
00159   // Get outBox as ITK region object.
00160   typename IN_IMAGE_TYPE::RegionType region =
00161     ITKRegionFromMLSubImgBox<IN_IMAGE_TYPE>(outBox);
00162 
00163   // Convert boxes to ITK boxes and pass them to the filter.
00164   for (MLint inps=0; inps < numActiveInputs; ++inps){
00165     // Get input image extents as ITK region object.
00166     typename IN_IMAGE_TYPE::RegionType largestPossibleRegion = 
00167       ITKRegionFromMLSubImgBox<IN_IMAGE_TYPE>(module.getInputImage(inps)->getBoxFromImageExtent());    
00168 
00169     // Create a filter object, set input image extents.
00170     typename IN_IMAGE_TYPE::Pointer m_Image = IN_IMAGE_TYPE::New();  
00171     m_Image->SetRegions(largestPossibleRegion); 
00172     filterPointer->SetInput(inps, m_Image);
00173   }
00174 
00175   // Set requested region at output, and ask for the input 
00176   // region which is needed by the filter.
00177   try       
00178     {
00179     filterPointer->GetOutput(outIndex)->SetRequestedRegion(region); 
00180     filterPointer->GetOutput(outIndex)->UpdateOutputInformation();  
00181     filterPointer->GetOutput(outIndex)->PropagateRequestedRegion(); 
00182     typename IN_IMAGE_TYPE::RegionType inReg = filterPointer->GetInput(inIndex)->GetRequestedRegion();  
00183     SubImageBox retRegion = MLSubImgBoxFromITKRegion<IN_IMAGE_TYPE>(inReg);
00184     return retRegion;
00185     }       
00186   catch( itk::ExceptionObject & e )          
00187    {
00188      postITKException(e, &module, ML_ERROR);
00189      return SubImageBox(); /* Return empty box in case of error. */  
00190    }        
00191 }         
00192 
00193 
00194 //----------------------------------------------------------------------------
00196 //----------------------------------------------------------------------------
00197 inline void testFunc_determineImageFilterOutImageRegionT0() {}
00198 
00199 //----------------------------------------------------------------------------
00202 //----------------------------------------------------------------------------
00203 template <typename FILTER_TYPE, typename OUT_IMAGE_TYPE>
00204 SubImageBox determineImageFilterOutImageRegionT0(typename FILTER_TYPE::Pointer &filterPointer,
00205                                                  Module &module)
00206 {   
00207   ML_TRACE_IN("template determineImageFilterOutImageRegionT0()");
00208 
00209   // Test and debug function call.
00210   testFunc_determineImageFilterOutImageRegionT0();
00211 
00212   if (!filterPointer){ return SubImageBox(); } 
00213     
00214   /* Define a dummy region of one voxel. */                                   
00215   typename OUT_IMAGE_TYPE::RegionType region =                                  
00216     ITKRegionFromMLSubImgBox<OUT_IMAGE_TYPE>(SubImageBox(ImageVector(0), ImageVector(0)));  
00217     
00218   try{ 
00219     /* Propagate a dummy region through filter to update output infos */      
00220     /* correctly. It seems not to work for all filters without setting */     
00221     /* and propagating such a dummy region. */                                
00222     filterPointer->GetOutput()->SetRequestedRegion(region);     
00223     filterPointer->GetOutput()->UpdateOutputInformation();      
00224     filterPointer->GetOutput()->PropagateRequestedRegion();     
00225     
00226     /* Now filter regions are updated, get the largest possible region. */    
00227     typename OUT_IMAGE_TYPE::RegionType outRegion =                             
00228       filterPointer->GetOutput()->GetLargestPossibleRegion();   
00229     
00230     /* Return output image extent defined by largest possible region. */      
00231     SubImageBox retRegion = MLSubImgBoxFromITKRegion<OUT_IMAGE_TYPE>(outRegion);
00232     return retRegion;
00233   }
00234   catch( itk::ExceptionObject & e )                                           
00235   { 
00236     postITKException(e, &module, ML_ERROR, "Setting empty output image region"); 
00237     return SubImageBox(); /* Return empty box in case of error. */              
00238   }
00239 }  
00240 
00241 //----------------------------------------------------------------------------
00243 //----------------------------------------------------------------------------
00244 inline void testFunc_determineImageFilterOutImageRegionTN() {}
00245 
00246 //----------------------------------------------------------------------------
00249 //----------------------------------------------------------------------------
00250 template <typename FILTER_TYPE, typename OUT_IMAGE_TYPE, typename IN_IMAGE_TYPE>
00251 SubImageBox determineImageFilterOutImageRegionTN(typename FILTER_TYPE::Pointer &filterPointer,
00252                                                  Module &module,
00253                                                  int numImageInputs)                             
00254 {   
00255   ML_TRACE_IN("template determineImageFilterOutImageRegionTN()");
00256 
00257   // Test and debug function call.
00258   testFunc_determineImageFilterOutImageRegionTN();
00259 
00260   if (!filterPointer){ return SubImageBox(); } 
00261 
00262   /* Define a dummy region of one voxel. */                                   
00263   typename OUT_IMAGE_TYPE::RegionType region =                                  
00264     ITKRegionFromMLSubImgBox<OUT_IMAGE_TYPE>(SubImageBox(ImageVector(0), ImageVector(0)));  
00265     
00266   /* Create and set a dummy ITK input images. */                              
00267   for (MLint inps=0; inps < numImageInputs; ++inps){
00268  
00269     /* Get input image extents as ITK region object. */                       
00270     SubImageBox inSubImgBox(module.getInputImage(inps)->getImageExtent());  
00271     typename IN_IMAGE_TYPE::RegionType largestPossibleRegion =                  
00272       ITKRegionFromMLSubImgBox<IN_IMAGE_TYPE>(inSubImgBox);
00273 
00274     /* Set a dummy ITK image with extents of input image as input. */         
00275     typename IN_IMAGE_TYPE::Pointer m_Image = IN_IMAGE_TYPE::New();    
00276     try{   
00277       m_Image->SetRegions(largestPossibleRegion); 
00278       filterPointer->SetInput(inps, m_Image);    
00279     }
00280     catch( itk::ExceptionObject & e )                                         
00281     {  
00282       postITKException(e, &module, ML_ERROR, "Setting empty output image region");\
00283       return SubImageBox(); /* Return empty box in case of error. */            
00284     }
00285   }
00286 
00287   try{ 
00288     /* Propagate a dummy region through filter to update output infos */      
00289     /* correctly. It seems not to work for all filters without setting */     
00290     /* and propagating such a dummy region. */                                
00291     filterPointer->GetOutput()->SetRequestedRegion(region);     
00292     filterPointer->GetOutput()->UpdateOutputInformation();      
00293     filterPointer->GetOutput()->PropagateRequestedRegion();     
00294     
00295     /* Now filter regions are updated, get the largest possible region. */    
00296     typename OUT_IMAGE_TYPE::RegionType outRegion =                             
00297       filterPointer->GetOutput()->GetLargestPossibleRegion();   
00298     
00299     /* Return output image extent defined by largest possible region. */
00300     SubImageBox retRegion = MLSubImgBoxFromITKRegion<OUT_IMAGE_TYPE>(outRegion);
00301     return retRegion;
00302   }
00303   catch( itk::ExceptionObject & e )                                           
00304   { 
00305     postITKException(e, &module, ML_ERROR, "Setting empty output image region"); 
00306     return SubImageBox(); /* Return empty box in case of error. */              
00307   }
00308 }  
00309 
00310 
00311 ML_END_NAMESPACE
00312 
00313 #endif
00314