Getting started with images

By: Michele Pratusevich

Re-posted from: http://learningjulia.com/2017/02/20/getting-started-with-images.html

To get started with Julia, I wanted to learn more about the ecosystem of image support in Julia. Until now, my tools of choice for manipulating images was numpy in Python and OpenCV. Instead, Julia has the JuliaImages ecosystem of packages for image support.

Now I’ve included the notebook I used to install and play with basic images:



Resource for images and relevant packages in Julia: http://juliaimages.github.io/latest/

You only have to run the Pkg.add() lines once before you do the using statement for that package.

In [1]:
# install Images for the basic images, TestImages for some test images
# Pkg.add("Images");
# Pkg.add("TestImages");
# FileIO for loading images from disk
# Pkg.add("FileIO");
# using Colors package to access the color spaces
# Pkg.add("Colors");
In [2]:
using Images, TestImages, Colors
In [3]:
methods(testimage)
Out[3]:
1 method for generic function testimage:

Right now there is no way to read the source code into the REPL, but at least it is a requested feature: https://github.com/JuliaLang/julia/issues/2625

The names of all the files that are available in the TestImages packages is then found by looking directly at the source code link above.

In [4]:
# show an image directly in the notebook
# when I first ran this cell to load the "toucan" image,
# I got a message in the noteobok REPL that asked if
# ImageMagick (the Julia package) should be installed to parse .png files.
# I said yes and everything is going great
img = testimage("lighthouse")
Out[4]:
In [5]:
# for this image, we see the internal format is stored as RGB
summary(img)
Out[5]:
"512×768 Array{RGB{N0f8},2}"
In [6]:
# we can extract a single pixel, and it gets automatically displayed
img[1]
Out[6]:


Turns out, one of the annoying things about Julia is that everything is 1-indexed. So if you try to access a 0-index pixel in the image, you will get an error.

In [7]:
img[0, 0]
Out[7]:
BoundsError: attempt to access 512×768 Array{RGB{N0f8},2} at index [0,0]

 in getindex(::Array{ColorTypes.RGB{FixedPointNumbers.Normed{UInt8,8}},2}, ::Int64, ::Int64) at ./array.jl:387

And directly from the docs: “numbering goes down columns, and then wraps around at the top of the next column, because Julia arrays are stored in “column major” order where the fastest dimension is the first dimension”.

All Images are actually AbstractArrays, so you can do indexing with [] notation (just like numpy or MATLAB), just remember that it is one-indexed!

In [8]:
@which []
Out[8]:

And the Colors.jl package lets us access either individual color channels either by their indexers or the methods:

In [9]:
c = img[10];
println(summary(img));
println(c.r, " ", c.g, " ", c.b);
println(red(c), " ", green(c), " ", blue(c));
Out[9]:
512×768 Array{RGB{N0f8},2}
0.349N0f8 0.49N0f8 0.62N0f8
0.349N0f8 0.49N0f8 0.62N0f8

Can convert from RGB to a different color space (like BGR). But then if you use the red, green, and blue accessors, you get the same values as when the image is in RGB format!

In [10]:
# use the casting BGR function to convert each pixel from the
# image into the BGR color space
img_bgr = BGR.(img);
println(summary(img_bgr));
c = img_bgr[10];
println(c.r, " ", c.g, " ", c.b);
println(red(c), " ", green(c), " ", blue(c));
Out[10]:
512×768 Array{BGR{N0f8},2}
0.349N0f8 0.49N0f8 0.62N0f8
0.349N0f8 0.49N0f8 0.62N0f8
In [11]:
# load an image from disk
img = load("skyline.jpg")
Out[11]:


Any comments for me?