将图片buffer写入到image

Import Pixel Buffer into an Image
官方demo:https://itk.org/ITKExamples/src/Core/Common/ImportPixelBufferIntoAnImage/Documentation.html

效果图:
这里写图片描述

源码整理:

    //输出文件名称
    const char* outputFileName = "../Data/imagedata/imagetest.jpg";
   //图片buffer类型
    using PixelType = unsigned char;
    constexpr unsigned int Dimension = 2;

    using ImageType = itk::Image< PixelType, Dimension >;
    using ImportFilterType = itk::ImportImageFilter< PixelType, Dimension >;
    //将图片buffer写入到ImportFilterType
    ImportFilterType::Pointer importFilter = ImportFilterType::New();
    //指定图片的长宽
    ImportFilterType::SizeType  size;
    size[0]  = 200;  // size along X
    size[1]  = 200;  // size 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];

    //生成图片buffer区域
    auto * localBuffer = new PixelType[ numberOfPixels ];
    constexpr double radius = 80.0;
    const 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( outputFileName );
    writer->SetInput(  importFilter->GetOutput()  );
    typedef itk::JPEGImageIO ImageIOType;
    ImageIOType::Pointer jpegImageIO = ImageIOType::New();
    writer->SetImageIO(jpegImageIO);
    try
      {
      writer->Update();
      }
    catch( itk::ExceptionObject & exp )
      {
      std::cerr << "Exception caught !" << std::endl;
      std::cerr << exp << std::endl;
      return EXIT_FAILURE;
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力减肥的小胖子5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值