ITK学习笔记-2:图像采样

来源:互联网 发布:basic编程语言 编辑:程序博客网 时间:2024/05/01 01:20
主要是利用ITK对RBG图像进行采样处理。至于ITK的管道结构等基础概念不在此进行讨论,本文直接给出代码。

#include "itkImage.h"#include "itkIdentityTransform.h"#include "itkImageFileReader.h"#include "itkVectorResampleImageFilter.h"#include "itkVectorNearestNeighborInterpolateImageFunction.h"#include "itkRGBPixel.h"#include "QuickView.h"typedef itk::Image<itk::RGBPixel<unsigned char>, 2> ImageType;static void CreateImage(ImageType::Pointer image);int main(int argc, char *argv[]){double factor = 2.0;// Create input imageImageType::Pointer input;if(argc < 2){input = ImageType::New();CreateImage(input);}else{typedef itk::ImageFileReader<ImageType> ReaderType;ReaderType::Pointer reader = ReaderType::New();reader->SetFileName(argv[1]);reader->Update();input = reader->GetOutput();if (argc > 2){factor = atof(argv[2]);}}ImageType::SizeType inputSize = input->GetLargestPossibleRegion().GetSize();std::cout << "Input size: " << inputSize << std::endl;// ResizeImageType::SizeType outputSize;outputSize[0] = inputSize[0] * factor;outputSize[1] = inputSize[1] * factor;ImageType::SpacingType outputSpacing;outputSpacing[0] = input->GetSpacing()[0] * (static_cast<double>(inputSize[0]) / static_cast<double>(outputSize[0]));outputSpacing[1] = input->GetSpacing()[1] * (static_cast<double>(inputSize[1]) / static_cast<double>(outputSize[1]));typedef itk::IdentityTransform<double, 2> TransformType;typedef itk::VectorResampleImageFilter<ImageType, ImageType> ResampleImageFilterType;ResampleImageFilterType::Pointer resample = ResampleImageFilterType::New();resample->SetInput(input);resample->SetSize(outputSize);resample->SetOutputSpacing(outputSpacing);resample->SetTransform(TransformType::New());resample->UpdateLargestPossibleRegion();typedef itk::VectorNearestNeighborInterpolateImageFunction<ImageType, double >  NearestInterpolatorType;NearestInterpolatorType::Pointer nnInterpolator =NearestInterpolatorType::New();ResampleImageFilterType::Pointer resampleNN =ResampleImageFilterType::New();resampleNN->SetInput(input);resampleNN->SetSize(outputSize);resampleNN->SetOutputSpacing(outputSpacing);resampleNN->SetTransform(TransformType::New());resampleNN->SetInterpolator(nnInterpolator);//相比较于,多了最近邻插值resampleNN->UpdateLargestPossibleRegion();ImageType::Pointer output = resample->GetOutput();std::cout << "Output size: " << output->GetLargestPossibleRegion().GetSize() << std::endl;QuickView viewer;viewer.AddRGBImage(input.GetPointer(),true,"Original");viewer.AddRGBImage(output.GetPointer(),true,"Resampled");viewer.AddRGBImage(resampleNN->GetOutput(),true,"Resampled NN");viewer.Visualize();return EXIT_SUCCESS;}void CreateImage(ImageType::Pointer image){// Create a black image with 2 white regionsImageType::IndexType start;start.Fill(0);ImageType::SizeType size;size.Fill(200);ImageType::RegionType region(start,size);image->SetRegions(region);image->Allocate();image->FillBuffer( itk::NumericTraits< ImageType::PixelType >::Zero);ImageType::PixelType pixel;pixel.SetRed(200);pixel.SetGreen(50);pixel.SetBlue(50);// Make a squarefor(unsigned int r = 20; r < 80; r++){for(unsigned int c = 30; c < 100; c++){ImageType::IndexType pixelIndex;pixelIndex[0] = r;pixelIndex[1] = c;image->SetPixel(pixelIndex, pixel);}}}

运行时候界面如下图所示:


因为主程序中所采样的是int main(int argc, char*argv[])

所以在执行exe的时候,通过dos窗口进行exe所在的目录,后续的两个参数分别为F:\DIPcode\SIMC\imagesrc\2.jpg  0.2

第一个参数用于指定待处理的图像,第二个参数说明采样率。此处设置为0.2,则采样后的图像为原始图像尺寸的0.2倍。如上图可以看到,原始图像是512X512的图像,采样之后图像的尺寸变为102X102,约为512*0.2。

运行结果如下图所示:






0 0
原创粉丝点击