Re-posted from: https://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.
# 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");
using Images, TestImages, Colors
methods(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.
# 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")
# for this image, we see the internal format is stored as RGB
summary(img)
# we can extract a single pixel, and it gets automatically displayed
img[1]
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.
img[0, 0]
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!
@which []
And the Colors.jl package lets us access either individual color channels either by their indexers or the methods:
c = img[10];
println(summary(img));
println(c.r, " ", c.g, " ", c.b);
println(red(c), " ", green(c), " ", blue(c));
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!
# 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));
# load an image from disk
img = load("skyline.jpg")
Any comments for me?