Newer
Older
import matplotlib.pyplot as plt
from dtuimldmtools import similarity
filename = importlib_resources.files("dtuimldmtools").joinpath("data/digits.mat")
# Image to use as query
i = 1
# Similarity: 'SMC', 'Jaccard', 'ExtendedJaccard', 'Cosine', 'Correlation'
similarity_measure = "SMC"
# Load the digits
X = loadmat(filename)["X"]
# You can also try the CBCL faces dataset (remember to change 'transpose')
N, M = X.shape
# Index of all other images than i
# Compute similarity between image i and all others
sim = sim.tolist()[0]
# Tuples of sorted similarities and their indices
# Visualize query image and 5 most/least similar images
img_hw = int(np.sqrt(len(X[0])))
img = np.reshape(X[i], (img_hw, img_hw))
if transpose:
img = img.T
plt.imshow(img, cmap=plt.cm.gray)
plt.xticks([])
plt.yticks([])
plt.title("Query image")
plt.ylabel("image #{0}".format(i))
for ms in range(5):
# 5 most similar images found
plt.subplot(3, 5, 6 + ms)
im_id = sim_to_index[-ms - 1][1]
im_sim = sim_to_index[-ms - 1][0]
img = np.reshape(X[im_id], (img_hw, img_hw))
if transpose:
img = img.T
plt.imshow(img, cmap=plt.cm.gray)
plt.xlabel("sim={0:.3f}".format(im_sim))
plt.ylabel("image #{0}".format(im_id))
plt.xticks([])
plt.yticks([])
if ms == 2:
plt.title("Most similar images")
# 5 least similar images found
im_id = sim_to_index[ms][1]
im_sim = sim_to_index[ms][0]
img = np.reshape(X[im_id], (img_hw, img_hw))
if transpose:
img = img.T
plt.imshow(img, cmap=plt.cm.gray)
plt.xlabel("sim={0:.3f}".format(im_sim))
plt.ylabel("image #{0}".format(im_id))
plt.xticks([])
plt.yticks([])
if ms == 2:
plt.title("Least similar images")
plt.show()