实例4 ITK访问图像像素数据
#include "itkImage.h"
//这个例子阐述了 SetPixel( )和 GetPixel( )方法的用法
//可以直接访问图像中包含的像素数据
int main(int, char *[])
{
typedef itk::Image< unsigned short, 3 > ImageType;
ImageType::Pointer image = ImageType::New();
const ImageType::SizeType size = {{ 200, 200, 200}}; //Size along {X,Y,Z}
const ImageType::IndexType start = {{ 0, 0, 0 }}; // First index on {X,Y,Z}
ImageType::RegionType region;
region.SetSize( size );
region.SetIndex( start );
// Pixel data is allocated
image->SetRegions( region );
image->Allocate(true); // initialize buffer to zero
//对 index 类型实例的声明并进行初始化
const ImageType::IndexType pixelIndex = {{27,29,37}}; // Position of {X,Y,Z}
//GetPixel(pixelIndex)方法将可得到pixelIndex处的像素值pixelValue
ImageType::PixelType pixelValue = image->GetPixel( pixelIndex );
std::cout << "pixelIndex处的像素值:"<<pixelValue << std::endl;//输出pixelIndex索引处的像素值pixelValue
//SetPixel( )方法可设定像素值,将pixelIndex索引处的像素值赋值为pixelValue+1
image->SetPixel( pixelIndex, pixelValue+12 );
ImageType::PixelType pixelValue1 = image->GetPixel(pixelIndex);//获取更改后的像素值
std::cout << "更改后pixelIndex处的像素值:" << pixelValue1 << std::endl;//输出pixelIndex索引处的像素值
return EXIT_SUCCESS;
}
图像基础:
在这个图表中,圆卷表示像素的中心;
像素间距是像素中心之间的距离;如本图表中,X方向像素间距为20,Y方向上像素间距为30;
图像原点是图像中第一个像素的坐标;如图中原点坐标(60,70);
一个像素就是含有数据值的像素中心周围的矩形区域;
图像大小指的是图像长宽方向像素个数相乘;如图中图像大小为7*6=42(像素);
ITK系列目录:
注:例程配套素材见系列目录