Book Image

Hands-On Computer Vision with Julia

By : Dmitrijs Cudihins
Book Image

Hands-On Computer Vision with Julia

By: Dmitrijs Cudihins

Overview of this book

Hands-On Computer Vision with Julia is a thorough guide for developers who want to get started with building computer vision applications using Julia. Julia is well suited to image processing because it’s easy to use and lets you write easy-to-compile and efficient machine code. . This book begins by introducing you to Julia's image processing libraries such as Images.jl and ImageCore.jl. You’ll get to grips with analyzing and transforming images using JuliaImages; some of the techniques discussed include enhancing and adjusting images. As you make your way through the chapters, you’ll learn how to classify images, cluster them, and apply neural networks to solve computer vision problems. In the concluding chapters, you will explore OpenCV applications to perform real-time computer vision analysis, for example, face detection and object tracking. You will also understand Julia's interaction with Tesseract to perform optical character recognition and build an application that brings together all the techniques we introduced previously to consolidate the concepts learned. By end of the book, you will have understood how to utilize various Julia packages and a few open source libraries such as Tesseract and OpenCV to solve computer vision problems with ease.
Table of Contents (11 chapters)
9
Assessments

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)
Copyright notice: Pixabay provides images under CC0 Creative Commons. They are free for commercial use and no attributions are required.

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.