The Azimuth Project
Simple image processing tutorial (changes)

Showing changes from revision #23 to #24: Added | Removed | Changed

Contents

Idea

This tutorial is intended to show how to upload an image to Sage and just get some basic information about the image, and do some simple transforms by using Numpy/Scipy, which are included in Sage. We will also use the time-series functionality to show typical properties, when starting an image analysis, which is legio in Remote sensing typically in the form of image restoration - to remove noise and blur.

Details

Upload the image

So start by creating a new sage worksheet, open it and choose “Upload” on the Data menu of that worksheet:

detab

The upload is just standard dialogue so it shouldn’t present any problems. I uploaded a cat image which I will use for this tutorial. The first thing we need to do before we can do anything is to import the necessary libraries. They are included in the Python that Sage uses, but we have tell Sage that we want to use them. To save ourselves from typing to much we set aliases in our import statements:

import numpy as np
import pylab as pl

In Sage and Python its good to keep track of which module something is located in and it also is good for finding out what a module actually contains. So we can now use functions in Numpy, by typing “np.function” or “pl.s” followed by “tab” (completion) which shows a list of all functions or modules that starts with an “s”, which comes a very handy feature. You can but should not do:

from np import * 

which is considered bad among Python programmers, as it soon lead to confusion.

Plotting an image

After uploading the image, we use the pylab imread function and use Matrix_plot in Sage to show it:

plot image

Show image attribute and do some old skool transforms

So Matrix_plot knows how to handle Numpy arrays automatically. That is convenient! So let us add some attributes to the image using Scipy. I’ll add the size of the image and also its variance:

plot image

I’ve also shown a plot of the Fast Fourier Transform (FFT) to the right of the original image. So now we are in a position to start to do image processing in both the spatial or spectral domain. I also added “auto” which evaluates whatever it is applied to when a sage worksheet is opened. In this case its “%auto” which is telling Sage to evaluate the whole cell. So it can be used as a way to initialize or automate things in your programs.

Use some time to get to know numpy, scipy and the python and sage constructs I have used. There is a list comprehension for making a list of graphics with just two elements, which seems unjustified but I plan to add some more images to the graphics array and this makes that simpler.

See if you can recover the color image back from the jazz cat and how long it takes (on the demo server) to do this ?

I just did these changes and used the inverse descrete Fourier transform (IFFT) in numpy.fft library, called ifft() and the timing by using sage timeit():

# inverse Fourier transform to try to get the cat back
cf = np.fft.ifft2(jazz_cat)

# here we construct a list of graphic objects - for now matrix plots
mat_cats = [matrix_plot(im) for im in [cat,real_part(jazz_cat), real_part(cf), cat - real_part(cf)]]
ga = graphics_array(mat_cats,2,2)

So we assign the result of doing the IFFT to the variable cf and add the real part of cf and the difference between the array cat and the real part of the numpy array cf. I left out the code on timing on purpose. The last three lines with html() and ga.show(), remains unchanged.

  1. Try out the code changes and explain why did I do the latter subtraction?

Its a way to verify that we got the cat back from its journey in complex space ! So black indicates all zeros in the matrix.

  1. Can you find a way to calculate the image size by using numpy array methods?

Here is one way:

prod(cat.shape()) 

We get back a list of three items , the x,y,z dimensions. Then we just multiply all item in the list with the prod() function.

Some reflections sofar

If the image resides on the web they might be retrieved by importing urlib which is part of Python/Sage standard libraries:


import urllib
urllib.urlretrieve()

There is an example on how this is done in the worksheet in the reference section. The intentional transparency between Sage and Numpy/Scipy also eases the coding a lot and enables us to focus on the task at hand.

References

  • Public worksheet for download. Which has answers and s lot more examples on how to work with images in Sage. Like interactive Savitsky-Golay smoothing and Gauss kernel blurring, where you can change the parameters in Sage interact() examples. Here is one animation on an image with 10% of noise of blur_image(), with a Gauss kernel increasing from 1 to 20:
blur filter