[Insight-users] MultiThreader error in SheppLoganFilter?
Luis Ibanez
luis.ibanez at kitware.com
Fri Dec 11 18:19:39 EST 2009
Hi Ming,
Thanks for posting your code.
The error is that you are not calling
the method that sets the VRadius value.
That's why you get the error message:
"Member variable VRadius
not properly initialized."
You simply need to add:
filter->SetVRadius( 1 );
before you make the call to Update();
...
Of course,
you may want to try values of "VRadius"
different from 1.
BTW: doing
"using namespace std;"
is a really bad practice.
Opening a namespace defeats the purpose
of having namespaces at all.
Please get used to typing:
std::string
std::cout
std::cerr
std::endl
and so on....
Also, please note that the implementation of
templated classes shouldn't be put in .cpp files.
We usually put this in .txx files.
Please find attached the three files fixed
along with a CMakeLists.txt file.
BTW: your code was missing several
essential lines of code, such as includes
for ImageFileReader, ImageFileWriter and
the ShepLogan filter itself.
We strongly encourage you to share this
filter with the ITK community by submitting
it to the Insight Journal :-)
http://www.insight-journal.org/
Regards,
Luis
-------------------------------
On Tue, Dec 8, 2009 at 6:19 PM, Ming Chao <mingchao2005 at gmail.com> wrote:
> Hi,
> I got a code of SheppLoganFilter and used it to filter a raw sinogram image
> (I am not sure if this is a correct use though). However, I got the
> following error:
> writing image image/test2.png caused a problem.
> itk::ExceptionObject (0122FEC4)
> Location: "void __thiscall itk::MultiThreader::SingleMethodExecute(void)"
> File: \ITK\InsightToolkit-3.8.0\Code\Common\itkMultiThreader.cxx
> Line: 429
> Description: itk::ERROR: MultiThreader(01870068): Exception occurred during
> Sing
> leMethodExecute
> c:\my rsch work\image reconstruction\code\SheppLoganFilter.cpp:27:
> itk::ERROR: SheppLoganFilter(0177DD28): Member variable VRadius not properly
> ini
> tialized.
>
> The codes I used are attached in the following. Any
> comments/suggestions/help would be greatly appreciated.
>
> The main function:
> #include "itkImage.h"
> using namespace std;
> int main(int argc, char* argv[])
> {
> string filename("image/test.png");
> string filename2("image/test2.png");
> typedef unsigned char uchar;
> typedef uchar PixelType;
> const int Dimension = 2;
> typedef itk::Image< PixelType, 2 > ImageType;
>
> //reading image from disk
> typedef itk::ImageFileReader< ImageType > ReaderType;
> ReaderType::Pointer reader = ReaderType::New();
> reader->SetFileName(filename);
> reader->Update();
> ImageType::Pointer input_image = ImageType::New();
> input_image = reader->GetOutput();
> typedef SheppLoganFilter<ImageType, ImageType> FilterType;
> FilterType::Pointer filter = FilterType::New();
> filter->SetInput(input_image);
> typedef itk::ImageFileWriter< ImageType > WriterType;
> WriterType::Pointer writer = WriterType::New();
> writer->SetInput(filter->GetOutput());
> writer->SetFileName(filename2);
> try {
> writer->Update();
> }
> catch( itk::ExceptionObject exp )
> {
> cerr << "writing image " << filename2 << " caused a problem." << endl;
> cerr << exp << endl;
> return 1;
> }
> }
> The SheppLoganFilter.h:
>
> #ifndef __SheppLoganFilter_h
> #define __SheppLoganFilter_h
> #include <vector>
>
> //ITK includes
> #include "itkImageToImageFilter.h"
> #include "itkImageRegionIterator.h"
> #include "itkConstNeighborhoodIterator.h"
> #include "itkOffset.h"
> /*!
> * \class SheppLoganFilter
> * \brief Applies a shepp logan filter to an image and returns the filtered
> image.
> */
> template < typename TInputImage, typename TOutputImage >
> class SheppLoganFilter : public itk::ImageToImageFilter< TInputImage,
> TOutputImage >
> {
> public:
> /*! Standard class typedefs. */
> typedef SheppLoganFilter Self;
> typedef itk::ImageToImageFilter< TInputImage, TOutputImage > Superclass;
> typedef itk::SmartPointer< Self > Pointer;
> typedef itk::SmartPointer< const Self > ConstPointer;
> /*! Method for creation through object factory */
> itkNewMacro(Self);
> /*! Run-time type information (and related methods) */
> itkTypeMacro(SheppLoganFilter, ImageToImageFilter);
> //Convenience typedefs
> typedef TInputImage ImageType;
> typedef TOutputImage OImageType;
> typedef typename ImageType::RegionType RegionType;
> typedef itk::ConstNeighborhoodIterator< ImageType >
> NeighborhoodIteratorType;
> typedef itk::ImageRegionIterator< OImageType> IteratorType;
> typedef typename ImageType::ConstPointer ImageConstPointer;
> typedef typename OImageType::Pointer ImagePointer;
> /*! Extract dimension from input and output image. */
> itkStaticConstMacro(ImageDimension, unsigned int,
> TInputImage::ImageDimension);
> itkSetMacro(VRadius, int);
> itkGetConstReferenceMacro(VRadius, int);
> protected:
> SheppLoganFilter();
> virtual ~SheppLoganFilter() {}
> /*! Standard "PrintSelf" method */
> void PrintSelf(std::ostream& os, itk::Indent indent) const;
> /*!
> * SheppLoganFilter can be implemented as a
> * multithreaded filter. Therefore, this implementation provides
> * a ThreadedGenerateData() routine which is called for each
> * processing thread. The output image data is allocated
> * automatically by the superclass prior to calling
> * ThreadedGenerateData(). ThreadedGenerateData can only
> * write to the portion of the output image specified by the
> * parameter "outputRegionForThread".
> *
> * \sa ImageToImageFilter::ThreadedGenerateData(),
> * ImageToImageFilter::GenerateData()
> */
> void ThreadedGenerateData(const RegionType& regionForThread,
>
> int threadId );
> private:
> SheppLoganFilter(const Self&) {}; //purposely not implemented
> void operator=(const Self&); //purposely not implemented
> /*! vertical radius of shepp logan filter */
> int m_VRadius;
> };
>
> #ifndef ITK_MANUAL_INSTANTIATION
> #include "SheppLoganFilter.cpp"
> #endif
> #endif
> And the SheppLoganFilter.cpp:
> #ifndef _SheppLoganFilter_txx
> #define _SheppLoganFilter_txx
> #include "SheppLoganFilter.h"
> #ifndef PI
> #define PI 3.141592653
> #endif
> template < typename TInputImage, typename TOutputImage >
> SheppLoganFilter< TInputImage, TOutputImage >::SheppLoganFilter()
> {
> m_VRadius = 0;
> }
> template < typename TInputImage, typename TOutputImage >
> void SheppLoganFilter< TInputImage, TOutputImage >
> ::ThreadedGenerateData(const RegionType&
> regionForThread, int threadId )
> {
> if(m_VRadius <= 0)
> itkExceptionMacro(<< "Member variable VRadius not properly initialized.");
> //pointers to output and input image
> ImageConstPointer inputImage = this->GetInput();
> ImagePointer outputImage = this->GetOutput();
> RegionType inputRegion = inputImage->GetLargestPossibleRegion();
> RegionType outputRegion = outputImage->GetLargestPossibleRegion();
> IteratorType out(outputImage, outputRegion);
> NeighborhoodIteratorType::RadiusType radius;
> radius.Fill(0);
> radius[1] = m_VRadius;
> int vDim = m_VRadius*2+1;
> NeighborhoodIteratorType it(radius, inputImage, inputRegion);
> //pointer to Shepp-Logan filter weights
> float *sl = new float[vDim];
> //vector with the OffsetTypes for the neighborhood iterator
> std::vector< NeighborhoodIteratorType::OffsetType > offset;
> int actIndex;
> for(int i = -m_VRadius; i <= m_VRadius; ++i)
> {
> actIndex = i + m_VRadius;
> *(sl+actIndex) = -2.0/(PI*PI*(4*i*i-1));
> NeighborhoodIteratorType::OffsetType tmpO = {{0,i}};
> offset.push_back(tmpO);
> }
> std::vector< int > tmpImg;
> for (it.GoToBegin(), out.GoToBegin(); !it.IsAtEnd(); ++it, ++out)
> {
> float sum = 0;
> for(int i = 0; i < m_VRadius*2+1; ++i)
> {
> sum += *(sl+i) * it.GetPixel(offset[i]);
> }
> tmpImg.push_back(sum);
> }
> int i = 0;
> for (it.GoToBegin(), out.GoToBegin(); !it.IsAtEnd(); ++it, ++out)
> {
> out.Set(tmpImg[i++]);
> }
> }
>
> template < typename TInputImage, typename TOutputImage>
> void SheppLoganFilter< TInputImage, TOutputImage >::PrintSelf(std::ostream&
> os, itk::Indent indent) const
> {
> Superclass::PrintSelf(os,indent);
> os << "VRadius = " << m_VRadius << std::endl;
> }
>
> #endif
>
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Source.tgz
Type: application/x-gzip
Size: 2553 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20091211/5f31cd5f/attachment.bin>
More information about the Insight-users
mailing list