ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Core/Common/ConvertArrayToImage/Code.cxx
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkImage.h"
int
main(int, char *[])
{
using PixelType = unsigned char;
constexpr unsigned int Dimension = 3;
ImportFilterType::Pointer importFilter = ImportFilterType::New();
size[0] = 200; // size along X
size[1] = 200; // size along Y
size[2] = 200; // size along Z
start.Fill(0);
region.SetIndex(start);
region.SetSize(size);
importFilter->SetRegion(region);
double origin[Dimension];
origin[0] = 0.0; // X coordinate
origin[1] = 0.0; // Y coordinate
origin[2] = 0.0; // Z coordinate
importFilter->SetOrigin(origin);
double spacing[Dimension];
spacing[0] = 1.0; // along X direction
spacing[1] = 1.0; // along Y direction
spacing[2] = 1.0; // along Z direction
importFilter->SetSpacing(spacing);
const unsigned int numberOfPixels = size[0] * size[1] * size[2];
auto * localBuffer = new PixelType[numberOfPixels];
constexpr double radius = 80.0;
const double radius2 = radius * radius;
PixelType * it = localBuffer;
for (unsigned int z = 0; z < size[2]; z++)
{
const double dz = static_cast<double>(z) - static_cast<double>(size[2]) / 2.0;
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 + dz * dz;
*it++ = (d2 < radius2) ? 255 : 0;
}
}
}
const bool importImageFilterWillOwnTheBuffer = true;
importFilter->SetImportPointer(localBuffer, numberOfPixels, importImageFilterWillOwnTheBuffer);
using WriterType = itk::ImageFileWriter<ImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("test.png");
writer->SetInput(importFilter->GetOutput());
writer->Update();
return EXIT_SUCCESS;
}
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkImage.h
itk::Index::Fill
void Fill(IndexValueType value)
Definition: itkIndex.h:270
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:87
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itkImportImageFilter.h
itk::ImportImageFilter
Import data from a standard C array into an itk::Image.
Definition: itkImportImageFilter.h:43
itkImageFileWriter.h
itk::ImageRegion::SetIndex
void SetIndex(const IndexType &index)
Definition: itkImageRegion.h:153
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44