1 #include "itkImage.h" 2 #include "itkImageFileReader.h" 3 //为了使用 itk::RGBPixel 类,包含头文件 4 #include "itkRGBPixel.h" 5 //在 ITK 中如何表示 RGB 图像。 6 int main( int , char * argv[] ) 7 { 8 //RGBPixeld 类的使用是基于用来代表红、绿和蓝的像素成分的类型之上的 9 //定义RGBPixel类型的PixelType对象 10 typedef itk::RGBPixel< unsigned char > PixelType; 11 //然后将这个类型PixelType对象作为图像中的像素模板参数,得到ImageType对象 12 typedef itk::Image< PixelType, 3 > ImageType; 13 //使用itk::ImageFileReader 对象从文件中读取图像 14 typedef itk::ImageFileReader< ImageType > ReaderType; 15 //实例化ReaderType的读取对象reader 16 ReaderType::Pointer reader = ReaderType::New(); 17 //设置RGB图像索引 18 const char * const filename = "789.jpg"; 19 //图像读取 20 reader->SetFileName( filename ); 21 reader->Update(); 22 //reader读取到的图像数据输出到image 23 ImageType::Pointer image = reader->GetOutput(); 24 const ImageType::IndexType pixelIndex = {{25,35,0}}; 25 26 //使用 RGBPixel 类提供的方法来执行对像素色彩成分的访问 27 PixelType onePixel = image->GetPixel( pixelIndex ); 28 PixelType::ValueType red = onePixel.GetRed();//提取红色部分 29 PixelType::ValueType green = onePixel.GetGreen();//提取绿色部分 30 PixelType::ValueType blue = onePixel.GetBlue();//提取蓝色部分 31 // Software Guide : EndCodeSnippet 32 std::cout << "method1" << std::endl; 33 std::cout << "Pixel values from GetRed,GetGreen,GetBlue:" << std::endl; 34 std::cout << "Red = " 35 << itk::NumericTraits<PixelType::ValueType>::PrintType(red) 36 << std::endl; 37 std::cout << "Green = " 38 << itk::NumericTraits<PixelType::ValueType>::PrintType(green) 39 << std::endl; 40 std::cout << "Blue = " 41 << itk::NumericTraits<PixelType::ValueType>::PrintType(blue) 42 << std::endl << std::endl; 43 44 45 //由于 itk::RGBPixel 从 itk::FixedArray 类继承了 [ ] 操作 46 //以下方法也可执行对像素色彩成分的访问 47 red = onePixel[0]; // extract Red component 48 green = onePixel[1]; // extract Green component 49 blue = onePixel[2]; // extract Blue component 50 51 std::cout << "method2" << std::endl; 52 std::cout << "Pixel values:" << std::endl; 53 std::cout << "Red = " 54 << itk::NumericTraits<PixelType::ValueType>::PrintType(red) 55 << std::endl; 56 std::cout << "Green = " 57 << itk::NumericTraits<PixelType::ValueType>::PrintType(green) 58 << std::endl; 59 std::cout << "Blue = " 60 << itk::NumericTraits<PixelType::ValueType>::PrintType(blue) 61 << std::endl; 62 return EXIT_SUCCESS; 63 }