.GetPointer works. But the code don’t seem to be working, I turned off the advection image for tests. I Defined the vector images above in the code.
with this code I get malloc()
using ROIFilterType_Heart_GVF = itk::RegionOfInterestImageFilter<ImageVectorType_Heart, ImageVectorType_Heart>;
auto ROIFilter_Heart_GVF = ROIFilterType_Heart_GVF::New();
ROIFilter_Heart_GVF->SetInput(cast_gvf->GetOutput());
ROIFilter_Heart_GVF->SetRegionOfInterest(ROI_for_Heart_GVF);
ROIFilter_Heart_GVF->Update();
///
// GEODESIC ACTIVE CONTOUR HEART
seedValue = -seedValue;
// READ ROI IMAGE
auto Reader_Heart_ROI_4 = ReaderType_Heart_Internal::New();
Reader_Heart_ROI_4->SetFileName("ROI Heart.tiff");
Reader_Heart_ROI_4->Update();
using InternalImageType_2D = itk::Image<float, 2>;
InternalImageType::Pointer Heart_ROI_Image = Reader_Heart_ROI_4->GetOutput();
Heart_ROI_Image->SetSpacing(spacing_internal);
Heart_ROI_Image->SetOrigin(origin_internal);
Heart_ROI_Image->SetDirection(direction_internal);
//////////////////
auto final_image_heart = Reader_Heart_ROI_4->GetOutput();
final_image_heart->FillBuffer(0.0);
final_image_heart->SetSpacing(spacing_internal);
final_image_heart->SetOrigin(origin_internal);
final_image_heart->SetDirection(direction_internal);
Reader_Heart_ROI_4->Update();
InternalImageType::Pointer Heart_ROI_Image_2 = Reader_Heart_ROI_4->GetOutput();
Heart_ROI_Image_2->SetSpacing(spacing_internal);
Heart_ROI_Image_2->SetOrigin(origin_internal);
Heart_ROI_Image_2->SetDirection(direction_internal);
std::cerr << "final_image_heart spacing: " << final_image_heart->GetSpacing()<< std::endl;
////////////
/////////////// AGC LOOP
using ExtractFilterType_Heart = itk::ExtractImageFilter<InternalImageType, InternalImageType_2D>;
// using ExtractFilterType_Heart_2 = itk::ExtractImageFilter<ImageVectorType_Heart, ImageVectorType_Heart_2D>;
using CastingFilterType_Heart_Writer =itk::CastImageFilter<InternalImageType, ImageType>;
using WriterType_Hugo =itk::ImageFileWriter<ImageType>;
using SmoothingFilterType_Heart_Seg =itk::CurvatureAnisotropicDiffusionImageFilter<InternalImageType_2D, InternalImageType_2D>;
using GradientFilterType_Heart_Seg = itk::GradientMagnitudeRecursiveGaussianImageFilter<InternalImageType_2D, InternalImageType_2D>;
using SigmoidFilterType_Heart_Seg = itk::SigmoidImageFilter<InternalImageType_2D, InternalImageType_2D>;
using FastMarchingFilterType_Heart = itk::FastMarchingImageFilter<InternalImageType_2D, InternalImageType_2D>;
using NodeContainer_Heart = FastMarchingFilterType_Heart::NodeContainer;
using NodeType_Heart = FastMarchingFilterType_Heart::NodeType;
using GeodesicActiveContourFilterType_Heart = itk::GeodesicActiveContourLevelSetImageFilter<InternalImageType_2D, InternalImageType_2D>;
using ThresholdingFilterType_Heart = itk::BinaryThresholdImageFilter<InternalImageType_2D, InternalImageType_2D>;
/// DEFINE
InternalImageType_2D::SpacingType spacing_internal_2D;
spacing_internal_2D[0] = spacing_internal[0];
spacing_internal_2D[1] = spacing_internal[1];
InternalImageType_2D::PointType origin_internal_2D;
origin_internal_2D[0] = 0;
origin_internal_2D[1] = 0;
InternalImageType_2D::DirectionType direction_internal_2D;
direction_internal_2D.SetIdentity();
ImageVectorType_Heart_2D::IndexType start_vector_image_heart_2D;
start_vector_image_heart_2D.Fill(0);
ImageVectorType_Heart_2D::SizeType size_vector_image_heart_2D;
size_vector_image_heart[0] = ROIFilter_Heart_GVF->GetOutput()->GetBufferedRegion().GetSize()[0];
size_vector_image_heart[1] = ROIFilter_Heart_GVF->GetOutput()->GetBufferedRegion().GetSize()[1];
using IndexType_gvf = itk::Index<3>;
using IndexType_gvf_2 = itk::Index<2>;
// LOOP
///
for(int slice_to_process = Heart_ROI_Image->GetBufferedRegion().GetIndex()[2]; slice_to_process < Heart_ROI_Image->GetBufferedRegion().GetSize()[2] ; slice_to_process++){
std::cerr << "Slice: " << slice_to_process<< std::endl;
InternalImageType::RegionType Region_heart = Heart_ROI_Image->GetBufferedRegion();
InternalImageType::SizeType size_heart = Region_heart.GetSize();
InternalImageType::IndexType start_heart = Region_heart.GetIndex();
auto extractor_heart = ExtractFilterType_Heart::New();
extractor_heart->SetDirectionCollapseToSubmatrix();
extractor_heart->InPlaceOn();
size_heart[2] = 0;
start_heart[2] = slice_to_process + start_heart[2];
InternalImageType::RegionType desiredRegion_heart;
desiredRegion_heart.SetSize(size_heart);
desiredRegion_heart.SetIndex(start_heart);
extractor_heart->SetExtractionRegion(desiredRegion_heart);
extractor_heart->SetInput(Heart_ROI_Image);
extractor_heart->Update();
std::cerr << "Heart ROI 2D Extracted: " << std::endl;
/// BEGIN FILTERING
auto extracted_image_heart = extractor_heart ->GetOutput();
extracted_image_heart->SetSpacing(spacing_internal_2D);
extracted_image_heart->SetOrigin(origin_internal_2D);
extracted_image_heart->SetDirection(direction_internal_2D);
auto SmoothingFilter_Heart_Seg = SmoothingFilterType_Heart_Seg::New();
SmoothingFilter_Heart_Seg->SetTimeStep(0.09);
SmoothingFilter_Heart_Seg->SetNumberOfIterations(5);
SmoothingFilter_Heart_Seg->SetConductanceParameter(2.0);
SmoothingFilter_Heart_Seg->SetInput(extractor_heart->GetOutput());
// SmoothingFilter_Heart_Seg->Update();
auto gradientMagnitude_Heart_Seg = GradientFilterType_Heart_Seg::New();
gradientMagnitude_Heart_Seg->SetSigma(sigma);
gradientMagnitude_Heart_Seg->SetInput(SmoothingFilter_Heart_Seg->GetOutput());
// gradientMagnitude_Heart_Seg->Update();
auto sigmoid_Heart_Seg = SigmoidFilterType_Heart_Seg::New();
sigmoid_Heart_Seg->SetOutputMinimum(OutputMinimum);
sigmoid_Heart_Seg->SetOutputMaximum(OutputMaximum);
sigmoid_Heart_Seg->SetAlpha(Alpha);
sigmoid_Heart_Seg->SetBeta(Beta);
sigmoid_Heart_Seg->SetInput(gradientMagnitude_Heart_Seg->GetOutput());
// sigmoid_Heart_Seg->Update();
auto fastMarching_Heart = FastMarchingFilterType_Heart::New();
fastMarching_Heart->SetInput(sigmoid_Heart_Seg->GetOutput());
// else fastMarching_Heart->SetInput(gradientMagnitude_Heart_Seg->GetOutput());
std::cerr << "All Heart Filters Ok: " << std::endl;
////////////////// EXTRACT ADVECTION SLICE
///////////////
auto roi_image_gvf = ROIFilter_Heart_GVF->GetOutput();
roi_image_gvf->SetSpacing(spacing_gfv_vector);
roi_image_gvf->SetOrigin(origin_gfv_vector);
roi_image_gvf->SetDirection(direction_gfv_vector);
auto intermediare_advection_image = ImageVectorType_Heart_2D::New();
ImageVectorType_Heart_2D::RegionType region_vector_image_heart_2D(start_vector_image_heart_2D, size_vector_image_heart_2D);
intermediare_advection_image->SetRegions(region_vector_image_heart_2D);
intermediare_advection_image->Allocate();
intermediare_advection_image->SetSpacing(spacing_vector_2D);
intermediare_advection_image->SetOrigin(origin_vector_2D);
intermediare_advection_image->SetDirection(direction_vector_2D);
itk::ImageRegionIterator<ImageVectorType_Heart> it_adv_vec(roi_image_gvf, roi_image_gvf->GetBufferedRegion());
for(it_adv_vec.GoToBegin();!it_adv_vec.IsAtEnd(); ++it_adv_vec)
{
if((it_adv_vec.Get()[0]!=0)||(it_adv_vec.Get()[1]!=0))
if(it_adv_vec.GetIndex()[2]==slice_to_process){
IndexType_gvf Index_vec;
Index_vec[0] = it_adv_vec.GetIndex()[0];
Index_vec[1] = it_adv_vec.GetIndex()[1];
IndexType_gvf_2 Index_vec_3;
Index_vec_3[0] = Index_vec[0];
Index_vec_3[1] = Index_vec[1];
itk::Vector<float, 3> vec;
vec[0] = it_adv_vec.Get()[0];
vec[1] = it_adv_vec.Get()[1];
int vec_pass_advection_x = vec[0];
int vec_pass_advection_y = vec[1];
itk::FixedArray<float,2> vec2;
vec2[0] = vec_pass_advection_x;
vec2[1] = vec_pass_advection_y;
intermediare_advection_image->SetPixel(Index_vec_3,vec2);
}
}
// intermediare_advection_image->SetSpacing(spacing_vector_2D);
// intermediare_advection_image->SetOrigin(origin_vector_2D);
// intermediare_advection_image->SetDirection(direction_vector_2D);
std::cerr << "Advection 2D Image Ok: " << std::endl;
// APPLY SEED TO FAST MARCHING
InternalImageType_2D::IndexType seedPosition_AGC;
std::cerr << "Create Seed Ok: " << std::endl;
auto seeds = NodeContainer_Heart::New();
NodeType_Heart node_heart;
node_heart.SetValue(seedValue);
node_heart.SetIndex(seedPosition_AGC);
seeds->Initialize();
seeds->InsertElement(0, node_heart);
std::cerr << "Seed Inserted Ok: " << std::endl;
fastMarching_Heart->SetTrialPoints(seeds);
fastMarching_Heart->SetSpeedConstant(1.0);
std::cerr << "Seeds Set to FM Ok: " << std::endl;
// fastMarching_Heart->Update();
std::cerr << "Fast Marching Update Ok: " << std::endl;
/// GEODESIC ACTIVE CONTOUR
auto geodesicActiveContour_heart = GeodesicActiveContourFilterType_Heart::New();
geodesicActiveContour_heart->SetPropagationScaling(propagationScaling);
// geodesicActiveContour_heart->SetAdvectionImage(intermediare_advection_image.GetPointer());
geodesicActiveContour_heart->SetCurvatureScaling(SetCurvatureScaling);
geodesicActiveContour_heart->SetAdvectionScaling(SetAdvectionScaling);
geodesicActiveContour_heart->SetMaximumRMSError(SetMaximumRMSError);
geodesicActiveContour_heart->SetNumberOfIterations(NumberofIterations);
geodesicActiveContour_heart->SetInput(fastMarching_Heart->GetOutput());
geodesicActiveContour_heart->SetFeatureImage(sigmoid_Heart_Seg->GetOutput());
// geodesicActiveContour_heart->SetFeatureImage(gradientMagnitude_Heart_Seg->GetOutput());
std::cerr << "AGC Ok: " << std::endl;
auto thresholder_heart = ThresholdingFilterType_Heart::New();
thresholder_heart->SetLowerThreshold(LowerThreshold_Thresholder);
thresholder_heart->SetUpperThreshold(UpperThreshold_Thresholder);
thresholder_heart->SetOutsideValue(itk::NumericTraits<InternalPixelType>::min());
thresholder_heart->SetInsideValue(itk::NumericTraits<InternalPixelType>::max());
thresholder_heart->SetInput(geodesicActiveContour_heart->GetOutput());
std::cerr << "Thresholder Ok: " << std::endl;
// APPLY ACTIVE GEODESIC CONTOUR SLICE BY SLICE
geodesicActiveContour_heart->Update();
thresholder_heart->Update();
auto intermediare_image_heart = thresholder_heart->GetOutput();
intermediare_image_heart->SetSpacing(spacing_internal_2D);
intermediare_image_heart->SetOrigin(origin_internal_2D);
intermediare_image_heart->SetDirection(direction_internal_2D);
itk::ImageRegionConstIteratorWithIndex<InternalImageType_2D> It_Heart_Write(intermediare_image_heart, intermediare_image_heart->GetLargestPossibleRegion());
for(It_Heart_Write.GoToBegin(); !It_Heart_Write.IsAtEnd(); ++It_Heart_Write)
{
if(It_Heart_Write.Get()!=0){
InternalImageType::IndexType index_to_get;
index_to_get[0] = It_Heart_Write.GetIndex()[0];
index_to_get[1] = It_Heart_Write.GetIndex()[1];
index_to_get[2] = slice_to_process;
final_image_heart->SetPixel(index_to_get, 255);}
}
std::cerr << "Final Image Ok: " << std::endl;
if(slice_to_process == Heart_ROI_Image->GetBufferedRegion().GetSize()[2]-2){
// AGC CONTROL STATS
typedef itk::SliceBySliceImageFilter< InternalImageType, InternalImageType> SliceBySliceImageFilterType_Heart;
auto SmoothingFilter_Heart_Seg_2 = SmoothingFilterType_Heart_Seg::New();
SmoothingFilter_Heart_Seg_2->SetTimeStep(0.09);
SmoothingFilter_Heart_Seg_2->SetNumberOfIterations(5);
SmoothingFilter_Heart_Seg_2->SetConductanceParameter(9.0);
// SmoothingFilter_Heart_Seg_2->SetInput(extracted_image_heart_2D);
auto gradientMagnitude_Heart_Seg_2 = GradientFilterType_Heart_Seg::New();
gradientMagnitude_Heart_Seg_2->SetSigma(sigma);
gradientMagnitude_Heart_Seg_2->SetInput(SmoothingFilter_Heart_Seg_2->GetOutput());
// gradientMagnitude_Heart_Seg_2->Update();
auto sigmoid_Heart_Seg_2 = SigmoidFilterType_Heart_Seg::New();
sigmoid_Heart_Seg_2->SetOutputMinimum(OutputMinimum);
sigmoid_Heart_Seg_2->SetOutputMaximum(OutputMaximum);
sigmoid_Heart_Seg_2->SetAlpha(Alpha);
sigmoid_Heart_Seg_2->SetBeta(Beta);
sigmoid_Heart_Seg_2->SetInput(gradientMagnitude_Heart_Seg_2->GetOutput());
// sigmoid_Heart_Seg_2->Update();
auto fastMarching_Heart_2 = FastMarchingFilterType_Heart::New();
fastMarching_Heart_2->SetTrialPoints(seeds);
fastMarching_Heart_2->SetSpeedConstant(1.0);
// fastMarching_Heart_2->SetOutputSize(roi_for_Heart_extract_2D.GetSize());
fastMarching_Heart_2->SetInput(sigmoid_Heart_Seg_2->GetOutput());
// fastMarching_Heart_2->Update();
////////////
auto sliceBySliceFilter_Heart_1 = SliceBySliceImageFilterType_Heart::New();
sliceBySliceFilter_Heart_1->SetInput(Heart_ROI_Image_2); // input
sliceBySliceFilter_Heart_1->SetInputFilter(SmoothingFilter_Heart_Seg_2); // the first filter of the image processing pipline
sliceBySliceFilter_Heart_1->SetOutputFilter(gradientMagnitude_Heart_Seg_2); // the first filter of the image processing pipline
sliceBySliceFilter_Heart_1->SetDimension(2);
sliceBySliceFilter_Heart_1->Update();
//
auto sliceBySliceFilter_Heart_2 = SliceBySliceImageFilterType_Heart::New();
sliceBySliceFilter_Heart_2->SetInput(Heart_ROI_Image_2); // input
sliceBySliceFilter_Heart_2->SetInputFilter(SmoothingFilter_Heart_Seg_2); // the first filter of the image processing pipline
sliceBySliceFilter_Heart_2->SetOutputFilter(sigmoid_Heart_Seg_2); // the first filter of the image processing pipline
sliceBySliceFilter_Heart_2->SetDimension(2);
sliceBySliceFilter_Heart_2->Update();
//
auto sliceBySliceFilter_Heart_3 = SliceBySliceImageFilterType_Heart::New();
sliceBySliceFilter_Heart_3->SetInput(Heart_ROI_Image_2); // input
sliceBySliceFilter_Heart_3->SetInputFilter(SmoothingFilter_Heart_Seg_2); // the first filter of the image processing pipline
sliceBySliceFilter_Heart_3->SetOutputFilter(fastMarching_Heart_2); // the first filter of the image processing pipline
sliceBySliceFilter_Heart_3->SetDimension(2);
sliceBySliceFilter_Heart_3->Update();
std::cerr << "Controls Ok: " << std::endl;
// WRITE CONTROLS
using WriterType_Heart_Controls =itk::ImageFileWriter<InternalImageType>;
auto writer_Heart_Final_1 = WriterType_Heart_Controls::New();
writer_Heart_Final_1->SetFileName("gradient magnitude.tiff");
writer_Heart_Final_1->SetInput(sliceBySliceFilter_Heart_1->GetOutput());
try
{
writer_Heart_Final_1->Update();
}
catch (const itk::ExceptionObject & excep)
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
return EXIT_FAILURE;
}
auto writer_Heart_Final_2 = WriterType_Heart_Controls::New();
writer_Heart_Final_2->SetFileName("sigmoid filter.tiff");
writer_Heart_Final_2->SetInput(sliceBySliceFilter_Heart_2->GetOutput());
try
{
writer_Heart_Final_2->Update();
}
catch (const itk::ExceptionObject & excep)
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
return EXIT_FAILURE;
}
auto writer_Heart_Final_3 = WriterType_Heart_Controls::New();
writer_Heart_Final_3->SetFileName("fast marching.tiff");
writer_Heart_Final_3->SetInput(sliceBySliceFilter_Heart_3->GetOutput());
try
{
writer_Heart_Final_3->Update();
}
catch (const itk::ExceptionObject & excep)
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
return EXIT_FAILURE;
}
std::cerr << "Write Controls Ok: " << std::endl;
}
std::cerr << std::endl;
}// END LOOP