Implementing DTW
In this example, we will look for similarity in 684 images from 8 categories. We will use four imports of PIL, numpy, mlpy, and collections:
from PIL import Image from numpy import array import mlpy from collections import OrderedDict
First, we need to obtain the time series representation of the images and store it in a dictionary (data) with the number of the image and its time series as data[fn] = list.
Tip
The performance of this process will lie in the number of images processed, so beware of the use of memory with large datasets.
data = {}
for fn in range(1,685):
img = Image.open("ImgFolder\\{0}.jpg".format(fn))
arr = array(img)
list = []
for n in arr: list.append(n[0][0])
for n in arr: list.append(n[0][1])
for n in arr: list.append(n[0][2])
data[fn] = listThen, we need to select an image for the reference, which will be compared with all the other images in the data dictionary:
reference = data[31]
Now, we need to apply the mlpy.dtw_std function to all the...