Hello World
Overview
A “Hello World” example for SimpleITK. The example constructs a 128x128 greyscale image, draws a smiley face made of Gaussian blobs, and calls the Show function to display the image with Fiji.
Sample Output
Running the python code produces this image:
Code
using System;
using itk.simple;
namespace itk.simple.examples {
class HelloWorld {
static void Main(string[] args) {
try {
// Create an image
PixelIDValueEnum pixelType = PixelIDValueEnum.sitkUInt8;
VectorUInt32 imageSize = new VectorUInt32( new uint[] { 128, 128 } );
Image image = new Image( imageSize, pixelType );
// Create a face image
VectorDouble faceSize = new VectorDouble( new double[] { 64, 64 } );
VectorDouble faceCenter = new VectorDouble( new double[] { 64, 64 } );
Image face = SimpleITK.GaussianSource( pixelType, imageSize, faceSize, faceCenter );
// Create eye images
VectorDouble eyeSize = new VectorDouble( new double[] { 5, 5 } );
VectorDouble eye1Center = new VectorDouble( new double[] { 48, 48 } );
VectorDouble eye2Center = new VectorDouble( new double[] { 80, 48 } );
Image eye1 = SimpleITK.GaussianSource( pixelType, imageSize, eyeSize, eye1Center, 150 );
Image eye2 = SimpleITK.GaussianSource( pixelType, imageSize, eyeSize, eye2Center, 150 );
// Apply the eyes to the face
face = SimpleITK.Subtract( face, eye1 );
face = SimpleITK.Subtract( face, eye2 );
face = SimpleITK.BinaryThreshold( face, 200, 255, 255 );
// Create the mouth
VectorDouble mouthRadii = new VectorDouble( new double[] { 30, 20 } );
VectorDouble mouthCenter = new VectorDouble( new double[] { 64, 76 } );
Image mouth = SimpleITK.GaussianSource( pixelType, imageSize, mouthRadii, mouthCenter );
mouth = SimpleITK.BinaryThreshold( mouth, 200, 255, 255 );
mouth = SimpleITK.Subtract( 255, mouth );
// Paste the mouth onto the face
VectorUInt32 mouthSize = new VectorUInt32( new uint[] { 64, 18 } );
VectorInt32 mouthLoc = new VectorInt32( new int[] { 32, 76 } );
face = SimpleITK.Paste( face, mouth, mouthSize, mouthLoc, mouthLoc );
// Apply the face to the original image
image = SimpleITK.Add( image, face );
// Display the results
if (Environment.GetEnvironmentVariable("SITK_NOSHOW") == null)
SimpleITK.Show( image, "Hello World: CSharp", true );
} catch (Exception ex) {
Console.WriteLine(ex);
}
}
}
}
// This one header will include all SimpleITK filters and external
// objects.
#include <SimpleITK.h>
#include <sitkImageOperators.h>
#include <iostream>
#include <stdlib.h>
// create convenient namespace alias
namespace sitk = itk::simple;
int
main(int argc, char * argv[])
{
sitk::PixelIDValueEnum pixelType = sitk::sitkUInt8;
std::vector<unsigned int> imageSize(2, 128);
// Create an image
sitk::Image image(imageSize, pixelType);
// Create a face image
std::vector<double> faceSize(2, 64.0);
std::vector<double> faceCenter(2, 64.0);
;
sitk::Image face = sitk::GaussianSource(pixelType, imageSize, faceSize, faceCenter);
// Create eye images
std::vector<double> eyeSize(2, 5.0);
std::vector<double> eye1Center(2, 48.0);
std::vector<double> eye2Center = { 80.0, 48.0 };
sitk::Image eye1 = sitk::GaussianSource(pixelType, imageSize, eyeSize, eye1Center, 150);
sitk::Image eye2 = sitk::GaussianSource(pixelType, imageSize, eyeSize, eye2Center, 150);
// Apply the eyes to the face
face = face - eye1 - eye2;
face = sitk::BinaryThreshold(face, 200, 255, 255);
// Create the mouth
std::vector<double> mouthRadii = { 30.0, 20.0 };
std::vector<double> mouthCenter = { 64.0, 76.0 };
sitk::Image mouth =
255 - sitk::BinaryThreshold(sitk::GaussianSource(pixelType, imageSize, mouthRadii, mouthCenter), 200, 255, 255);
// Paste the mouth onto the face
std::vector<unsigned int> mouthSize = { 64, 18 };
std::vector<int> mouthLoc = { 32, 76 };
face = sitk::Paste(face, mouth, mouthSize, mouthLoc, mouthLoc);
// Apply the face to the original image
image = image + face;
// Display the results
if (getenv("SITK_NOSHOW") == NULL)
sitk::Show(image, "Hello World: C++", true);
}
import org.itk.simple.*;
class HelloWorld {
public static void main(String argv[]) {
// Create an image
PixelIDValueEnum pixelType = PixelIDValueEnum.sitkUInt8;
long[] isize = {128, 128};
VectorUInt32 imageSize = new VectorUInt32( isize );
Image image = new Image( imageSize, pixelType );
// Create a face image
double[] fsize = {64., 64.};
VectorDouble faceSize = new VectorDouble( fsize );
double[] fcenter = {64., 64.};
VectorDouble faceCenter = new VectorDouble( fcenter );
Image face = SimpleITK.gaussianSource( pixelType, imageSize, faceSize, faceCenter );
// Create eye images
double[] esize = {5., 5.};
VectorDouble eyeSize = new VectorDouble( esize );
double[] e1center = {48., 48.};
VectorDouble eye1Center = new VectorDouble( e1center );
double[] e2center = {80., 48.};
VectorDouble eye2Center = new VectorDouble( e2center );
Image eye1 = SimpleITK.gaussianSource( pixelType, imageSize, eyeSize, eye1Center, 150 );
Image eye2 = SimpleITK.gaussianSource( pixelType, imageSize, eyeSize, eye2Center, 150 );
// Apply the eyes to the face
face = SimpleITK.subtract( face, eye1 );
face = SimpleITK.subtract( face, eye2 );
face = SimpleITK.binaryThreshold( face, 200., 255., (short) 255 );
// Create the mouth
double[] mradius = {30., 20.};
VectorDouble mouthRadius = new VectorDouble( mradius );
double[] mcenter = {64., 76.};
VectorDouble mouthCenter = new VectorDouble( mcenter );
Image mouth = SimpleITK.gaussianSource( pixelType, imageSize, mouthRadius, mouthCenter );
mouth = SimpleITK.binaryThreshold( mouth, 200, 255, (short) 255 );
mouth = SimpleITK.subtract( 255, mouth );
// Paste the mouth onto the face
long[] msize = {64, 18};
VectorUInt32 mouthSize = new VectorUInt32( msize );
int[] mloc = {32, 76};
VectorInt32 mouthLoc = new VectorInt32( mloc );
face = SimpleITK.paste( face, mouth, mouthSize, mouthLoc, mouthLoc );
// Apply the face to the original image
image = SimpleITK.add( image, face );
// Display the results
if(System.getenv("SITK_NOSHOW") == null) {
SimpleITK.show( image, "Hello World: Java", true );
}
}
}
require "SimpleITK"
local sitk = SimpleITK
-- Create an image
pixelType = sitk.sitkUInt8
imageSize = sitk.VectorUInt32()
imageSize:push_back( 128 )
imageSize:push_back( 128 )
image = sitk.Image( imageSize, sitk.sitkUInt8 )
-- Create a face image
faceSize = sitk.VectorDouble()
faceSize:push_back( 64 )
faceSize:push_back( 64 )
faceCenter = sitk.VectorDouble()
faceCenter:push_back( 64 )
faceCenter:push_back( 64 )
face = sitk.GaussianSource( pixelType, imageSize, faceSize, faceCenter )
-- Create eye images
eyeSize = sitk.VectorDouble()
eyeSize:push_back( 5 )
eyeSize:push_back( 5 )
eye1Center = sitk.VectorDouble()
eye1Center:push_back( 48 )
eye1Center:push_back( 48 )
eye2Center = sitk.VectorDouble()
eye2Center:push_back( 80 )
eye2Center:push_back( 48 )
eye1 = sitk.GaussianSource( pixelType, imageSize, eyeSize, eye1Center, 150 )
eye2 = sitk.GaussianSource( pixelType, imageSize, eyeSize, eye2Center, 150 )
-- Apply the eyes to the face
face = sitk.Subtract( face, eye1 )
face = sitk.Subtract( face, eye2 )
face = sitk.BinaryThreshold( face, 200, 255, 255 )
-- Create the mouth
mouthRadii = sitk.VectorDouble()
mouthRadii:push_back( 30.0 )
mouthRadii:push_back( 20.0 )
mouthCenter = sitk.VectorDouble()
mouthCenter:push_back( 64.0 )
mouthCenter:push_back( 76.0 )
mouth = sitk.GaussianSource( pixelType, imageSize, mouthRadii, mouthCenter )
mouth = sitk.BinaryThreshold( mouth, 200, 255, 255 )
mouth = sitk.Subtract( 255, mouth )
-- Paste the mouth onto the face
mouthSize = sitk.VectorUInt32()
mouthSize:push_back( 64 )
mouthSize:push_back( 18 )
mouthLoc = sitk.VectorInt32()
mouthLoc:push_back( 32 )
mouthLoc:push_back( 76 )
face = sitk.Paste( face, mouth, mouthSize, mouthLoc, mouthLoc );
-- Apply the face to the original image
image = sitk.Add( image, face )
-- Display the results
if os.getenv("SITK_NOSHOW") == nil then
sitk.Show( image, "Hello World: Lua", true )
end
import os
import sys
import SimpleITK as sitk
def main(_):
""" Generate a simple image and display it using the SimpleITK Show function. """
# Create an image
pixelType = sitk.sitkUInt8
imageSize = [128, 128]
image = sitk.Image(imageSize, pixelType)
# Create a face image
faceSize = [64, 64]
faceCenter = [64, 64]
face = sitk.GaussianSource(pixelType, imageSize, faceSize, faceCenter)
# Create eye images
eyeSize = [5, 5]
eye1Center = [48, 48]
eye2Center = [80, 48]
eye1 = sitk.GaussianSource(pixelType, imageSize, eyeSize, eye1Center, 150)
eye2 = sitk.GaussianSource(pixelType, imageSize, eyeSize, eye2Center, 150)
# Apply the eyes to the face
face = face - eye1 - eye2
face = sitk.BinaryThreshold(face, 200, 255, 255)
# Create the mouth
mouthRadii = [30, 20]
mouthCenter = [64, 76]
mouth = 255 - sitk.BinaryThreshold(
sitk.GaussianSource(pixelType, imageSize, mouthRadii, mouthCenter),
200,
255,
255,
)
# Paste the mouth into the face
mouthSize = [64, 18]
mouthLoc = [32, 76]
face = sitk.Paste(face, mouth, mouthSize, mouthLoc, mouthLoc)
# Apply the face to the original image
image = image + face
return {"output_image": image}
library(SimpleITK)
# Create an image
pixelType <- 'sitkUInt8'
imageSize <- c( 128, 128 )
image <- Image( imageSize, pixelType )
# Create a face image
faceSize <- c( 64, 64)
faceCenter <- c( 64, 64)
face <- GaussianSource( pixelType, imageSize, faceSize, faceCenter )
# Create eye images
eyeSize <- c( 5, 5 )
eye2Center <- c( 48, 48 )
eye1Center <- c( 80, 48 )
eye1 <- GaussianSource( pixelType, imageSize, eyeSize, eye1Center, 150 )
eye2 <- GaussianSource( pixelType, imageSize, eyeSize, eye2Center, 150 )
# Apply the eyes to the face
face <- face - eye1 - eye2
face <- BinaryThreshold( face, 200, 255, 255 )
# Create the mouth
mouthRadii <- c( 30, 20 )
mouthCenter <- c( 64, 76 )
mouth <- 255 - BinaryThreshold( GaussianSource( pixelType, imageSize, mouthRadii, mouthCenter ),
200, 255, 255 )
# Paste mouth onto the face
mouthSize <- c( 64, 18 )
mouthLoc <- c( 32, 76 )
face = Paste( face, mouth, mouthSize, mouthLoc, mouthLoc )
# Apply the face to the original image
image <- image + face
# Display the results
if(Sys.getenv("SITK_NOSHOW") == "") {
Show(image, "Hello World: R", debugOn=TRUE)
}
require 'simpleitk'
# Create an image
pixelType = Simpleitk::SitkUInt8
imageSize = Simpleitk::VectorUInt32.new
imageSize << 128
imageSize << 128
image = Simpleitk::Image.new( imageSize, pixelType )
# Create a face image
faceSize = Simpleitk::VectorDouble.new
faceSize << 64
faceSize << 64
faceCenter = Simpleitk::VectorDouble.new
faceCenter << 64
faceCenter << 64
face = Simpleitk::gaussian_source( pixelType, imageSize, faceSize, faceCenter )
# Create eye images
eyeSize = Simpleitk::VectorDouble.new
eyeSize << 5
eyeSize << 5
eye1Center = Simpleitk::VectorDouble.new
eye1Center << 48
eye1Center << 48
eye2Center = Simpleitk::VectorDouble.new
eye2Center << 80
eye2Center << 48
eye1 = Simpleitk::gaussian_source( pixelType, imageSize, eyeSize, eye1Center, 150 )
eye2 = Simpleitk::gaussian_source( pixelType, imageSize, eyeSize, eye2Center, 150 )
# Apply the eyes to the face
face = Simpleitk.subtract( face, eye1 )
face = Simpleitk.subtract( face, eye2 )
face = Simpleitk.binary_threshold( face, 200, 255, 255 );
# Create the mouth
mouthRadii = Simpleitk::VectorDouble.new
mouthRadii << 30
mouthRadii << 20
mouthCenter = Simpleitk::VectorDouble.new
mouthCenter << 64
mouthCenter << 76
mouth = Simpleitk::gaussian_source( pixelType, imageSize, mouthRadii, mouthCenter )
mouth = Simpleitk::binary_threshold( mouth, 200, 255, 255 )
mouth = Simpleitk::subtract( 255, mouth )
# Paste the mouth onto the face
mouthSize = Simpleitk::VectorUInt32.new
mouthSize << 64
mouthSize << 18
mouthLoc = Simpleitk::VectorInt32.new
mouthLoc << 32
mouthLoc << 76
face = Simpleitk::paste( face, mouth, mouthSize, mouthLoc, mouthLoc )
# Apply the face to the original image
image = Simpleitk.add( image, face )
if (ENV["SITK_NOSHOW"] == "")
Simpleitk.show( image, "Hello World: Ruby", true )
end
# Create an image
set pixelType $sitkUInt16
set imageSize { 128 128 }
Image img $imageSize $pixelType
# Create a face
set faceSize { 64 64 }
set faceCenter { 64 64 }
set face [ GaussianSource $pixelType $imageSize $faceSize $faceCenter ]
# Create eye images
set eyeSize { 5 5 }
set eye1Center { 48 48 }
set eye2Center { 80 48 }
set eye1 [ GaussianSource $pixelType $imageSize $eyeSize $eye1Center 150 ]
set eye2 [ GaussianSource $pixelType $imageSize $eyeSize $eye2Center 150 ]
# Apply the eyes to the face
set face [ Subtract $face $eye1 ]
set face [ Subtract $face $eye2 ]
set face [ BinaryThreshold $face 200 255 255 ]
# Create the mouth
set mouthRadii { 30 20 }
set mouthCenter { 64 76 }
set mouth [ GaussianSource $pixelType $imageSize $mouthRadii $mouthCenter ]
set mouth [ Subtract 255 [ BinaryThreshold $mouth 200 255 255 ] ]
# Paste the mouth onto the face
set mouthSize { 64 18 }
set mouthLoc { 32 76 }
set face [ Paste $face $mouth $mouthSize $mouthLoc $mouthLoc ]
# Apply the face to the original image
set img $face
# Display the results
if { ![info exists ::env(SITK_NOSHOW)] } {
Show $img "Hello World: TCL" 1
}