Image::Pointer binary_and(Image::Pointer input, ImageBinary::Pointer mask) { Image::Pointer output = Image::New(); Image::RegionType region = input->GetLargestPossibleRegion(); output->SetRegions(region); try { output->Allocate(); } catch(itk::ExceptionObject & err) { std::cout<<"Error in binary_and. Allocation failed"<<std::endl; std::cout<<err<<std::endl; return NULL; } ConstIteratorBinary iterator_mask(mask, mask->GetLargestPossibleRegion()); Iterator iterator_input(input, input->GetLargestPossibleRegion()); Iterator iterator_output(output, output->GetLargestPossibleRegion()); // all images should be the same size try { while(!iterator_input.IsAtEnd()) { if(iterator_mask.Get() == 0) iterator_output.Set(0); else iterator_output.Set(iterator_input.Get()); ++iterator_input; ++iterator_mask; ++iterator_output; } } catch(itk::ExceptionObject & err) { std::cout<<"Iterator error"<<std::endl; std::cout<<err<<std::endl; } return output; }