Ex1 done without star
parent
d0bc2515dc
commit
5798425bc4
|
@ -10,10 +10,11 @@ import os
|
|||
# EXCERCISE 1: Exercise 1: Global approach to image description #
|
||||
#################################################################
|
||||
def ex1():
|
||||
one_a()
|
||||
one_b()
|
||||
one_c()
|
||||
one_d('./data/dataset_reduced/', 10)
|
||||
#one_a()
|
||||
#one_b()
|
||||
#one_c()
|
||||
image, distances, selected_distances = one_d('./data/dataset', './data/dataset_reduced/', 10)
|
||||
one_e(image, distances, selected_distances)
|
||||
|
||||
def one_a() -> npt.NDArray[np.float64]:
|
||||
"""
|
||||
|
@ -27,9 +28,12 @@ def one_a() -> npt.NDArray[np.float64]:
|
|||
array with H = np.zeros((n_bins,n_bins,n_bins)). Take care that you normalize
|
||||
the resulting histogram.
|
||||
"""
|
||||
image = uz_image.imread('./data/dataset/object_01_1.png', uz_image.ImageType.float64)
|
||||
bins = uz_image.get_image_bins_ND(image, 20)
|
||||
return bins
|
||||
lena = uz_image.imread('./data/images/lena.png', uz_image.ImageType.float64)
|
||||
lincoln = uz_image.imread('./data/images/lincoln.jpg', uz_image.ImageType.float64)
|
||||
lena_h = uz_image.get_image_bins_ND(lena, 128)
|
||||
lincoln_h = uz_image.get_image_bins_ND(lincoln, 128)
|
||||
print(uz_image.compare_two_histograms(lena_h, lincoln_h, uz_image.DistanceMeasure.euclidian_distance))
|
||||
return lena_h
|
||||
|
||||
def one_b() -> None:
|
||||
"""
|
||||
|
@ -85,7 +89,7 @@ def one_c() -> None:
|
|||
|
||||
plt.show()
|
||||
|
||||
def one_d(directory: str, n_bins: int):
|
||||
def one_d(directory: str, reduced_directory: str, n_bins: int):
|
||||
"""
|
||||
You will now implement a simple image retrieval system that will use histograms.
|
||||
Write a function that will accept the path to the image directory and the parameter
|
||||
|
@ -99,15 +103,15 @@ def one_d(directory: str, n_bins: int):
|
|||
does the retrieved sequence change if you use a different number of bins? Is the
|
||||
execution time affected by the number of bins?
|
||||
"""
|
||||
img_names = os.listdir(directory)
|
||||
img_names = os.listdir(reduced_directory)
|
||||
methods=[uz_image.DistanceMeasure.euclidian_distance, uz_image.DistanceMeasure.chi_square_distance,
|
||||
uz_image.DistanceMeasure.intersection_distance, uz_image.DistanceMeasure.hellinger_distance ]
|
||||
imgs=[]
|
||||
hists=[]
|
||||
selected_dists=[]
|
||||
for i in range(len(img_names)):
|
||||
imgs.append(uz_image.imread(f'{directory}/{img_names[i]}', uz_image.ImageType.float64))
|
||||
imgs.append(uz_image.imread(f'{reduced_directory}/{img_names[i]}', uz_image.ImageType.float64))
|
||||
hists.append(uz_image.get_image_bins_ND(imgs[i], n_bins).reshape(-1))
|
||||
|
||||
for method in methods:
|
||||
fig, axs = plt.subplots(2, len(imgs))
|
||||
fig.suptitle(f'Comparrison between different measures, using:{method.name}')
|
||||
|
@ -116,6 +120,7 @@ def one_d(directory: str, n_bins: int):
|
|||
distances.append(uz_image.compare_two_histograms(hists[0], hists[i], method))
|
||||
|
||||
indexes = np.argsort(distances)
|
||||
selected_dists.append(distances)
|
||||
|
||||
for i in range(len(imgs)):
|
||||
axs[0, i].imshow(imgs[indexes[i]])
|
||||
|
@ -125,6 +130,44 @@ def one_d(directory: str, n_bins: int):
|
|||
|
||||
plt.show()
|
||||
|
||||
img_names = os.listdir(directory)
|
||||
h_image = uz_image.get_image_bins_ND(imgs[0], n_bins).reshape(-1)
|
||||
all_dists = [[] for _ in range(len(methods))]
|
||||
|
||||
for i in range(len(img_names)):
|
||||
im = uz_image.imread(f'{directory}/{img_names[i]}', uz_image.ImageType.float64)
|
||||
h = uz_image.get_image_bins_ND(im, n_bins).reshape(-1)
|
||||
for j in range(len(methods)):
|
||||
all_dists[j].append(uz_image.compare_two_histograms(h_image, h, methods[j]))
|
||||
|
||||
print(all_dists)
|
||||
return hists[0], all_dists, selected_dists
|
||||
|
||||
def one_e(hist: npt.NDArray[np.float64], distances: list, selected_dists: list):
|
||||
"""
|
||||
You can get a better sense of the differences in the distance values if you plot all
|
||||
of them at the same time. Use the function plt.plot() to display image indices
|
||||
on the x axis and distances to the reference image on the y axis. Display both the
|
||||
unsorted and the sorted image sequence and mark the most similar values using a
|
||||
circle (see pyplot documentation)
|
||||
"""
|
||||
methods=[uz_image.DistanceMeasure.euclidian_distance, uz_image.DistanceMeasure.chi_square_distance,
|
||||
uz_image.DistanceMeasure.intersection_distance, uz_image.DistanceMeasure.hellinger_distance ]
|
||||
|
||||
for i in range(len(distances)):
|
||||
fig, axs = plt.subplots(1, 2)
|
||||
fig.suptitle(f'Using {methods[i].name}')
|
||||
indexes = np.arange(0, len(distances[i]) , 1)
|
||||
makevery_indexes = []
|
||||
|
||||
for j in range(len(distances[i])):
|
||||
print(distances[i][j])
|
||||
if distances[i][j] in selected_dists[i]:
|
||||
makevery_indexes.append(j)
|
||||
|
||||
axs[0].plot(indexes,distances[i],markevery=makevery_indexes, markerfacecolor = "none", marker = "o", markeredgecolor = "orange")
|
||||
axs[1].plot(indexes,np.sort(distances[i]),markevery=makevery_indexes, markerfacecolor = "none", marker = "o", markeredgecolor = "orange")
|
||||
plt.show()
|
||||
|
||||
|
||||
# ######## #
|
||||
|
|
Loading…
Reference in New Issue