|
|
@ -11,10 +11,10 @@ import os
|
|
|
|
#################################################################
|
|
|
|
#################################################################
|
|
|
|
|
|
|
|
|
|
|
|
def ex1():
|
|
|
|
def ex1():
|
|
|
|
#one_a()
|
|
|
|
one_a()
|
|
|
|
one_b()
|
|
|
|
one_b()
|
|
|
|
#one_c()
|
|
|
|
one_c()
|
|
|
|
distances, selected_distances = one_d('./data/dataset', './data/dataset_reduced/', 10)
|
|
|
|
distances, selected_distances = one_d('./data/dataset', 8)
|
|
|
|
one_e(distances, selected_distances)
|
|
|
|
one_e(distances, selected_distances)
|
|
|
|
|
|
|
|
|
|
|
|
def one_a() -> npt.NDArray[np.float64]:
|
|
|
|
def one_a() -> npt.NDArray[np.float64]:
|
|
|
@ -90,7 +90,12 @@ def one_c() -> None:
|
|
|
|
|
|
|
|
|
|
|
|
plt.show()
|
|
|
|
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
def one_d(directory: str, reduced_directory: str, n_bins: int):
|
|
|
|
def find_position(a, ix):
|
|
|
|
|
|
|
|
for i in range(len(a)):
|
|
|
|
|
|
|
|
if a[i] == ix:
|
|
|
|
|
|
|
|
return i
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def one_d(directory: str, n_bins: int):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
You will now implement a simple image retrieval system that will use histograms.
|
|
|
|
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
|
|
|
|
Write a function that will accept the path to the image directory and the parameter
|
|
|
@ -104,45 +109,52 @@ def one_d(directory: str, reduced_directory: str, n_bins: int):
|
|
|
|
does the retrieved sequence change if you use a different number of bins? Is the
|
|
|
|
does the retrieved sequence change if you use a different number of bins? Is the
|
|
|
|
execution time affected by the number of bins?
|
|
|
|
execution time affected by the number of bins?
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
img_names = os.listdir(reduced_directory)
|
|
|
|
|
|
|
|
methods=[uz_image.DistanceMeasure.euclidian_distance, uz_image.DistanceMeasure.chi_square_distance,
|
|
|
|
methods=[uz_image.DistanceMeasure.euclidian_distance, uz_image.DistanceMeasure.chi_square_distance,
|
|
|
|
uz_image.DistanceMeasure.intersection_distance, uz_image.DistanceMeasure.hellinger_distance ]
|
|
|
|
uz_image.DistanceMeasure.intersection_distance, uz_image.DistanceMeasure.hellinger_distance ]
|
|
|
|
imgs=[]
|
|
|
|
img_names = os.listdir(directory)
|
|
|
|
hists=[]
|
|
|
|
all_dists = [[] for _ in range(len(methods))]
|
|
|
|
selected_dists=[]
|
|
|
|
selected_distances = [[] for _ in range(len(methods))]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
compare_image = uz_image.imread('./data/dataset/object_05_4.png', uz_image.ImageType.float64)
|
|
|
|
|
|
|
|
h_compare_image = uz_image.get_image_bins_ND(compare_image, n_bins).reshape(-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
imgs = []
|
|
|
|
|
|
|
|
hists = []
|
|
|
|
|
|
|
|
|
|
|
|
for i in range(len(img_names)):
|
|
|
|
for i in range(len(img_names)):
|
|
|
|
imgs.append(uz_image.imread(f'{reduced_directory}/{img_names[i]}', uz_image.ImageType.float64))
|
|
|
|
# Firstly read all images
|
|
|
|
hists.append(uz_image.get_image_bins_ND(imgs[i], n_bins).reshape(-1))
|
|
|
|
current_image = uz_image.imread(f'{directory}/{img_names[i]}', uz_image.ImageType.float64)
|
|
|
|
for method in methods:
|
|
|
|
imgs.append(current_image)
|
|
|
|
fig, axs = plt.subplots(2, len(imgs))
|
|
|
|
current_image_histogram = uz_image.get_image_bins_ND(current_image, n_bins).reshape(-1)
|
|
|
|
fig.suptitle(f'Comparrison between different measures, using:{method.name}')
|
|
|
|
hists.append(current_image_histogram)
|
|
|
|
distances = []
|
|
|
|
# Then iterate through all methods and calculate distances
|
|
|
|
for i in range(len(hists)):
|
|
|
|
for j in range(len(methods)):
|
|
|
|
distances.append(uz_image.compare_two_histograms(hists[0], hists[i], method))
|
|
|
|
all_dists[j].append(uz_image.compare_two_histograms(h_compare_image, current_image_histogram, methods[j]))
|
|
|
|
|
|
|
|
|
|
|
|
indexes = np.argsort(distances)
|
|
|
|
for i in range(len(all_dists)):
|
|
|
|
selected_dists.append(distances)
|
|
|
|
# Setup plot
|
|
|
|
|
|
|
|
fig, axs = plt.subplots(2, 6)
|
|
|
|
for i in range(len(imgs)):
|
|
|
|
fig.suptitle(f'Comparrison between different measures, using:{methods[i].name}')
|
|
|
|
axs[0, i].imshow(imgs[indexes[i]])
|
|
|
|
# Sort the distances
|
|
|
|
axs[0, i].set(title=f'{img_names[indexes[i]]}')
|
|
|
|
sorted_dists = np.sort(all_dists[i])
|
|
|
|
axs[1, i].bar(np.arange(n_bins**3), hists[indexes[i]], width=2)
|
|
|
|
ixs=[]
|
|
|
|
axs[1, i].set(title=f'd={distances[indexes[i]]}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# find the closest and plot em
|
|
|
|
|
|
|
|
for j in range(6):
|
|
|
|
|
|
|
|
# Find indexes of closes distances
|
|
|
|
|
|
|
|
for k in range(len(all_dists[i])):
|
|
|
|
|
|
|
|
if all_dists[i][k] == sorted_dists[j]:
|
|
|
|
|
|
|
|
ixs.append(k)
|
|
|
|
|
|
|
|
selected_distances[i].append(sorted_dists[j])
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Now plot them
|
|
|
|
|
|
|
|
axs[0, j].imshow(imgs[ixs[j]])
|
|
|
|
|
|
|
|
axs[0, j].set(title=f'{img_names[ixs[j]]}')
|
|
|
|
|
|
|
|
axs[1, j].bar(np.arange(n_bins**3), hists[ixs[j]], width=3)
|
|
|
|
|
|
|
|
axs[1, j].set(title=f'd={np.round(all_dists[i][ixs[j]], 2)}')
|
|
|
|
plt.show()
|
|
|
|
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
img_names = os.listdir(directory)
|
|
|
|
return all_dists, selected_distances
|
|
|
|
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 all_dists, selected_dists
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def one_e(distances: list, selected_dists: list):
|
|
|
|
def one_e(distances: list, selected_dists: list):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@ -162,12 +174,11 @@ def one_e(distances: list, selected_dists: list):
|
|
|
|
makevery_indexes = []
|
|
|
|
makevery_indexes = []
|
|
|
|
|
|
|
|
|
|
|
|
for j in range(len(distances[i])):
|
|
|
|
for j in range(len(distances[i])):
|
|
|
|
print(distances[i][j])
|
|
|
|
|
|
|
|
if distances[i][j] in selected_dists[i]:
|
|
|
|
if distances[i][j] in selected_dists[i]:
|
|
|
|
makevery_indexes.append(j)
|
|
|
|
makevery_indexes.append(j)
|
|
|
|
|
|
|
|
|
|
|
|
axs[0].plot(indexes,distances[i],markevery=makevery_indexes, markerfacecolor = "none", marker = "o", markeredgecolor = "orange")
|
|
|
|
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")
|
|
|
|
axs[1].plot(indexes,np.sort(distances[i]),markevery=[i for i in range(6)], markerfacecolor = "none", marker = "o", markeredgecolor = "orange")
|
|
|
|
plt.show()
|
|
|
|
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
############################
|
|
|
@ -358,8 +369,8 @@ def three_b():
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
#ex1()
|
|
|
|
#ex1()
|
|
|
|
#ex2()
|
|
|
|
ex2()
|
|
|
|
ex3()
|
|
|
|
# ex3()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
main()
|
|
|
|