当前位置:   article > 正文

将图片buffer写入到image_图片 buffer

图片 buffer

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;
      }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/145139
推荐阅读
相关标签
  

闽ICP备14008679号