目录
深度拷贝图像
#include "itkImage.h"
#include "itkImageRegionIterator.h"
template <typename TImage>
void DeepCopy(typename TImage::Pointer input, typename TImage::Pointer output)
{
output->SetRegions(input->GetLargestPossibleRegion());
output->Allocate();
itk::ImageRegionConstIterator<TImage> inputIterator(input, input->GetLargestPossibleRegion());
itk::ImageRegionIterator<TImage> outputIterator(output, output->GetLargestPossibleRegion());
while (!inputIterator.IsAtEnd())
{
outputIterator.Set(inputIterator.Get());
++inputIterator;
++outputIterator;
}
}
int main()
{
using ImageType = itk::Image<unsigned char, 2>;
auto image1 = ImageType::New();
auto image2 = ImageType::New();
DeepCopy<ImageType>(image1, image2);
return 0;
}
提取图像的感兴趣区域
Input image
Output image
示例代码
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRegionOfInterestImageFilter.h"
int
main(int argc, char * argv[])
{
if (argc != 7)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << " <InputFileName> <OutputFileName>";
std::cerr << " <start x> <end x> <start y> <end y>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
const char * inputFileName = argv[1];
const char * outputFileName = argv[2];
const auto startx = static_cast<itk::IndexValueType>(std::stoi(argv[3]));
const auto endx = static_cast<itk::IndexValueType>(std::stoi(argv[4]));
const auto starty = static_cast<itk::IndexValueType>(std::stoi(argv[5]));
const auto endy = static_cast<itk::IndexValueType>(std::stoi(argv[6]));
constexpr unsigned int Dimension = 2;
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType, Dimension>;
const auto input = itk::ReadImage<ImageType>(inputFileName);
ImageType::IndexType start;
start[0] = startx;
start[1] = starty;
ImageType::IndexType end;
end[0] = endx;
end[1] = endy;
ImageType::RegionType region;
region.SetIndex(start);
region.SetUpperIndex(end);
using FilterType = itk::RegionOfInterestImageFilter<ImageType, ImageType>;
auto filter = FilterType::New();
filter->SetInput(input);
filter->SetRegionOfInterest(region);
try
{
itk::WriteImage(filter->GetOutput(), outputFileName);
}
catch (const itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
提取图像的感兴趣区域 方法二
#include "itkImage.h"
#include <itkExtractImageFilter.h>
int
main()
{
using ImageType = itk::Image<unsigned char, 2>;
ImageType::IndexType start;
start.Fill(0);
ImageType::SizeType size;
size.Fill(10);
ImageType::RegionType region(start, size);
auto image = ImageType::New();
image->SetRegions(region);
image->Allocate();
image->FillBuffer(5);
std::cout << "Image largest region: " << image->GetLargestPossibleRegion() << std::endl;
ImageType::IndexType desiredStart;
desiredStart.Fill(3);
ImageType::SizeType desiredSize;
desiredSize.Fill(4);
ImageType::RegionType desiredRegion(desiredStart, desiredSize);
std::cout << "desiredRegion: " << desiredRegion << std::endl;
using FilterType = itk::ExtractImageFilter<ImageType, ImageType>;
auto filter = FilterType::New();
filter->SetExtractionRegion(desiredRegion);
filter->SetInput(image);
#if ITK_VERSION_MAJOR >= 4
filter->SetDirectionCollapseToIdentity(); // This is required.
#endif
filter->Update();
ImageType::Pointer output = filter->GetOutput();
output->DisconnectPipeline();
output->FillBuffer(2);
itk::Index<2> index;
index.Fill(5);
std::cout << "new largest region: " << output->GetLargestPossibleRegion() << std::endl;
std::cout << "new: " << (int)output->GetPixel(index) << std::endl;
std::cout << "Original: " << (int)image->GetPixel(index) << std::endl;
return EXIT_SUCCESS;
}
指定起点,遍历图像的指定区域的像素并修改
输出结果
Indices:
Index: [0, 0] value: ?
Index: [1, 0] value: ?
Index: [2, 0] value: ?
Index: [3, 0] value: ?
Index: [4, 0] value: ?
Index: [0, 1] value: ?
Index: [1, 1] value: ?
Index: [2, 1] value: ?
Index: [3, 1] value: ?
Index: [4, 1] value: ?
Index: [0, 2] value: ?
Index: [1, 2] value: ?
Index: [2, 2] value: ?
Index: [3, 2] value: ?
Index: [4, 2] value: ?
Index: [0, 3] value: ?
Index: [1, 3] value: ?
Index: [2, 3] value: ?
Index: [3, 3] value: ?
Index: [4, 3] value: ?
示例代码
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageRegionIteratorWithIndex.h"
int main(int argc, char * argv[])
{
if (argc < 2)
{
std::cerr << "Required: filename" << std::endl;
return EXIT_FAILURE;
}
using ImageType = itk::Image<unsigned char, 2>;
ImageType::Pointer image = itk::ReadImage<ImageType>(argv[1]);
ImageType::SizeType regionSize;
regionSize[0] = 5;
regionSize[1] = 4;
ImageType::IndexType regionIndex;
regionIndex[0] = 0;
regionIndex[1] = 0;
ImageType::RegionType region;
region.SetSize(regionSize);
region.SetIndex(regionIndex);
itk::ImageRegionIteratorWithIndex<ImageType> imageIterator(image, region);
while (!imageIterator.IsAtEnd())
{
std::cout << "Index: " << imageIterator.GetIndex() << " value: " << (int)imageIterator.Get() << std::endl;
// Set the current pixel to white
imageIterator.Set(255);
++imageIterator;
}
return EXIT_SUCCESS;
}