This tutorial is for the people who are complete beginners in the area of computer vision and image processing also aren't very familiar with numpy and other tools in python. It intends to give a good balance between the practical and the theoretical aspects of the area. I expect to finish the tutorial within 2 hours.
conda
(installed with anaconda) using
command conda install pil
np.array
function creates a numpy array out of python lists, or nested lists. Unlike Python lists which can have any type of element at any position Numpy arrays are homogenious. It means all elements are of the same type. The data type of the array can be specified by the keyword argument dtype
.
In [2]:
import numpy as np
arr = np.array([1, 2, 3, 4], dtype=np.uint8)
print(type(arr))
In [5]:
arr = np.array([[1, 2],[2, 3],[3, 4]], dtype=np.uint8)
print(arr)
Creating numpy array in which every element cannot be represented in the same type raises Value Error.
In [4]:
arr = np.array([[1],[2, 3],[3, 4]], dtype=np.uint8)
In [12]:
ten_cross_ten = np.array([range(i*10, (i + 1)*10) for i in range(10)])
print(ten_cross_ten)
The individual elements of the array can be acessed in the same way the values of a regular python nested list can be acessed.
In [8]:
print(ten_cross_ten[1][1])
In [6]:
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x[1:7:2]
Out[6]:
In [7]:
x[-2:10]
Out[7]:
In [9]:
x[-3:3:-1]
Out[9]:
In [10]:
x[5:]
Out[10]:
Now suppose you want to select the first two coloums from the first five rows.
In [23]:
ten_cross_ten[:5][:2]
Out[23]:
Well it didn't worked out this way. Can any one of you explain why?
In [24]:
ten_cross_ten[:5]
Out[24]:
Now you must have guessed, ten_cross_ten[:5][:2]
selects the first two elements from the output of ten_cross_ten[:5]
. So to do our desired operation you have to do the following:
In [25]:
ten_cross_ten[:5, :5]
Out[25]:
I hope you get the point. This works for arrays with higher dimentions.
Exercise: Print the matrix formed by the even elements
In [26]:
ten_cross_ten + 1
Out[26]:
In [27]:
ten_cross_ten * ten_cross_ten
Out[27]:
In [31]:
ten_cross_ten[:5, :5] = 0
print(ten_cross_ten)
In [32]:
ten_cross_ten[6, :] = 0
print(ten_cross_ten)
In [ ]:
In [1]:
from scipy.misc import imread
panda = imread('panda.jpg')
To display images you will another library called matplotlib. The %matplotlib inline
magic of IPython has to set to display results inline in the notebook.
In [2]:
%matplotlib inline
import matplotlib.pyplot as plt
In [3]:
plt.imshow(panda)
Out[3]:
In [4]:
panda.shape
Out[4]:
A colored image can be seen as a 3 dimentional array. So every pixel is an array of size 3
In [6]:
panda[0][0]
Out[6]:
Since the images read from imread
are numpy array, they can manipulated in the same way we manipulated array earlier. Try to guess the output of the following code.
In [4]:
panda[100:200, 100:200] = [0, 0, 255]
plt.imshow(panda)
Out[4]:
Now guess what will this do
In [6]:
rows, cols, channels = panda.shape
mirror_panda = panda.copy() #because we want to preserve the original panda image
mirror_panda[:rows/2, :] = panda[-rows/2 - 1::-1, :]
plt.imshow(mirror_panda)
Out[6]:
Exercise: Given the coordinates write a function to make a red "H" on the image, with thickness 3px, width 12px and heigh 24px
In [ ]:
You can do other crazy stuff like
In [7]:
mirror_panda = panda.copy()
mirror_panda[:rows/2, :cols/2, :-1] = 0
mirror_panda[rows/2:, cols/2:, 1:] = 0
plt.imshow(mirror_panda)
Out[7]:
Exercise: make the other two quadrants yellow and green
(20 minutes)
Matplotlib provides easy procedures to create publishing quality plots.
matplotlib.pyplot
is a collection of command style functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: eg, create a figure, create a plotting area in a figure, plot some lines in a plotting area, decorate the plot with labels, etc.... matplotlib.pyplot
is stateful, in that it keeps track of the current figure and plotting area, and the plotting functions are directed to the current axes. Without the %matplotlib inline
directive we specified earlier plt.show()
has to be used to display the plots.
In [14]:
import matplotlib.pyplot as plt
import numpy as np
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
Out[14]:
To plot points (x1, y1), (x2, y2) ..., (xn ,yn)
the pyplot
syntax is plot([x1, x2, ..., xn], [y1, y2, ..., yn], <other parameters>)
when only one array is provided it is assumed to be y values and x values automatically takes the value [0, 1, ..., n-1]
where n
is the number of y values. When no styling option is provided pyplot
joins all the plotted points with a blue line.
In [16]:
y_vals = np.sin(np.arange(0, np.pi, 0.01))
x_vals = np.arange(0, np.pi, 0.01)
plt.plot(x_vals, y_vals)
Out[16]:
Exercise: plot a circle
In [19]:
plt.hist(panda.flatten())
Out[19]: