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

Hands-On Computer Vision with Julia: Build complex applications with advanced Julia packages for image processing, neural networks, and Artificial Intelligence

eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Hands-On Computer Vision with Julia

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"
The Julia community does not keep sources other than the Julia website or GitHub up-to-date. Therefore, it is strongly advised to refer to the official website for the latest available version. For example, Ubuntu users get an older version when installing Julia using apt-get.

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.

Windows users are required to complete additional steps to make the TestImages package work. Users are required to follow an extensive post-installation guide from the package page, http://juliaimages.github.io/TestImages.jl/, or from Chapter 9, Case Study – Book Cover Classification, Analysis, and Recognition.

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.

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:

Images are vertical-major, which means that this first index corresponds to the vertical axis and the second to the horizontal axis. This might be different from other programming languages.

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.

Remember: size(source_image)[1] corresponds to height, while size(source_image)[2] corresponds to width.

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:

  1. Which package(s) are required to load an image from a disk?
  2. Which package is required to download a file from the internet?
  3. Which types of files/file extensions are returned by the readdir function?
  4. Which function is used to save an image to disk? What are the prerequisites for saving a file to disk?
  5. What is the most noticeable difference when saving images in JPG or PNG formats?
  6. What is the difference between scale and resize?

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • • Build a full-fledged image processing application using JuliaImages
  • • Perform basic to advanced image and video stream processing with Julia's APIs
  • • Understand and optimize various features of OpenCV with easy examples

Description

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.

Who is this book for?

Hands-On Computer Vision with Julia is for Julia developers who are interested in learning how to perform image processing and want to explore the field of computer vision. Basic knowledge of Julia will help you understand the concepts more effectively.

What you will learn

  • • Analyze image metadata and identify critical data using JuliaImages
  • • Apply filters and improve image quality and color schemes
  • • Extract 2D features for image comparison using JuliaFeatures
  • • Cluster and classify images with KNN/SVM machine learning algorithms
  • • Recognize text in an image using the Tesseract library
  • • Use OpenCV to recognize specific objects or faces in images and videos
  • • Build neural network and classify images with MXNet

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 29, 2018
Length: 202 pages
Edition : 1st
Language : English
ISBN-13 : 9781788999236
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Jun 29, 2018
Length: 202 pages
Edition : 1st
Language : English
ISBN-13 : 9781788999236
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 102.97
Julia 1.0 Programming
€32.99
Julia Programming Projects
€36.99
Hands-On Computer Vision with Julia
€32.99
Total 102.97 Stars icon

Table of Contents

10 Chapters
Getting Started with JuliaImages Chevron down icon Chevron up icon
Image Enhancement Chevron down icon Chevron up icon
Image Adjustment Chevron down icon Chevron up icon
Image Segmentation Chevron down icon Chevron up icon
Image Representation Chevron down icon Chevron up icon
Introduction to Neural Networks Chevron down icon Chevron up icon
Using Pre-Trained Neural Networks Chevron down icon Chevron up icon
OpenCV Chevron down icon Chevron up icon
Assessments Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(1 Ratings)
5 star 0%
4 star 100%
3 star 0%
2 star 0%
1 star 0%
granate Nov 17, 2020
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Es una aproximación muy práctica a las técnicas de clasificación de imágenes. Describe tanto las clásicas como las redes neuronales y el entrenamiento de las mismas, con un lenguaje muy asequible.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.