Getting Started with JuliaImages
This chapter is all about introducing the JuliaImages collection. JuliaImages is a collection of different packages that are used for image processing. We will look into the Images.jl and ImagesMetadata.jl packages, load and preview images from various sources, read metadata, resize and scale images, create thumbnails, and save them back to disk in a different format.
In this chapter, we will cover the following topics:
- Setting up Julia
- Reading images from various sources
- Saving images in different formats
- Cropping, scaling, and resizing images
- Rotating images
- Using test images
Technical requirements
Users are required to have Julia v. 1.0 or above installed. Julia can be downloaded from the official page at https://julialang.org/downloads/.
You can confirm your version number by typing VERSION into the Julia console or REPL, as shown in the following code snippet:
julia> VERSION
v"0.7.0-DEV.4465"
You should also clone or download a GitHub repository containing source code and sample images:
https://github.com/PacktPublishing/Hands-On-Computer-Vision-with-Julia
This can be done by going to the GitHub page and pressing either the Clone or Download button in the top right corner.
Setting up your Julia
Before we start working with our images, we need to ensure that our Julia environment has all the required prerequisites so that we can complete the chapter. We already confirmed that our Julia setup is correct, so let's proceed with installing the most essential packages from the JuliaImages collection.
Installing packages
The most essential packages from the JuliaImages collection are the following:
- Images.jl
- ImageMetadata.jl
- ImageView.jl
- TestImages.jl
These packages are all you need to perform simple tasks, and most regular users should be fine with the setup.
Run the following commands in the Julia REPL to get them installed and configured. If you have not used Julia before, it is very likely that these commands will install additional dependencies:
using Pkg
Pkg.add("Images")
Pkg.add("ImageMetadata")
Pkg.add("ImageView")
Pkg.add("TestImages")
Pkg.update()
The moment installation completes, it is advised that you verify whether the packages can be loaded. This is done by merely importing them into the current environment, waiting for new packages to compile, and seeing whether the command succeeds:
julia> using Images, ImageMetadata, TestImages, ImageView
There is a small chance that the preceding command will fail with an exception message stating that one of the packages does not exist:
ERROR: ArgumentError: Module XXX not found in current path.
Run `Pkg.add("XXX")` to install the TestImages package.
Please follow the instructions to install a missing package and repeat the steps from this chapter.
Reading images
There are multiple different sources for your images. Let's look into three of the most popular methods:
- Reading images from disk
- Reading images from URL
- Reading multiple images in a folder
Start by loading the Images package and verifying your current working directory using pwd:
julia> using Images
julia> pwd()
"/Users/dc/reps/packt-julia"
If pwd does not correspond to your project folder, you have two options:
- Start Julia from a folder that does correspond
- Use the cd function to change it
The cd function accepts a single argument—the local path. An example of using the cd function would be as follows:
cd("~/repositories/julia-hands-on") # Unix-like systems
cd("C:\\repositories\\julia-hands-on") # Windows users
When you are all set, you can proceed to load your first image.
Reading a single image from disk
Reading an image from disk is simple and is done by calling the load function. The load function accepts a single argument—the image path—and returns an image object. The following code assigns an image to a custom variable.
We will be using the sample-images folder from the GitHub repository. You are required to have a functioning project folder when running the following code:
using Images
sample_image_path = "sample-images/cats-3061372_640.jpg";
sample_image = nothing
if isfile(sample_image_path)
sample_image = load(sample_image_path);
else
info("ERROR: Image not found!")
end
A typical problem users face is using the wrong path. The preceding code example implements a check to see whether the file exists and prints an error if it does not.
Reading a single image from a URL
The process of reading an image from a URL is first getting it downloaded to disk using the download function and then processing it, as in the preceding section:
image_url = "https://cdn.pixabay.com/photo/2018/01/04/18/58/cats-3061372_640.jpg?attachment"
downloaded_image_path = download(image_url)
downloaded_image = load(downloaded_image_path)
Depending on your project, it might make sense to download the file to a custom folder. You can define a download location by sending it as a second parameter to the download function:
image_url = "https://cdn.pixabay.com/photo/2018/01/04/18/58/cats-3061372_640.jpg?attachment"
downloaded_image_path = download(image_url, 'custom_image_name.jpg')
downloaded_image = load(downloaded_image_path)
Reading images in a folder
Loading files from a directory is a common use case. This is done by identifying a list of files in a directory, filtering the necessary files, and then executing a set of operations for each and every one of them.
We will be using the sample-images folder from the GitHub repository. You are required to have a functioning project folder when running the following example:
using Images
directory_path = "sample-images";
directory_files = readdir(directory_path);
directory_images = filter(x -> ismatch(r"\.(jpg|png|gif){1}$"i, x), directory_files);
for image_name in directory_images
image_path = joinpath(directory_path, image_name);
image = load(image_path);
# other operations
end
This example introduces a number of new functions and techniques, which are explained as follows:
- We use readdir from the Julia Base to read all the files names in a directory
- We use filter from the Julia Base, as well as custom regular expressions to find files ending with .jpg, .png, or .gif, both in lower and upper-case
- We use the for loop to iterate over filtered names
- We use joinpath from the Julia Base to combine the directory name and filename so that we have a full path to the image
- We use the load function (which you have already learned about) to load the image
Please be aware that readdir returns filenames. This is the reason for us using joinpath, which joins components into a full path.
Saving images
You have already learned how to load and download images, so now it's time to learn how to save your image. We will use the save function from the Images package to save the image to disk.
The save function accepts two arguments:
- The destination file location and name
- The image object
Let's have a look at the code to save images:
# load an image
img = load("sample-images/cats-3061372_640.jpg")
# save file in JPG format
save("my_new_file.jpg", img)
# save file in PNG format
save("my_new_file.png", img)
The image format is chosen based on the filename extension. Please note that saving the image in different formats can affect the output quality and file size. Users should find a balance between size and quality.
The save function does not allow you to set image quality, which is usually available in graphics editors, such as GIMP.
Using test images
The TestImages.jl package and dataset provides easy access to a small number of free images out of the box. It is the way to go when trying out different computer vision techniques and algorithms.
The benefits of using the TestImages dataset are the following:
- Images are of different file types, such as JPG, PNG, and TIF
- Images are of different sizes, such as 512x512 and 256x256
- Images are of different color schemes, such as RGB and grayscale
It is very easy to start with the TestImages package. You just need to load the TestImages package and use the testimage function to load the image by name:
using TestImages
img = testimage("mandril_color");
save("mandril_color.png", img);
Our code example would result in loading a mandrill image from the TestImages dataset. You can save this in your current working directory.
Previewing images
You have already learned to load, download, and save images. The only way to check the image itself would be to go to the project folder and open it from there.
The ImageView package solves this problem by previewing the image directly from Julia:
using Images, ImageView
img = load("sample-images/cats-3061372_640.jpg");
imshow(img);
This will preview the image in a new window.
Cropping, scaling, and resizing
Now that you know how to load and preview your image, it is time to start working on content. Three of the most frequent activities you will do when working with images are as follows:
- Crop: Select a specific area of an image
- Resize: Change the size of an image without keeping the proportions
- Scale: Enlarge or shrink an image while keeping the proportions
Cropping an image
Let's go back to the image with the two cats we previewed recently. Here, we will create a new picture, which will only contain the cat on the right:
Our first step will be to identify an area of interest. We will do this by loading the image to Julia and checking its width and height:
using Images, ImageView
source_image = load("sample-images/cats-3061372_640.jpg");
size(source_image)
The size function will output (360, 640), which stands for 360px in height (y-axis) and 640px in width (x-axis). Both coordinates start from the top-left corner.
I have run a number of experiments and identified an area we are interested in—the height from 100 to 290 and the width from 280 to 540. You can try playing around with the following code to see how changing the region will affect the output:
cropped_image = img[100:290, 280:540];
imshow(cropped_image)
This will result in the following image being created and stored in the cropped_image variable. This will also allocate memory so that you can store the newly created image:
There is also another way to create a cropped image, which is by creating a view to the original image. Views don't create a new object or allocate memory, they just point to the original image (or array). They are great when you want to analyze or change a specific part of the picture without interfering with the rest of it:
cropped_image_view = view(img, 100:290, 280:540);
imshow(cropped_image_view)
If you run the preceding code, you will see that it returns an identical result. You can also save the image to disk without any problems.
Resizing an image
Image resizing is the process of changing an image's size without keeping proportions. This is done by calling the imresize function and supplying a new width and height.
Let's take our image with the cats as an example and resize it to 100 x 250 so that we can see what has changed:
We will use our classic code and load the image from disk:
using Images, ImageView
source_image = load("sample-images/cats-3061372_640.jpg");
resized_image = imresize(source_image, (100, 250));
imshow(resized_image);
You should be able to see an image of a smaller size. It has a width of 250 pixels and a height of 100 pixels:
A typical example would be to resize an image to fit a square. You would need to pass the width and height as equal values:
using Images, ImageView
source_image = load("sample-images/cats-3061372_640.jpg");
resized_image = imresize(source_image, (200, 200));
imshow(resized_image);
This would result in an image like this:
Scaling an image
But what if you want to create a thumbnail and keep the original proportions? You will need to scale the image.
Image scaling is the process of changing the size of an image and saving the original proportions. If in the previous section, we manually picked the width and height, we will now calculate it.
Scaling by percentage
Let's start by scaling the image using percentages. Let's say we want to scale the image to be 60% of the original size:
using Images, ImageView
source_image = load("sample-images/cats-3061372_640.jpg");
scale_percentage = 0.6
new_size = trunc.(Int, size(source_image) .* scale_percentage)
resized_image = imresize(source_image, new_size)
imshow(resized_image);
We have done the following:
- We have loaded the image using the load function from the Images package
- We have defined the scaling percentage in the scale_percentage variable
- We have calculated the new_size by first multiplying the current size by our proportion and then converting the float values to int
- We have resized the image using the new_size values
The resulting image is neat and tidy. All of the proportions have been saved:
Don't forget that you can scale the image upward or downward if you so desire.
Scaling to a specific dimension
It is very common to scale your image to a given width and adapt the height automatically or vice versa.
There are multiple ways to approach this problem, but the most straightforward option would be to reuse and extend the code we wrote when scaling by percentage.
Given the original dimension and desired width, what would be the easiest way to calculate the change in percentage? That's correct—just divide the desired width or height by the original.
Let's assume we want to fix our width to 200 and calculate the value for scale_percentage:
new_width = 200
scale_percentage = new_width / size(source_image)[2]
Let's put it all together:
using Images, ImageView
source_image = load("sample-images/cats-3061372_640.jpg");
new_width = 200
scale_percentage = new_width / size(source_image)[2]
new_size = trunc.(Int, size(source_image) .* scale_percentage)
resized_image = imresize(source_image, new_size)
imshow(resized_image);
We have updated our scale by percentage solution to calculate scale_percentage dynamically based on a change in one of the dimensions.
Scaling by two-fold
This is a bonus section for scaling/resizing. The JuliaImages package has a very useful function called somepkg(restrict), which reduces the size of an image by two-fold along the dimensions specified. In other words, it scales the image by 50%.
restrict can be run in three ways:
- Without an additional argument—the image will become twice as small in width and height
- Sending 1 as an argument will make the height twice as small
- Sending 2 as an argument will make the width twice as small
Let's run a demo. Try sending 1 as an additional argument so that we decrease the height by 50%:
Consider the following code:
using Images
source_image = load("sample-images/cats-3061372_640.jpg");
resized_image = restrict(source_image, 1); # height
imshow(resized_image);
Rotating images
Rotation is another must-know technique when working with any type of image. We will start by initializing the required libraries and loading the image from disk. We will also be using the CoordinateTransformations package from the JuliaImages collection to define a rotation transformation:
using Images, CoordinateTransformations
img = load("sample-images/cats-3061372_640.jpg");
tfm = LinearMap(RotMatrix(-pi/4))
img = warp(img, tfm)
imshow(img)
This results in the img variable being updated with a new image, which is shown as follows:
For ease of use, please refer to the following table:
Degree | Formula |
-30o | -pi/6 |
-90o | -pi/2 |
180 | pi |
Please be aware that rotating by degrees other than 90°, -90°, and 180° will result in a black background being added around the original image.
Summary
In this chapter, you received your first introduction to the JuliaImages packages. We went through the process of loading images from disk and URL; performing a different set of transformations, such as resizing and scaling; and finally, learned how to save and preview results.
Next, we will focus on enhancing the image by improving the color scheme and removing noise.
Questions
Please answer the following questions to see whether you have successfully learned this chapter:
- Which package(s) are required to load an image from a disk?
- Which package is required to download a file from the internet?
- Which types of files/file extensions are returned by the readdir function?
- Which function is used to save an image to disk? What are the prerequisites for saving a file to disk?
- What is the most noticeable difference when saving images in JPG or PNG formats?
- What is the difference between scale and resize?