// ------------------------------------------------------------------------ void buildOutput(const SeriesTransform::List &series, ImageType::Pointer &outputImage, ImageType::Pointer &outputLabel, const unsigned int & timestep) { // get the output parameters unsigned int slices = series.size(); unsigned int timeSteps = series.front().images.size(); ImageType::Pointer ref = series.front().images.front(); ImageType::SpacingType spacing = ref->GetSpacing(); spacing[2] = series.front().sliceThickness; ImageType::DirectionType direction = ref->GetDirection(); ImageType::PointType origin = ref->GetOrigin(); ImageType::RegionType region = ref->GetLargestPossibleRegion(); region.SetSize(2,slices); // create the outputs outputImage->SetSpacing(spacing); outputImage->SetDirection(direction); outputImage->SetOrigin(origin); outputImage->SetRegions(region); outputImage->Allocate(); outputLabel->SetSpacing(spacing); outputLabel->SetDirection(direction); outputLabel->SetOrigin(origin); outputLabel->SetRegions(region); outputLabel->Allocate(); itk::ImageRegionIterator<ImageType> outLIt(outputLabel, outputLabel->GetLargestPossibleRegion()); itk::ImageRegionIterator<ImageType> outImIt(outputImage, outputImage->GetLargestPossibleRegion()); // loop through the slices for(unsigned int i = 0; i < slices; i++) { ImageType::Pointer im = series[i].images[timestep]; ImageType::Pointer label = series[i].labelImages[timestep]; itk::ImageRegionConstIterator<ImageType> imIt(im, im->GetLargestPossibleRegion()); itk::ImageRegionConstIterator<ImageType> lIt(label, label->GetLargestPossibleRegion()); while(!imIt.IsAtEnd()) { outLIt.Set(lIt.Get()); outImIt.Set(imIt.Get()); ++imIt; ++lIt; ++outLIt; ++outImIt; } } }
bool TestIsClosedLoop() { std::cout << "TestIsClosedLoop()" << std::endl; // Open loop { typedef itk::Image<unsigned char, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{100,100}}; itk::ImageRegion<2> region(corner, size); image->SetRegions(region); image->Allocate(); image->FillBuffer(0); for(itk::Index<2>::IndexValueType i = 20; i < 30; ++i) { itk::Index<2> pixel = {{i, 50}}; image->SetPixel(pixel, 255); } itk::Index<2> start = {{25, 50}}; bool isClosedLoop = ITKHelpers::IsClosedLoop(image.GetPointer(), start); std::cout << "Is closed loop? " << isClosedLoop << std::endl; } // Closed loop { typedef itk::Image<unsigned char, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{100,100}}; itk::ImageRegion<2> region(corner, size); image->SetRegions(region); image->Allocate(); image->FillBuffer(0); itk::Index<2> corner0 = {{10,10}}; itk::Index<2> corner1 = {{30,30}}; ITKHelpers::DrawRectangle(image.GetPointer(), 255, corner0, corner1); itk::Index<2> start = {{10, 10}}; bool isClosedLoop = ITKHelpers::IsClosedLoop(image.GetPointer(), start); std::cout << "Is closed loop? " << isClosedLoop << std::endl; } return true; }
void TestGetOpenContourOrdering() { typedef itk::Image<unsigned char, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{100,100}}; itk::ImageRegion<2> region(corner, size); image->SetRegions(region); image->Allocate(); image->FillBuffer(0); for(unsigned int i = 20; i < 30; ++i) { itk::Index<2> pixel = {{i, 50}}; image->SetPixel(pixel, 255); } itk::Index<2> start = {{25, 50}}; std::vector<itk::Index<2> > contourOrdering = ITKHelpers::GetOpenContourOrdering(image.GetPointer(), start); for(unsigned int i = 0; i < contourOrdering.size(); ++i) { //std::cout << breadthFirstOrdering[i] << " "; std::cout << contourOrdering[i] << std::endl; } std::cout << std::endl; }
int main() { std::string votesfilename = "../../half_vessel_votes2.txt"; std::string outputfilename = "half_vessel_votes2.tif"; FILE * fp = fopen(votesfilename.c_str(), "r"); int x,y,z,v; int maxx = -1,maxy = -1, maxz = -1; while(fscanf(fp,"%d %d %d %d",&x,&y,&z,&v)>0) { maxx = MAX(maxx, x); maxy = MAX(maxy, y); maxz = MAX(maxz, z); } printf("maxx = %d maxy = %d maxz = %d\n",maxx,maxy,maxz); fclose(fp); fp = fopen(votesfilename.c_str(), "r"); ImageType::Pointer im = ImageType::New(); ImageType::SizeType size; ImageType::IndexType index; ImageType::RegionType region; size[0] = maxx+1; size[1] = maxy+1; size[2] = maxz+1; index[0] = index[1] = index[2] = 0; region.SetIndex(index); region.SetSize(size); im->SetRegions(region); im->Allocate(); while(fscanf(fp,"%d %d %d %d",&x,&y,&z,&v)>0) { index[0] = x; index[1] = y; index[2] = z; im->SetPixel(index,v); } // scanf("%*d"); fclose(fp); FileWriterType::Pointer writer = FileWriterType::New(); writer->SetFileName(outputfilename.c_str()); writer->SetInput(im); writer->Update(); return 0; }
bool TestBreadthFirstOrderingNonZeroPixels() { std::cout << "TestBreadthFirstOrderingNonZeroPixels()" << std::endl; typedef itk::Image<unsigned char, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{100,100}}; itk::ImageRegion<2> region(corner, size); image->SetRegions(region); image->Allocate(); image->FillBuffer(0); for(itk::Index<2>::IndexValueType i = 20; i < 30; ++i) { itk::Index<2> pixel = {{i, 50}}; image->SetPixel(pixel, 255); } itk::Index<2> start = {{25, 50}}; std::vector<itk::Index<2> > breadthFirstOrdering = ITKHelpers::BreadthFirstOrderingNonZeroPixels(image.GetPointer(), start); for(unsigned int i = 0; i < breadthFirstOrdering.size(); ++i) { //std::cout << breadthFirstOrdering[i] << " "; std::cout << breadthFirstOrdering[i] << std::endl; } std::cout << std::endl; return true; }
int main() { typedef itk::Image<itk::CovariantVector<float, 2>, 2> ImageType; ImageType::Pointer image = ImageType::New(); ImageType::IndexType corner = {{0,0}}; ImageType::SizeType size = {{100,100}}; ImageType::RegionType region(corner, size); image->SetRegions(region); image->Allocate(); typedef ImageType::PixelType::RealValueType ScalarType; typedef itk::NormImageAdaptor<ImageType, ScalarType> NormImageAdaptorType; typename NormImageAdaptorType::Pointer imageAdaptor = NormImageAdaptorType::New(); imageAdaptor->SetImage(image); ScalarType rangeMin = 0; ScalarType rangeMax = 255; unsigned int numberOfBins = 10; typedef int BinValueType; typedef HistogramGenerator<BinValueType> HistogramGeneratorType; typedef HistogramGeneratorType::HistogramType HistogramType; HistogramType histogram = HistogramGeneratorType::ComputeImageHistogram1D(imageAdaptor.GetPointer(), imageAdaptor->GetLargestPossibleRegion(), numberOfBins, rangeMin, rangeMax); histogram.Print(); std::cout << std::endl; return 0; }
void TestGetClosedContourOrdering() { typedef itk::Image<unsigned char, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{100,100}}; itk::ImageRegion<2> region(corner, size); image->SetRegions(region); image->Allocate(); image->FillBuffer(0); itk::Index<2> corner0 = {{10,10}}; itk::Index<2> corner1 = {{30,30}}; ITKHelpers::DrawRectangle(image.GetPointer(), 255, corner0, corner1); itk::Index<2> start = {{10, 10}}; std::vector<itk::Index<2> > ordering = ITKHelpers::GetClosedContourOrdering(image.GetPointer(), start); for(unsigned int i = 0; i < ordering.size(); ++i) { std::cout << ordering[i] << std::endl; } std::cout << std::endl; }
ImageType::Pointer TDimImage::toItkImage( const unsigned int &channel ) const { int sample = std::min<int>( this->samples()-1, channel); int bitdepth = sizeof(PixelType); D_DataFormat pf = D_FMT_UNSIGNED; if (!std::numeric_limits<PixelType>::is_integer) pf = D_FMT_FLOAT; else if (std::numeric_limits<PixelType>::is_signed) pf = D_FMT_SIGNED; TDimImage img; if (this->depth()!=bitdepth || this->pixelType()!=pf) { img = this->ensureTypedDepth(); img = img.convertToDepth( bitdepth, DimLut::ltLinearDataRange, pf ); } else img = *this; //------------------------------------------------------------------ // Create Itk Image and copy data //------------------------------------------------------------------ ImageType::Pointer image = ImageType::New(); ImageType::SizeType size; size[0] = nx; size[1] = ny; ImageType::IndexType start; start[0] = 0; start[1] = 0; ImageType::RegionType region; region.SetSize( size ); region.SetIndex( start ); image->SetRegions( region ); image->Allocate(); double spacing[2]; spacing[0] = 1.0; spacing[1] = 1.0; image->SetSpacing( spacing ); double origin[2]; origin[0] = 0.0; origin[1] = 0.0; image->SetOrigin( origin ); typedef itk::ImageRegionIterator< ImageType > IteratorType; IteratorType it( image, region ); it.GoToBegin(); // copy data PixelType *data = (PixelType *) img.sampleBits(sample); while( ! it.IsAtEnd() ) { it.Set( *data ); ++it; ++data; } return image; }
int main(int, char* [] ) { typedef itk::Image<unsigned char, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner={{0,0}}; itk::Size<2> size = {{10,10}}; itk::ImageRegion<2> region(corner,size); image->SetRegions(region); image->Allocate(); image->FillBuffer(0); unsigned char searchValue = 255; // This value does not appear in the image, so both functions // will have to search the entire image. int counter = 0; for(unsigned int i = 0; i < 1e7; ++i) { // counter += HasValue(image.GetPointer(), searchValue); // About 3 seconds counter += HasValueConditional(image.GetPointer(), searchValue); // About 3.3 seconds } std::cout << "counter " << counter << std::endl; return 0; }
void setup_snapshot_image(string basename, ImageType::Pointer model) { ImageType::SpacingType spacing = model->GetSpacing(); spacing[0] /= constants::SnapshotImageZoom; spacing[1] /= constants::SnapshotImageZoom; spacing[2] /= constants::SnapshotImageZoom; g_snapshot_image = ImageType::New(); g_snapshot_image->SetSpacing( spacing ); g_snapshot_image->SetOrigin( model->GetOrigin() ); ImageType::RegionType const& region = model->GetLargestPossibleRegion(); ImageType::RegionType::SizeType size = region.GetSize(); // size is in pixels ImageType::SizeType doubled_size(size); doubled_size[0] *= constants::SnapshotImageZoom; doubled_size[1] *= constants::SnapshotImageZoom; doubled_size[2] *= constants::SnapshotImageZoom; g_snapshot_image->SetRegions( doubled_size ); g_snapshot_image->Allocate(); s_snapshot_basename = basename; }
void ITKImage() { typedef itk::Image<float, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{20, 20}}; itk::ImageRegion<2> region(corner, size); image->SetRegions(region); image->Allocate(); itk::ImageRegionIterator<ImageType> imageIterator(image, region); itk::TimeProbe clock1; clock1.Start(); while(!imageIterator.IsAtEnd()) { float a = imageIterator.Get() - imageIterator.Get(); ++imageIterator; } clock1.Stop(); std::cout << "Total: " << clock1.GetTotal() << std::endl; }
void NRRDTools::saveNRRDFile(const cleaver::FloatField *field, const std::string &name) { auto dims = field->dataBounds().size; ImageType::Pointer img = ImageType::New(); itk::Index<3> start; start.Fill(0); ImageType::SizeType size; size[0] = static_cast<size_t>(dims[0]); size[1] = static_cast<size_t>(dims[1]); size[2] = static_cast<size_t>(dims[2]); ImageType::RegionType region(start, size); img->SetRegions(region); img->Allocate(); img->FillBuffer(0); for (size_t i = 0; i < dims[0]; i++) { for (size_t j = 0; j < dims[1]; j++) { for (size_t k = 0; k < dims[2]; k++) { ImageType::IndexType pixelIndex; pixelIndex[0] = i; pixelIndex[1] = j; pixelIndex[2] = k; auto data = ((cleaver::FloatField*)field)->data(); img->SetPixel(pixelIndex, data[i + size[0] * j + size[0] * size[1] * k]); } } } WriterType::Pointer write = WriterType::New(); write->SetFileName(name + ((name.find_first_of(".nrrd") == std::string::npos) ? ".nrrd" : "")); write->SetInput(img); write->Update(); }
void CreateKernel(ImageType::Pointer kernel, unsigned int width) { ImageType::IndexType start; start.Fill(0); ImageType::SizeType size; size.Fill(width); ImageType::RegionType region; region.SetSize(size); region.SetIndex(start); kernel->SetRegions(region); kernel->Allocate(); itk::ImageRegionIterator<ImageType> imageIterator(kernel, region); while(!imageIterator.IsAtEnd()) { //imageIterator.Set(255); imageIterator.Set(1); ++imageIterator; } }
/* * random a voxel. define plane through this voxel. reslice at the plane. compare the pixel vaues of the voxel * in the volume with the pixel value in the resliced image. * there are some indice shifting problems which causes the test to fail for oblique planes. seems like the chosen * worldcoordinate is not corrresponding to the index in the 2D image. and so the pixel values are not the same as * expected. */ static void PixelvalueBasedTest() { /* setup itk image */ typedef itk::Image<unsigned short, 3> ImageType; typedef itk::ImageRegionConstIterator< ImageType > ImageIterator; ImageType::Pointer image = ImageType::New(); ImageType::IndexType start; start[0] = start[1] = start[2] = 0; ImageType::SizeType size; size[0] = size[1] = size[2] = 32; ImageType::RegionType imgRegion; imgRegion.SetSize(size); imgRegion.SetIndex(start); image->SetRegions(imgRegion); image->SetSpacing(1.0); image->Allocate(); ImageIterator imageIterator( image, image->GetLargestPossibleRegion() ); imageIterator.GoToBegin(); unsigned short pixelValue = 0; //fill the image with distinct values while ( !imageIterator.IsAtEnd() ) { image->SetPixel(imageIterator.GetIndex(), pixelValue); ++imageIterator; ++pixelValue; } /* end setup itk image */ mitk::Image::Pointer imageInMitk; CastToMitkImage(image, imageInMitk); /*mitk::ImageWriter::Pointer writer = mitk::ImageWriter::New(); writer->SetInput(imageInMitk); std::string file = "C:\\Users\\schroedt\\Desktop\\cube.nrrd"; writer->SetFileName(file); writer->Update();*/ PixelvalueBasedTestByPlane(imageInMitk, mitk::PlaneGeometry::Frontal); PixelvalueBasedTestByPlane(imageInMitk, mitk::PlaneGeometry::Sagittal); PixelvalueBasedTestByPlane(imageInMitk, mitk::PlaneGeometry::Axial); }
int main(int argc, char *argv[]) { // Verify arguments if(argc < 6) { std::cerr << "Required: outputFilename sizeX sizeY components value" << std::endl; return EXIT_FAILURE; } // Parse arguments std::string outputFilename = argv[1]; std::stringstream ss; ss << argv[2] << " " << argv[3] << " " << argv[4]; itk::Size<2> size = {{0,0}}; unsigned int components = 0; float value = 0.0f; ss >> size[0] >> size[1] >> components >> value; // Output arguments std::cout << "outputFilename " << outputFilename << std::endl; std::cout << "size " << size << std::endl; std::cout << "value " << value << std::endl; ImageType::Pointer image = ImageType::New(); itk::Index<2> index = {{0,0}}; itk::ImageRegion<2> region(index, size); image->SetRegions(region); image->SetNumberOfComponentsPerPixel(components); image->Allocate(); typename itk::ImageRegionIterator<ImageType> imageIterator(image, image->GetLargestPossibleRegion()); ImageType::PixelType pixel; pixel.SetSize(components); pixel.Fill(value); while(!imageIterator.IsAtEnd()) { imageIterator.Set(pixel); ++imageIterator; } // Write the result typedef itk::ImageFileWriter<ImageType> WriterType; WriterType::Pointer writer = WriterType::New(); writer->SetFileName(outputFilename); writer->SetInput(image); writer->Update(); return EXIT_SUCCESS; }
// ------------------------------------------------------------------------ void createOutput(const ImageType::Pointer &input, ImageType::Pointer &output) { output->SetDirection(input->GetDirection()); output->SetSpacing(input->GetSpacing()); output->SetRegions(input->GetLargestPossibleRegion()); output->SetOrigin(input->GetOrigin()); output->Allocate(); output->FillBuffer(0); }
// ------------------------------------------------------------------------ void buildShortAxisVolume(const SeriesTransform::Map &transforms, const unsigned int seriesNumber, ImageType::Pointer &saVolume) { // get the short axis transforms SeriesTransform::Map::const_iterator mapIt = transforms.begin(); std::vector<SeriesTransform> saSlices; while(mapIt != transforms.end()) { if(mapIt->second.series == seriesNumber) { unsigned int sliceNum = mapIt->second.slice; if(saSlices.size() < (sliceNum+1)) saSlices.resize(sliceNum+1); saSlices[sliceNum] = mapIt->second; } ++mapIt; } // get the dimensions of the output image ImageType::Pointer reference = saSlices[0].images[0]; unsigned int x,y,z; x = reference->GetLargestPossibleRegion().GetSize()[0]; y = reference->GetLargestPossibleRegion().GetSize()[1]; z = saSlices.size(); ImageType::RegionType region; ImageType::SizeType size; ImageType::IndexType index; size[0] = x; size[1] = y; size[2] = z; index.Fill(0); region.SetSize(size); region.SetIndex(index); // get the other parts ImageType::SpacingType spacing = reference->GetSpacing(); spacing[2] = saSlices[0].sliceThickness; ImageType::DirectionType direction = reference->GetDirection(); ImageType::PointType origin = reference->GetOrigin(); saVolume->SetOrigin(origin); saVolume->SetDirection(direction); saVolume->SetSpacing(spacing); saVolume->SetRegions(region); saVolume->Allocate(); saVolume->FillBuffer(0); }
void Vector() { typedef itk::Image<unsigned char, 2 > ChannelType; const unsigned int NumberOfChannels = 3; typedef itk::Image<itk::CovariantVector<unsigned char, NumberOfChannels>, 2 > ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0,0}}; itk::Size<2> imageSize = {{500,500}}; itk::ImageRegion<2> fullRegion(corner, imageSize); image->SetRegions(fullRegion); image->Allocate(); for(unsigned int i = 0; i < NumberOfChannels; ++i) { itk::RandomImageSource<ChannelType>::Pointer randomImageSource = itk::RandomImageSource<ChannelType>::New(); randomImageSource->SetNumberOfThreads(1); // to produce non-random results randomImageSource->SetSize(imageSize); randomImageSource->Update(); ITKHelpers::SetChannel(image.GetPointer(), i, randomImageSource->GetOutput()); } itk::Size<2> patchSize = {{21,21}}; // There is nothing magic about these particular patches itk::Index<2> targetCorner = {{319, 302}}; itk::ImageRegion<2> targetRegion(targetCorner, patchSize); itk::Index<2> sourceCorner = {{341, 300}}; itk::ImageRegion<2> sourceRegion(sourceCorner, patchSize); Mask::Pointer mask = Mask::New(); mask->SetRegions(fullRegion); mask->Allocate(); ITKHelpers::SetImageToConstant(mask.GetPointer(), mask->GetValidValue()); typedef SumSquaredPixelDifference<ImageType::PixelType> PixelDifferenceType; typedef ImagePatchPixelDescriptor<ImageType> PatchType; ImagePatchDifference<PatchType, PixelDifferenceType> imagePatchDifference; PatchType targetPatch(image, mask, targetRegion); PatchType sourcePatch(image, mask, sourceRegion); float difference = imagePatchDifference(targetPatch, sourcePatch); std::cout << "GMHDifference: " << difference << std::endl; }
void TestDeepCopyUnsignedCharScalar() { itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{10,10}}; itk::ImageRegion<2> region(corner, size); typedef itk::Image<unsigned char, 2> ImageType; ImageType::Pointer imageIn = ImageType::New(); imageIn->SetRegions(region); imageIn->Allocate(); ImageType::Pointer imageOut = ImageType::New(); ITKHelpers::DeepCopy(imageIn.GetPointer(), imageOut.GetPointer()); }
void TestDeepCopyFloatVector() { itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{10,10}}; itk::ImageRegion<2> region(corner, size); typedef itk::VectorImage<float, 2> ImageType; ImageType::Pointer imageIn = ImageType::New(); imageIn->SetRegions(region); imageIn->SetNumberOfComponentsPerPixel(3); imageIn->Allocate(); ImageType::Pointer imageOut = ImageType::New(); ITKHelpers::DeepCopy(imageIn.GetPointer(), imageOut.GetPointer()); }
ComposeImageFilter::ImageType::Pointer ComposeImageFilter::ComposeIt() { ImageType::Pointer outputImage = ImageType::New(); outputImage->SetRegions(m_imageLR->GetLargestPossibleRegion()); outputImage->SetOrigin(m_imageLR->GetOrigin()); outputImage->SetSpacing(m_imageLR->GetSpacing()); outputImage->SetDirection(m_imageLR->GetDirection()); outputImage->Allocate(); outputImage->FillBuffer(0.0); std::cout << "outputImage" << std::endl; ImageType::IndexType testIndex; testIndex[0] = 0; testIndex[1]=0; testIndex[2]=0; std::cout << m_matrix.rows() << " " << m_matrix.cols() << std::endl; for (int i=0; i< m_matrix.rows() ; i++ ) { vcl_vector<int> rowIndices; vcl_vector<float> rowValues; vcl_vector<pair_t> rowM = m_matrix.get_row(i); float sum_vox =0; ImageType::IndexType outputIndex; outputIndex = ComputeImageIndex(i, m_imageLR); // std::cout << "ImageLR " << " " << i << " " << outputIndex << std::endl; for (vcl_vector<pair_t>::const_iterator it = rowM.begin(); it != rowM.end(); ++it) { ImageType::IndexType HRIndex; HRIndex = ComputeImageIndex(it->first, m_imageHR); sum_vox += (it->second)* (m_imageHR->GetPixel(HRIndex)); // std::cout << HRIndex << " " << it->second << std::endl; } // std::cout << sum_vox << std::endl; outputImage->SetPixel(outputIndex, sum_vox); // } return outputImage; }
bool TestDeepCopyFloatScalar() { itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{10,10}}; itk::ImageRegion<2> region(corner, size); typedef itk::Image<float, 2> ImageType; ImageType::Pointer imageIn = ImageType::New(); imageIn->SetRegions(region); imageIn->Allocate(); ImageType::Pointer imageOut = ImageType::New(); ITKHelpers::DeepCopy(imageIn.GetPointer(), imageOut.GetPointer()); return true; }
bool TestRandomImage() { typedef itk::Image<unsigned char, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{100,100}}; itk::ImageRegion<2> region(corner, size); image->SetRegions(region); image->Allocate(); image->FillBuffer(0); ITKHelpers::RandomImage(image.GetPointer()); ITKHelpers::WriteImage(image.GetPointer(), "random.png"); return true; }
void TestUpsample() { typedef itk::Image<unsigned char, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{100,100}}; itk::ImageRegion<2> region(corner, size); image->SetRegions(region); image->Allocate(); ImageType::Pointer upsampled = ImageType::New(); ITKHelpers::Upsample(image.GetPointer(), 2, upsampled.GetPointer()); std::cout << "TestUpsample() output size: " << upsampled->GetLargestPossibleRegion().GetSize() << std::endl; }
bool VolumeProcess:: RunFillingZeroOnBouandary(int Bx,int By,int Bz) { ImageType::Pointer tempImg = ImageType::New(); tempImg->SetRegions( m_outputImage->GetLargestPossibleRegion() ); tempImg->Allocate(); tempImg->FillBuffer(0); ImageType::RegionType fullRegion = m_outputImage->GetBufferedRegion(); int numColumns = fullRegion.GetSize(0); int numRows = fullRegion.GetSize(1); int numStacks = fullRegion.GetSize(2); int i, j,k; itk::ImageRegionIteratorWithIndex< ImageType > itr( m_outputImage, fullRegion ); for(itr.GoToBegin(); !itr.IsAtEnd(); ++itr) { ImageType::IndexType index = itr.GetIndex(); ImageType::PixelType pix = m_outputImage->GetPixel(index); i = index[0]; j = index[1]; k = index[2]; if (i < Bx || i > numColumns -Bx || j < By || j > numRows-By ||k <Bz ||k > numStacks-Bz ) tempImg->SetPixel(itr.GetIndex(), 0); else tempImg->SetPixel(itr.GetIndex(), pix); } //Copy temp img back to image itk::ImageRegionIterator< ImageType > itr1( tempImg, tempImg->GetLargestPossibleRegion()); itk::ImageRegionIterator< ImageType > itr2( m_outputImage, m_outputImage->GetLargestPossibleRegion()); for(itr1.GoToBegin(), itr2.GoToBegin() ; !itr1.IsAtEnd(); ++itr1, ++itr2) { itr2.Set( itr1.Get() ); } if(debug) std::cerr << "RunFillingZero Done" << std::endl; return true; }
void TestDrawRectangle() { typedef itk::Image<unsigned char, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{100,100}}; itk::ImageRegion<2> region(corner, size); image->SetRegions(region); image->Allocate(); image->FillBuffer(0); itk::Index<2> corner0 = {{10,10}}; itk::Index<2> corner1 = {{30,30}}; ITKHelpers::DrawRectangle(image.GetPointer(), 255, corner0, corner1); ITKHelpers::WriteImage(image.GetPointer(), "rectangle.png"); }
int main(int, char* [] ) { typedef itk::Image<unsigned char, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0, 0}}; itk::Size<2> size = {{31, 31}}; // Both dimensions of the size must be odd for the logic in this code to work itk::ImageRegion<2> region(corner, size); image->SetRegions(region); image->Allocate(); image->FillBuffer(1); // This is the pixel we will repeatedly query itk::Index<2> queryIndex = {{size[0]/2, size[0]/2}}; // Create a list of all of the offsets relative to the queryIndex std::vector<itk::Offset<2> > offsets; itk::ImageRegionConstIteratorWithIndex<ImageType> imageIterator(image, region); while(!imageIterator.IsAtEnd()) { offsets.push_back(imageIterator.GetIndex() - queryIndex); ++imageIterator; } // Call these functions many times for timing stability int totalSum = 0; // This variable is used so the compiler doesn't optimize away the loop for(unsigned int i = 0; i < 1e4; ++i) { // ImageType::PixelType pixelSum = SumPixelsManual(image.GetPointer(), queryIndex, offsets); // about 2 seconds ImageType::PixelType pixelSum = SumPixelsIterator(image.GetPointer(), queryIndex, offsets); totalSum += pixelSum; } std::cout << "totalSum " << totalSum << std::endl; return 0; }
int main(int, char* [] ) { typedef itk::Image<unsigned char, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{10,10}}; itk::ImageRegion<2> region(corner,size); image->SetRegions(region); image->Allocate(); unsigned int counter = 0; // To make sure the loop isn't optimized away for(unsigned int i = 0; i < 1e7; ++i) { counter += Iterator(image.GetPointer()); // about 7.2 seconds // counter += IteratorWithIndex(image.GetPointer()); // about 2.6 seconds } std::cout << "counter " << counter << std::endl; // To make sure the loop isn't optimized away return 0; }
void TestGetAllPatchesContainingPixel() { typedef itk::Image<float, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{10,10}}; itk::ImageRegion<2> imageRegion(corner,size); image->SetRegions(imageRegion); image->Allocate(); image->FillBuffer(0.0f); itk::Index<2> queryPixel = {{5,5}}; std::vector<itk::ImageRegion<2> > allPatches = ITKHelpers::GetAllPatchesContainingPixel(queryPixel, 1, imageRegion); std::cout << "Number of patches " << allPatches.size() << std::endl; }
void TestHistogramOfGradients() { typedef itk::Image<float, 2> ImageType; ImageType::Pointer image = ImageType::New(); itk::Index<2> corner = {{0,0}}; itk::Size<2> size = {{100,100}}; itk::ImageRegion<2> region(corner, size); image->SetRegions(region); image->Allocate(); std::vector<float> histogram = ITKHelpers::HistogramOfGradients(image.GetPointer(), image->GetLargestPossibleRegion(), 10); for(unsigned int i = 0; i < histogram.size(); ++i) { std::cout << histogram[i] << " "; } std::cout << std::endl; }