ITK 实例4 访问像素数据

发布时间 2023-08-16 14:14:08作者: 一杯清酒邀明月
 1 #include "itkImage.h"
 2 //这个例子阐述了 SetPixel( )和 GetPixel( )方法的用法
 3 //可以直接访问图像中包含的像素数据
 4 int main(int, char *[])
 5 {
 6   typedef itk::Image< unsigned short, 3 > ImageType;
 7  
 8   ImageType::Pointer image = ImageType::New();
 9  
10   const ImageType::SizeType  size  = {{ 200, 200, 200}}; //Size along {X,Y,Z}
11   const ImageType::IndexType start = {{ 0, 0, 0 }}; // First index on {X,Y,Z}
12  
13   ImageType::RegionType region;
14   region.SetSize( size );
15   region.SetIndex( start );
16  
17   // Pixel data is allocated
18   image->SetRegions( region );
19   image->Allocate(true); // initialize buffer to zero
20   //对 index 类型实例的声明并进行初始化
21   const ImageType::IndexType pixelIndex = {{27,29,37}}; // Position of {X,Y,Z}
22   //GetPixel(pixelIndex)方法将可得到pixelIndex处的像素值pixelValue
23   ImageType::PixelType   pixelValue = image->GetPixel( pixelIndex );
24  
25   std::cout << "pixelIndex处的像素值:"<<pixelValue << std::endl;//输出pixelIndex索引处的像素值pixelValue
26   //SetPixel( )方法可设定像素值,将pixelIndex索引处的像素值赋值为pixelValue+1
27   image->SetPixel(   pixelIndex,   pixelValue+12  );
28  
29   ImageType::PixelType   pixelValue1 = image->GetPixel(pixelIndex);//获取更改后的像素值
30   std::cout << "更改后pixelIndex处的像素值:" << pixelValue1 << std::endl;//输出pixelIndex索引处的像素值
31  
32   return EXIT_SUCCESS;
33 }

 图像基础:

在这个图表中,圆卷表示像素的中心;

像素间距是像素中心之间的距离;如本图表中,X方向像素间距为20,Y方向上像素间距为30;

图像原点是图像中第一个像素的坐标;如图中原点坐标(60,70);

一个像素就是含有数据值的像素中心周围的矩形区域;

图像大小指的是图像长宽方向像素个数相乘;如图中图像大小为7*6=42(像素);