Skip to content

ENH: Migrate example RGBImageReadWrite #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/IO/ImageBase/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ compare_to_baseline(

add_example(ConvertImageToAnotherType)

add_example( ReadWriteAnRGBImage )
compare_to_baseline(
EXAMPLE_NAME ReadWriteAnRGBImage
BASELINE_PREFIX OutputBaseline
TEST_IMAGE Output.png
)


#TODO: When example has valid input, uncomment below
#add_example(Create3DFromSeriesOf2D)
#compare_to_baseline(EXAMPLE_NAME Create3DFromSeriesOf2D
Expand Down
37 changes: 37 additions & 0 deletions src/IO/ImageBase/ReadWriteAnRGBImage/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
cmake_minimum_required(VERSION 3.16.3)

project( ReadWriteAnRGBImage )

find_package( ITK REQUIRED )
include( ${ITK_USE_FILE} )

add_executable( ReadWriteAnRGBImage Code.cxx )
target_link_libraries( ReadWriteAnRGBImage ${ITK_LIBRARIES} )

install( TARGETS ReadWriteAnRGBImage
DESTINATION bin/ITKSphinxExamples/IO/ImageBase/
COMPONENT Runtime
)

install( FILES Code.cxx CMakeLists.txt
DESTINATION share/ITKSphinxExamples/Code/IO/ImageBase/ReadWriteAnRGBImage
COMPONENT Code
)

set( input_image ${CMAKE_CURRENT_BINARY_DIR}/peppers.png )
set( output_image Output.png )

enable_testing()
add_test( NAME ReadWriteAnRGBImageTest
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ReadWriteAnRGBImage
${input_image}
${output_image}
)

if(ITK_WRAP_PYTHON)
add_test(NAME ReadWriteAnRGBImageTestPython
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
${input_image}
${output_image}
)
endif()
131 changes: 131 additions & 0 deletions src/IO/ImageBase/ReadWriteAnRGBImage/Code.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*=========================================================================
*
* 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
*
* https://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.
*
*=========================================================================*/

// Software Guide : BeginLatex
//
// RGB images are commonly used for representing data acquired from
// cryogenic sections, optical microscopy and endoscopy. This example
// illustrates how to read and write RGB color images to and from a file.
// This requires the following headers as shown.
//
// \index{itk::RGBPixel!Image}
// \index{RGB!writing Image}
// \index{RGB!reading Image}
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
#include "itkRGBPixel.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
// Software Guide : EndCodeSnippet


int
main(int argc, char ** argv)
{
// Verify the number of parameters in the command line
if (argc < 3)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " inputRGBImageFile outputRGBImageFile "
<< std::endl;
return EXIT_FAILURE;
}


// Software Guide : BeginLatex
//
// The \doxygen{RGBPixel} class is templated over the type used to
// represent each one of the red, green and blue components. A typical
// instantiation of the RGB image class might be as follows.
//
// \index{itk::RGBPixel!Instantiation}
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
using PixelType = itk::RGBPixel<unsigned char>;
using ImageType = itk::Image<PixelType, 2>;
// Software Guide : EndCodeSnippet


// Software Guide : BeginLatex
//
// The image type is used as a template parameter to instantiate
// the reader and writer.
//
// \index{itk::ImageFileReader!RGB Image}
// \index{itk::ImageFileWriter!RGB Image}
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
using ReaderType = itk::ImageFileReader<ImageType>;
using WriterType = itk::ImageFileWriter<ImageType>;

auto reader = ReaderType::New();
auto writer = WriterType::New();
// Software Guide : EndCodeSnippet


const char * inputFilename = argv[1];
const char * outputFilename = argv[2];


// Software Guide : BeginLatex
//
// The filenames of the input and output files must be provided to the
// reader and writer respectively.
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
reader->SetFileName(inputFilename);
writer->SetFileName(outputFilename);
// Software Guide : EndCodeSnippet


ImageType::Pointer image = reader->GetOutput();
writer->SetInput(image);


// Software Guide : BeginLatex
//
// Finally, execution of the pipeline can be triggered by invoking the
// Update() method in the writer.
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
writer->Update();
// Software Guide : EndCodeSnippet

// Software Guide : BeginLatex
//
// You may have noticed that apart from the declaration of the
// \code{PixelType} there is nothing in this code specific to RGB
// images. All the actions required to support color images are implemented
// internally in the \doxygen{ImageIO} objects.
//
// Software Guide : EndLatex


return EXIT_SUCCESS;
}
47 changes: 47 additions & 0 deletions src/IO/ImageBase/ReadWriteAnRGBImage/Code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python

# 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.

import itk
import argparse

parser = argparse.ArgumentParser(description="Write An Image.")
parser.add_argument("output_image", nargs="?")
args = parser.parse_args()

if args.output_image:
output_filename = args.output_image
else:
output_filename = "testPython.png"

pixel_type = itk.UC
dimension = 2
image_type = itk.Image[pixel_type, dimension]

start = itk.Index[dimension]()
start.Fill(0)

size = itk.Size[dimension]()
size[0] = 200
size[1] = 300

region = itk.ImageRegion[dimension]()
region.SetIndex(start)
region.SetSize(size)

image = image_type.New(Regions=region)
image.Allocate()

itk.imwrite(image, output_filename)
50 changes: 50 additions & 0 deletions src/IO/ImageBase/ReadWriteAnRGBImage/Documentation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
:name: WriteAnImage

Write an Image
==============

.. index::
single: itk::Image
single: itk::ImageFileReader
single: itk::ImageFileWriter
single: itk::RGBPixelImage


Synopsis
--------

RGB images are commonly used for representing data acquired from
cryogenic sections, optical microscopy and endoscopy. This example
illustrates how to read and write RGB color images to and from a file.


Results
-------
.. figure:: WriteAnImage.png
:scale: 70%
:alt: test.png

test.png


Code
----

Python
......

.. literalinclude:: Code.py
:language: python
:lines: 1, 16-

C++
...

.. literalinclude:: Code.cxx
:lines: 18-


Classes demonstrated
--------------------

.. breathelink:: itk::RGBPixelImage
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4e1e0621f19c2613d5a4c6774fe8933af4b8abad1afdb1d1b5918eacb0755340dea5fdc6135f52dcf865cb8ee8c7ba61096e4f707de64695c6c351e4cbce3f38
1 change: 1 addition & 0 deletions src/IO/ImageBase/ReadWriteAnRGBImage/peppers.png.sha512
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4e1e0621f19c2613d5a4c6774fe8933af4b8abad1afdb1d1b5918eacb0755340dea5fdc6135f52dcf865cb8ee8c7ba61096e4f707de64695c6c351e4cbce3f38