ITK 实例 (二)

ITK 官方网址:https://itk.org/ITK/project/project.html

(二) ITK 图像

(1) 从缓冲区导入图像,利用类“itkImportImageFilter”

下面实例是利用 ITK 生成一幅图像,首先是一幅二维图像,图像的大小是 200 * 200,内容是一个半径为 80 的圆,圆的范围内设置为白色,圆外设置为黑色。利用 SetFileName( ) 函数将生成的图像保存在某一路径中,并设置文件名。

#include "itkImage.h"
#include "itkImportImageFilter.h"
#include "itkImageFileWriter.h"

int main()
{
	using PixelType = unsigned char;
	constexpr unsigned int Dimension = 2;

	using ImageType = itk::Image<PixelType, Dimension>;

	using ImportFilterType = itk::ImportImageFilter<PixelType, Dimension>;
	ImportFilterType::Pointer importFilter = ImportFilterType::New();

	ImportFilterType::SizeType size;

	size[0] = 200;  // size along X
	size[1] = 200;  // szie along Y

	ImportFilterType::IndexType start;
	start.Fill(0);

	ImportFilterType::RegionType region;
	region.SetIndex(start);
	region.SetSize(size);

	importFilter->SetRegion(region);
	const itk::SpacePrecisionType origin[Dimension] = { 0.0,0.0};

	importFilter->SetOrigin(origin);

	const itk::SpacePrecisionType spacing[Dimension] = { 1.0,1.0};
	importFilter->SetSpacing(spacing);

	const unsigned int numberOfPixels = size[0] * size[1];
	auto *localBuffer = new PixelType[numberOfPixels];

	constexpr double radius = 80.0;

	constexpr double radius2 = radius * radius;
	PixelType *it = localBuffer;

		for (unsigned int y = 0; y < size[1]; y++)
		{
			const double dy = static_cast<double>(y) -
				static_cast<double>(size[1]) / 2.0;
			for (unsigned int x = 0; x < size[0]; x++)
			{
				const double dx = static_cast<double>(x) -
					static_cast<double>(size[0]) / 2.0;
				const double d2 = dx*dx + dy*dy;
				*it++ = (d2 < radius2) ? 255 : 0;
			}
		}
	//是否为importFilter所拥有,跟随importFilter释放
	const bool importImageFilterWillOwnTheBuffer = true;

	//将图片buffer设置到importFilter
	importFilter->SetImportPointer(localBuffer, 
		numberOfPixels, importImageFilterWillOwnTheBuffer);

	using WriterType = itk::ImageFileWriter<ImageType>;
	WriterType::Pointer writer = WriterType::New();

        // 要保存的文件位置和文件名
	writer->SetFileName("F:\\ITK_examples\\circle.jpg");  
	writer->SetInput(importFilter->GetOutput());

	try
	{
		writer->Update();
	}
	catch (itk::ExceptionObject &exp)
	{
		std::cerr << "Exception caught!" << std::endl;
		std::cerr << exp << std::endl;
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
	
}

运行结果:

 

 

(2)图像的读写

下面的实例展示了 ITK 中如何读取和另存为图像。

#include <itkImage.h>
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>

int main(int argc, char* argv[])
{
	const unsigned int Dimension = 2;
	typedef itk::RGBPixel<unsigned char> PixelType;
	typedef itk::Image<PixelType, 2> ImageType;
	typedef itk::ImageFileWriter<ImageType> WriterType;
	typedef itk::ImageFileReader<ImageType> ReaderType;
	ReaderType::Pointer reader = ReaderType::New();
	WriterType::Pointer writer = WriterType::New();

	reader->SetFileName("F:\\ITK_examples\\brain.png");  //读取的原始文件
	writer->SetFileName("F:\\ITK_examples\\brain2.png");  //将图片重命名后另存为

	ImageType::Pointer image = reader->GetOutput();
	writer->SetInput(image);

	writer->Update();

	return 0;
}

结果如下:

 

(3)RGB 图像

下面实例展示了如何获取读入的彩色图像的 RGB 值。

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkRGBPixel.h"

int main()
{
	using PixelType = itk::RGBPixel<unsigned char>;
	using ImageType = itk::Image<PixelType, 3>;
	using ReaderType = itk::ImageFileReader<ImageType>;

	ReaderType::Pointer reader = ReaderType::New();
	const char *const filename = 
		"C:\\Camera Roll\\xiaonvhai.jpg";
	reader->SetFileName(filename);
	reader->Update();

	ImageType::Pointer image = reader->GetOutput();
	const ImageType::IndexType pixelIndex = { {25,35,0} };

	PixelType onePixel = image->GetPixel(pixelIndex);

	PixelType::ValueType red = onePixel.GetRed();
	PixelType::ValueType green = onePixel.GetGreen();
	PixelType::ValueType blue = onePixel.GetBlue();

	std::cout << "Pixel values from GetRed, GetGreen, GetBlue: " << std::endl;
	std::cout << "Red = "
		<< itk::NumericTraits<PixelType::ValueType>::PrintType(red) << std::endl;
	std::cout << "Green = "
		<< itk::NumericTraits<PixelType::ValueType>::PrintType(green) << std::endl;
	std::cout << "Blue = "
		<< itk::NumericTraits<PixelType::ValueType>::PrintType(blue) << std::endl;
	
	red = onePixel[0];  //extract red component
	green = onePixel[1];  // extract green component
	blue = onePixel[2];  //extract bllue component

	std::cout << "Pixel Values:" << std::endl;
	std::cout << "Red = "
		<< itk::NumericTraits<PixelType::ValueType>::PrintType(red) << std::endl;
	std::cout << "Green = "
		<< itk::NumericTraits<PixelType::ValueType>::PrintType(green) << std::endl;
	std::cout << "Blue = "
		<< itk::NumericTraits<PixelType::ValueType>::PrintType(blue) << std::endl;

	return EXIT_SUCCESS;
}

读取的图像:

 

读取的结果:

 

(4)矢量图像

下面例子展示如何实例化像素为矢量类型的图像,利用函数 SetPixel( ) 和 GetPixel( )。

#include "itkVector.h"
#include "itkImage.h"

int main(int, char *[])
{
	using PixelType = itk::Vector<float, 3>;
	using ImageType = itk::Image<PixelType, 3>;

	ImageType::Pointer image = ImageType::New();

	const ImageType::IndexType start = { {0,0,0} };  // first index at {X,Y,Z}
	const ImageType::SizeType size = { {200,200,200} };  //Size if {X,Y,Z}

	ImageType::RegionType region;
	region.SetSize(size);
	region.SetIndex(start);

	image->SetRegions(region);
	image->Allocate();

	ImageType::PixelType initialValue;
	initialValue.Fill(0.0);

	image->FillBuffer(initialValue);

	const ImageType::IndexType pixelIndex = { {27,29,5} };  // position {X,Y,Z}

	ImageType::PixelType pixelValue;
	pixelValue[0] = 1.345;  // x component
	pixelValue[1] = 6.841;  // y component
	pixelValue[2] = 3.295;  // z component

	image->SetPixel(pixelIndex, pixelValue);
	ImageType::PixelType value = image->GetPixel(pixelIndex);

	std::cout << value << std::endl;

	return EXIT_SUCCESS;
}

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值