[Insight-users] Urgenthelp with Observer class to store image sequence
Jayant Chauhan
gripened@hotmail.com
Thu May 13 22:17:54 EDT 2004
<html><div style='background-color:'><DIV class=RTE>Hey Fellas,</DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE>Seems I am a lil bit confused about the sequential flow of control in ITK.</DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE>When I include all my image reading and filter executions in the main(), it works, but when I create a function which does all this as a member of my class, then I am not able to get the Observer to work. My code is as follows, I am using the GeodesicActiveContourLevelSetImageFilter to segment my image, and want to store all the contours, ie, the zerocrossing, so I add the observer to my zerocrossing filter,ie, m_contour.</DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE>ALso it seems that only when I add my geodesicfilter to the observer are the contours stored and not when I add m_contour. ie.</DIV>
<DIV class=RTE> m_observer->SetCallbackFunction( this, <BR> & firstFilter::GetContourAfterIteration );<BR> <STRONG>m_geodesic</STRONG>->AddObserver(itk::IterationEvent(), m_observer); //works</DIV>
<DIV class=RTE> <STRONG>m_contour</STRONG>->AddObserver(itk::IterationEvent(), m_observer); //doesnt work</DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE> my code:</DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE><STRONG><U>firstFilter.h</U></STRONG></DIV>
<DIV class=RTE><STRONG><U></U></STRONG> </DIV>
<DIV class=RTE>#ifndef _FIRST_FILTER_H<BR>#define _FIRST_FILTER_H</DIV>
<DIV class=RTE>#include "itkImage.h"<BR>#include "itkImageFileReader.h"<BR>#include "itkImageFileWriter.h"<BR>#include "itkGeodesicActiveContourLevelSetImageFilter.h"<BR>#include "itkCurvatureAnisotropicDiffusionImageFilter.h"<BR>#include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"<BR>#include "itkSigmoidImageFilter.h"<BR>#include "itkBinaryThresholdImageFilter.h"<BR>#include "itkGeodesicActiveContourLevelSetFunction.h"<BR>#include "itkSegmentationLevelSetImageFilter.h"<BR>#include "itkCastImageFilter.h"<BR>#include "itkRescaleIntensityImageFilter.h"<BR>#include "itkZeroCrossingImageFilter.h"<BR>#include "itkCommand.h"<BR>#include "itkWeightedAddImageFilter.h"</DIV>
<DIV class=RTE><BR>template <class TInputImage, class TInternalImage, class TOutputImage><BR>class firstFilter{<BR> <BR> public:</DIV>
<DIV class=RTE> typedef itk::CurvatureAnisotropicDiffusionImageFilter<TInputImage, <BR> TInternalImage> SmoothingFilterType;</DIV>
<DIV class=RTE> typedef itk::GradientMagnitudeRecursiveGaussianImageFilter<TInternalImage,<BR> TInternalImage> GradientFilterType;</DIV>
<DIV class=RTE> typedef itk::SigmoidImageFilter<TInternalImage, <BR> TInternalImage> SigmoidFilterType;</DIV>
<DIV class=RTE> typedef itk::GeodesicActiveContourLevelSetImageFilter<TInternalImage,<BR> TInternalImage> GACFilterType;<BR> <BR> typedef itk::BinaryThresholdImageFilter<TInternalImage,<BR> TOutputImage> TFilterType;<BR> <BR> typedef itk::ZeroCrossingImageFilter<TOutputImage, <BR> TInternalImage> ZeroFilterType;<BR> <BR> typedef itk::WeightedAddImageFilter<TInternalImage, <BR> TInternalImage, TInternalImage> WAddFilterType;<BR> <BR> typedef itk::SimpleMemberCommand< firstFilter > ObserverType;</DIV>
<DIV class=RTE><BR> SmoothingFilterType::Pointer m_smoothing;<BR> GradientFilterType::Pointer m_gradient;<BR> SigmoidFilterType::Pointer m_sigmoid;</DIV>
<DIV class=RTE> SmoothingFilterType::Pointer m_framesmoothing;<BR> GradientFilterType::Pointer m_framegradient;<BR> SigmoidFilterType::Pointer m_framesigmoid;</DIV>
<DIV class=RTE> GACFilterType::Pointer m_geodesic;<BR> TFilterType::Pointer m_threshold;<BR> ZeroFilterType::Pointer m_contour;<BR> WAddFilterType::Pointer m_wadd;<BR> ObserverType::Pointer m_observer;<BR> <BR> firstFilter();<BR> void ExecuteGAC(char* , char* , char* , char* , char* , char* , char*, <BR> int , float , float , float ,<BR> float , float , float );</DIV>
<DIV class=RTE> void GetContourAfterIteration();<BR>};</DIV>
<DIV class=RTE>template <class TInputImage, class TInternalImage, class TOutputImage><BR>void firstFilter<TInputImage, TInternalImage, TOutputImage><BR>::ExecuteGAC(char* featureimage, char* frameimage, char* initlevelset, <BR> char* sigmoidresult, char* framesigmoidresult, char* gacresult, char* threshresult,<BR> int no_iter, float sigma, float alpha, float beta,<BR> float ascale, float cscale, float pscale){</DIV>
<DIV class=RTE> typedef itk::ImageFileReader<TInputImage> InputImageReader;<BR> typedef itk::ImageFileReader<TInternalImage> InternalImageReader;<BR> <BR> typedef itk::ImageFileWriter<TInternalImage> InternalImageWriter;<BR> typedef itk::ImageFileWriter<TOutputImage> OutputImageWriter;</DIV>
<DIV class=RTE> std::cout<<"I am inside the function "<<std::endl;</DIV>
<DIV class=RTE> InputImageReader::Pointer reader1 = InputImageReader::New();<BR> InputImageReader::Pointer reader2 = InputImageReader::New();<BR> <BR> InternalImageReader::Pointer internalreader1 = InternalImageReader::New();<BR> InternalImageWriter::Pointer internalwriter1 = InternalImageWriter::New();<BR> <BR> OutputImageWriter::Pointer sigmoidwriter = OutputImageWriter::New();<BR> OutputImageWriter::Pointer framesigmoidwriter = OutputImageWriter::New();<BR> OutputImageWriter::Pointer gacwriter = OutputImageWriter::New();<BR> OutputImageWriter::Pointer twriter = OutputImageWriter::New();</DIV>
<DIV class=RTE> <BR> reader1->SetFileName(featureimage);<BR> reader2->SetFileName(frameimage);<BR> internalreader1->SetFileName(initlevelset);</DIV>
<DIV class=RTE> sigmoidwriter->SetFileName(sigmoidresult);<BR> framesigmoidwriter->SetFileName(framesigmoidresult);<BR> <BR> m_smoothing->SetInput(reader1->GetOutput());<BR> m_framesmoothing->SetInput(reader2->GetOutput());</DIV>
<DIV class=RTE> m_gradient->SetSigma(sigma);<BR> m_gradient->SetInput(m_smoothing->GetOutput());</DIV>
<DIV class=RTE> m_framegradient->SetSigma(sigma);<BR> m_framegradient->SetInput(m_framesmoothing->GetOutput());</DIV>
<DIV class=RTE> m_sigmoid->SetInput(m_gradient->GetOutput());<BR> m_sigmoid->SetAlpha( alpha );<BR> m_sigmoid->SetBeta( beta );</DIV>
<DIV class=RTE> m_framesigmoid->SetInput(m_framegradient->GetOutput());<BR> m_framesigmoid->SetAlpha( alpha );<BR> m_framesigmoid->SetBeta( alpha );</DIV>
<DIV class=RTE> m_wadd->SetInput1(m_sigmoid->GetOutput());<BR> m_wadd->SetInput2(m_framesigmoid->GetOutput());</DIV>
<DIV class=RTE> m_geodesic->SetPropagationScaling( pscale );<BR> m_geodesic->SetCurvatureScaling( cscale );<BR> m_geodesic->SetAdvectionScaling( ascale );<BR> <BR> m_geodesic->SetMaximumRMSError(0.02);<BR> m_geodesic->SetNumberOfIterations( no_iter );<BR> m_geodesic->SetReverseExpansionDirection(false);</DIV>
<DIV class=RTE> m_geodesic->SetInput(internalreader1->GetOutput());<BR> m_geodesic->SetFeatureImage(m_wadd->GetOutput());<BR> gacwriter->SetFileName(gacresult);<BR> <BR> twriter->SetFileName(threshresult);<BR> m_threshold->SetInput(m_geodesic->GetOutput());</DIV>
<DIV class=RTE> typedef itk::RescaleIntensityImageFilter<TInternalImage,<BR> TOutputImage> CastFilterType;</DIV>
<DIV class=RTE> CastFilterType::Pointer sigmoidcastfilter = CastFilterType::New();<BR> CastFilterType::Pointer gaccastfilter = CastFilterType::New();<BR> <BR> sigmoidcastfilter->SetInput(m_sigmoid->GetOutput());<BR> gaccastfilter->SetInput(m_geodesic->GetOutput());<BR> <BR> sigmoidcastfilter->SetOutputMinimum(0);<BR> sigmoidcastfilter->SetOutputMaximum(255);<BR> <BR> gaccastfilter->SetOutputMinimum(0);<BR> gaccastfilter->SetOutputMaximum(255);</DIV>
<DIV class=RTE> sigmoidwriter->SetInput(sigmoidcastfilter->GetOutput());<BR> gacwriter->SetInput(gaccastfilter->GetOutput());<BR> twriter->SetInput(m_threshold->GetOutput());</DIV>
<DIV class=RTE> CastFilterType::Pointer zerocastfilter = CastFilterType::New();</DIV>
<DIV class=RTE> zerocastfilter->SetOutputMinimum(0);<BR> zerocastfilter->SetOutputMaximum(255);</DIV>
<DIV class=RTE> OutputImageWriter::Pointer zcwriter = OutputImageWriter::New();</DIV>
<DIV class=RTE> m_contour->SetInput(m_threshold->GetOutput());<BR> zerocastfilter->SetInput(m_contour->GetOutput());</DIV>
<DIV class=RTE> zcwriter->SetFileName("zerocrossing.png");<BR> zcwriter->SetInput(zerocastfilter->GetOutput());<BR> <BR> try{<BR> gacwriter->Update();<BR> twriter->Update();<BR> sigmoidwriter->Update();<BR> zcwriter->Update();<BR> }<BR> catch (itk::ExceptionObject & err)<BR> {<BR> std::cerr<<"Exception caught "<<std::endl;<BR> std::cerr<<err<<std::endl;<BR> }</DIV>
<DIV class=RTE><BR>}</DIV>
<DIV class=RTE><BR>template <class TInputImage, class TInternalImage, class TOutputImage><BR>void firstFilter<TInputImage, TInternalImage, TOutputImage><BR>::GetContourAfterIteration(){<BR> <BR> static unsigned int iterationCounter = 0;<BR> std::cout<<"Hey do I get a change "<<std::endl;<BR> typedef itk::ImageFileWriter<TOutputImage> ImageWriter;<BR> typedef itk::RescaleIntensityImageFilter<TInternalImage,<BR> TOutputImage> CastFilterType;</DIV>
<DIV class=RTE> ImageWriter::Pointer contour_writer = ImageWriter::New();<BR> CastFilterType::Pointer zccast = CastFilterType::New();<BR> zccast->SetOutputMinimum(0);<BR> zccast->SetOutputMaximum(255);</DIV>
<DIV class=RTE><BR> char* contourfile;<BR> contourfile = (char*)malloc(sizeof(char)*50);<BR> <BR> if ( (iterationCounter != 0) && (iterationCounter%25 == 0) ){<BR> </DIV>
<DIV class=RTE> sprintf(contourfile,"%d%s",iterationCounter,".png");<BR> std::cout << "No. elpased iterations: " << m_geodesic->GetElapsedIterations() << std::endl;<BR> std::cout << "RMS change: " << m_geodesic->GetRMSChange() << std::endl;<BR> contour_writer->SetFileName(contourfile);<BR> zccast->SetInput(m_contour->GetOutput());<BR> contour_writer->SetInput(zccast->GetOutput());</DIV>
<DIV class=RTE> try{</DIV>
<DIV class=RTE> contour_writer->Update();<BR> }<BR> catch (itk::ExceptionObject & err){<BR> std::cerr<<"ITK Exception caught :"<<err<<std::endl;<BR> }<BR> <BR> }<BR> else{<BR> std::cout<<"Iteration counter :"<<iterationCounter<<std::endl;<BR> }<BR> iterationCounter++;<BR>}</DIV>
<DIV class=RTE><BR>template <class TInputImage, class TInternalImage, class TOutputImage><BR>firstFilter<TInputImage, TInternalImage, TOutputImage><BR>::firstFilter(){</DIV>
<DIV class=RTE> m_smoothing = SmoothingFilterType::New();<BR> m_framesmoothing = SmoothingFilterType::New();</DIV>
<DIV class=RTE> m_smoothing->SetTimeStep(0.125);<BR> m_smoothing->SetNumberOfIterations(5);<BR> m_smoothing->SetConductanceParameter(3.0);</DIV>
<DIV class=RTE> m_framesmoothing->SetTimeStep(0.125);<BR> m_framesmoothing->SetNumberOfIterations(5);<BR> m_framesmoothing->SetConductanceParameter(3.0);</DIV>
<DIV class=RTE> m_gradient = GradientFilterType::New();<BR> m_framegradient = GradientFilterType::New();</DIV>
<DIV class=RTE> m_sigmoid = SigmoidFilterType::New();<BR> m_framesigmoid = SigmoidFilterType::New();</DIV>
<DIV class=RTE> m_sigmoid->SetOutputMinimum(0.0);<BR> m_sigmoid->SetOutputMaximum(1.0);</DIV>
<DIV class=RTE> m_framesigmoid->SetOutputMinimum(0.0);<BR> m_framesigmoid->SetOutputMaximum(1.0);</DIV>
<DIV class=RTE> m_wadd = WAddFilterType::New();<BR> m_wadd->SetAlpha(0.5);<BR> <BR> m_geodesic = GACFilterType::New();<BR> m_geodesic->SetIsoSurfaceValue(127.0);</DIV>
<DIV class=RTE> <BR> m_threshold = TFilterType::New();<BR> <BR> m_threshold->SetLowerThreshold(-1000.0);<BR> m_threshold->SetUpperThreshold(0.0);<BR> <BR> m_threshold->SetOutsideValue(0);<BR> m_threshold->SetInsideValue(255);</DIV>
<DIV class=RTE> m_contour = ZeroFilterType::New();</DIV>
<DIV class=RTE> m_observer = ObserverType::New();</DIV>
<DIV class=RTE> m_observer->SetCallbackFunction( this, <BR> & firstFilter::GetContourAfterIteration );<BR> m_geodesic->AddObserver(itk::IterationEvent(), m_observer);</DIV>
<DIV class=RTE>}</DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE>#endif</DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE>--------------------------------------------------------------------------------------------------------------</DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE><STRONG><U>firstFilter.cxx</U></STRONG></DIV>
<DIV class=RTE>#include "firstFilter.h"</DIV>
<DIV class=RTE>int main(int argc, char** argv){</DIV>
<DIV class=RTE> typedef unsigned char InputPixelType;<BR> typedef float InternalPixelType;<BR> typedef unsigned char OutputPixelType;</DIV>
<DIV class=RTE> typedef itk::Image<InputPixelType, 2> InputImageType;<BR> typedef itk::Image<InternalPixelType, 2> InternalImageType;<BR> typedef itk::Image<OutputPixelType, 2> OutputImageType;</DIV>
<DIV class=RTE> typedef itk::ImageFileReader<InputImageType> InputImageReader;<BR> typedef itk::ImageFileReader<InternalImageType> InternalImageReader;<BR> <BR> typedef itk::ImageFileWriter<InternalImageType> InternalImageWriter;<BR> typedef itk::ImageFileWriter<OutputImageType> OutputImageWriter;</DIV>
<DIV class=RTE> if (argc < 15){</DIV>
<DIV class=RTE> std::cerr<<"Incorrect usage, Correct usage being : "<<std::endl;<BR> std::cerr<<"firstFilter.exe FeatureImage FrameImage InitialLevelSetImage ";<BR> std::cerr<<"sigmoidresult framesigmoidresult geodesicresult thresholdresult ";<BR> std::cerr<<"NoOfIterations sigma alpha beta pscale cscale ascale ";<BR> std::cerr<<"--------------"<<std::endl;<BR> exit(1);<BR> <BR> }</DIV>
<DIV class=RTE> firstFilter<InputImageType, InternalImageType, OutputImageType> ff;</DIV>
<DIV class=RTE> ff.ExecuteGAC(argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], atoi(argv[8]), <BR> atof(argv[9]), atof(argv[10]), atof(argv[11]), atof(argv[12]), atof(argv[13]),<BR> atof(argv[14])) ;</DIV>
<DIV class=RTE><BR> return 1;</DIV>
<DIV class=RTE>}<BR>----------------------------------------------------------------------------------------------------------</DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE>Any suggestions please, have to finish this asap</DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE>with regards</DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE>Jayant</DIV></div><br clear=all><hr>Send flowers in 24 hours! <a href="http://g.msn.com/8HMAENIN/2743??PS=47575">At MSN Shopping.</a> </html>
More information about the Insight-users
mailing list