Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On Computer Vision with Julia

You're reading from   Hands-On Computer Vision with Julia Build complex applications with advanced Julia packages for image processing, neural networks, and Artificial Intelligence

Arrow left icon
Product type Paperback
Published in Jun 2018
Publisher Packt
ISBN-13 9781788998796
Length 202 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Dmitrijs Cudihins Dmitrijs Cudihins
Author Profile Icon Dmitrijs Cudihins
Dmitrijs Cudihins
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Getting Started with JuliaImages FREE CHAPTER 2. Image Enhancement 3. Image Adjustment 4. Image Segmentation 5. Image Representation 6. Introduction to Neural Networks 7. Using Pre-Trained Neural Networks 8. OpenCV 9. Assessments
10. Other Books You May Enjoy

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.

You have been reading a chapter from
Hands-On Computer Vision with Julia
Published in: Jun 2018
Publisher: Packt
ISBN-13: 9781788998796
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime