Hi all,<br><br>I tried to filter a 3D image (300x300x512) with the CannyEdgeFilter. I had the problem that I could not allocate the memory for the filtering. Anyway I don't like the CannyEdgeFilter Implementation as one has to guess which values to take for the thresholding before the gradientmagnitude image exists... Therefore I wanted to write my own edge detection routine.<br>
<br>The steps:<br>I generate a image (size: 300x300x512)<br>I generate a vector image (size: 300x300x512, vector dimension:3)<br>a) I split the image, process the imagepart with GradientRecursiveGaussianImageFilter<br>b) I paste the processed imagepart in the vector image with the PasteImageFilter<br>
I repeat a) and b) until the whole image is processed<br><br>Now the first question: Why can't I process the complete image. I have to split the image and get a result... This is the error I get:<br><font size="1"><i>itk::ExceptionObject (0151C728)<br>
Location: "class itk::CovariantVector<float,3> *__thiscall itk::ImportImageConta<br>iner<unsigned long,class itk::CovariantVector<float,3> >::AllocateElements(unsig<br>ned long) const"<br>File: h:\itk\insighttoolkit-3.16.0\code\common\itkImportImageContainer.txx<br>
Line: 188<br>Description: Failed to allocate memory for image.</i></font><br><br>Ok, if I accept that I have to split the image, I have a second question: I was able to run my example programm (code at the end of this mail). Then I copied the code into my actual program. It is a programm with several itk functions and Qt GUI. The only difference is that the image is saved as global variable. I was not able to run the processing. I always had the memory error. Why???<br>
<br>Here now the code for the example processing:<br><br>edgetest.cxx<br><br><font size="1">#include "conio.h"<br><br>#include "itkImageFileWriter.h"<br>#include "itkImageFileReader.h"<br>#include "itkRegionOfInterestImageFilter.h"<br>
#include "itkGradientRecursiveGaussianImageFilter.h"<br>#include "itkGradientToMagnitudeImageFilter.h"<br>#include "itkPasteImageFilter.h"<br>#include "itkImage.h"<br><br>int main(int argc, char* argv[])<br>
{ <br> // Verify number of parameters in command line<br> // if( argc < 3 )<br> // {<br> // std::cerr << "Usage: " << std::endl;<br> // std::cerr << argv[0] << " inputImageFile outputVectorImageFile " << std::endl;<br>
// return EXIT_FAILURE;<br> // }<br> typedef float PixelType;<br> typedef float ComponentType;<br> static const unsigned int Dimension = 3;<br> typedef itk::Image< PixelType, Dimension > ImageType;<br>
typedef itk::CovariantVector< ComponentType, <br> Dimension > OutputPixelType;<br> typedef itk::Image< OutputPixelType, Dimension > OutputImageType;<br><br><br> ImageType::Pointer image = ImageType::New();<br>
<br> ImageType::IndexType start;<br> for(int i = 0; i < Dimension; i++)<br> start[i]=0;<br><br> ImageType::SizeType size;<br> size[0] = 300;<br> size[1] = 300;<br> size[2] = 512;<br> <br> ImageType::RegionType region;<br>
region.SetSize(size);<br> region.SetIndex(start);<br> <br> ImageType::SpacingType spacing;<br> spacing[0] = 20;<br> spacing[1] = 20;<br> spacing[2] = 4.00493;<br> <br> image->SetRegions(region);<br> image->SetSpacing(spacing);<br>
image->Allocate();<br><br> std::cout << region << std::endl;<br><br> OutputImageType::Pointer vec_image = OutputImageType::New();<br> OutputImageType::RegionType vecregion;<br> <br> OutputImageType::SizeType vecsize; //Size<br>
vecsize[0] = (image->GetLargestPossibleRegion().GetSize())[0];<br> vecsize[1] = (image->GetLargestPossibleRegion().GetSize())[1];<br> vecsize[2] = (image->GetLargestPossibleRegion().GetSize())[2];<br> std::cout<<"size0: "<< vecsize[0]<< " size1: "<< vecsize[1] << " size2: " << vecsize[2] <<std::endl;<br>
vecregion.SetSize( vecsize );<br><br> OutputImageType::IndexType vecstart; //Start<br> vecstart[0] = (image->GetOrigin())[0];<br> vecstart[1] = (image->GetOrigin())[1];<br> vecstart[2] = (image->GetOrigin())[2];<br>
std::cout<<" start0: "<< vecstart[0]<< " start1: "<< vecstart[1] << " start2: " << vecstart[2] <<std::endl;<br> vecregion.SetIndex( vecstart );<br>
<br> vec_image->SetRegions(vecregion);<br> vec_image->SetSpacing(image->GetSpacing());<br> vec_image->Allocate();<br><br> // The image buffer is initialized to a particular value<br> OutputImageType::PixelType initialValue;<br>
<br> // A vector can initialize all its components to the<br> // same value by using the Fill() method.<br> initialValue.Fill( 0.0 );<br><br> // Now the image buffer can be initialized with this<br> // vector value.<br>
vec_image->FillBuffer( initialValue );<br> std::cout<< "Allocate" << std::endl;<br><br> typedef itk::RegionOfInterestImageFilter< ImageType, ImageType > ROIFilterType;<br> ROIFilterType::Pointer roifilter = ROIFilterType::New();<br>
<br> // Number of Regions<br> int splitcnt_x = 2;<br> int splitcnt_y = 2;<br> int overlap = 15;<br><br> int stepcnt_x = (int) (size[0]*1.0/splitcnt_x + 0.5);<br> int stepcnt_y = (int) (size[1]*1.0/splitcnt_y + 0.5);<br>
<br><br> ImageType::IndexType roistart;<br> roistart[2]=0;<br> ImageType::SizeType roisize;<br> roisize[2]=512;<br> ImageType::RegionType roiregion;<br><br><br> for (int cnt_x = 0; cnt_x < splitcnt_x; cnt_x++)<br>
{<br> for (int cnt_y = 0; cnt_y < splitcnt_y; cnt_y++)<br> {<br> roistart[0]= cnt_x*stepcnt_x - overlap;<br> roistart[1]= cnt_y*stepcnt_y - overlap;<br> if(cnt_x == 0)<br> {<br> roistart[0] = 0;<br>
roisize[0] = stepcnt_x + overlap;<br> }<br> else<br> {<br> roisize[0] = stepcnt_x + 2*overlap;<br> }<br> if(roisize[0]+roistart[0] > size[0])<br> {<br> roisize[0] = size[0]-roistart[0];<br>
}<br> if(cnt_y == 0)<br> {<br> roistart[1] = 0;<br> roisize[1] = stepcnt_y + overlap;<br> }<br> else<br> {<br> roisize[1] = stepcnt_y + 2*overlap;<br>
}<br> if(roisize[1]+roistart[1] > size[1])<br> {<br> roisize[1] = size[1]-roistart[1];<br> }<br><br> <br> roiregion.SetSize(roisize);<br> roiregion.SetIndex(roistart);<br>
<br> std::cout << "cnt_x: " << cnt_x << " cnt_y: " << cnt_y << std::endl;<br><br> std::cout << roiregion << std::endl;<br><br> std::cout << "ROI region inside image region is " << region.IsInside(roiregion) << std::endl;<br>
<br> roifilter->SetRegionOfInterest(roiregion);<br> roifilter->SetInput(image);<br><br> //Filter class is instantiated<br> typedef itk::GradientRecursiveGaussianImageFilter<ImageType, OutputImageType> GradFilterType;<br>
<br> GradFilterType::Pointer gradfilter = GradFilterType::New();<br> <br> //sigma is specified in millimeters<br> gradfilter->SetSigma( 1.5 ); <br> <br> // processing pipeline:<br>
gradfilter->SetInput(roifilter->GetOutput());<br> <br><br> typedef itk::PasteImageFilter <OutputImageType, OutputImageType > PasteImageFilterType;<br> // The SetDestinationIndex() method prescribes where in the first<br>
// input to start pasting data from the second input.<br> // The SetSourceRegion method prescribes the section of the second<br> // image to paste into the first.<br> <br> OutputImageType::IndexType destinationIndex;<br>
destinationIndex[0] = cnt_x*stepcnt_x;<br> destinationIndex[1] = cnt_y*stepcnt_y;<br> destinationIndex[2] = 0;<br> <br> PasteImageFilterType::Pointer pasteFilter = PasteImageFilterType::New ();<br>
pasteFilter->SetSourceImage(gradfilter->GetOutput());<br> pasteFilter->SetDestinationImage(vec_image);<br> <br> OutputImageType::RegionType pasteregion;<br> OutputImageType::IndexType pastestart;<br>
pastestart[2]=0;<br> OutputImageType::SizeType pastesize;<br> pastesize[2]=512;<br><br> pastestart[0]= overlap;<br> pastestart[1]= overlap;<br> pastesize[0] = stepcnt_x;<br> pastesize[1] = stepcnt_y;<br>
<br> if(cnt_x == 0)<br> {<br> pastestart[0] = 0;<br> }<br> if(cnt_y == 0)<br> {<br> pastestart[1] = 0;<br> }<br><br> pasteregion.SetIndex(pastestart);<br>
pasteregion.SetSize(pastesize);<br><br> pasteFilter->SetSourceRegion(pasteregion);<br> pasteFilter->SetDestinationIndex(destinationIndex);<br><br> try<br> {<br> pasteFilter->Update();<br>
}<br> catch( itk::ExceptionObject & err )<br> {<br> std::cerr << "ExceptionObject caught !" << std::endl;<br> std::cerr << err << std::endl;<br>
return EXIT_SUCCESS;<br> }<br> }<br> typedef itk::GradientToMagnitudeImageFilter< OutputImageType, ImageType > MagFilterType;<br> MagFilterType::Pointer magfilter = MagFilterType::New();<br>
<br> magfilter->SetInput(vec_image);<br><br> try<br> {<br> magfilter->Update();<br> image = magfilter->GetOutput();<br> }<br> catch( itk::ExceptionObject & err )<br> {<br> std::cerr << "ExceptionObject caught !" << std::endl;<br>
std::cerr << err << std::endl;<br> return EXIT_SUCCESS;<br> }<br><br> }<br> <br> getch();<br> return EXIT_SUCCESS;<br>}</font><br>