Popravil prvo nalogo

main
Gasper Spagnolo 2022-11-04 15:14:08 +01:00
parent 45d019d816
commit ab95afdea7
7 changed files with 51 additions and 46 deletions

View File

@ -1 +0,0 @@
../dataset/object_18_1.png

View File

@ -1 +0,0 @@
../dataset/object_18_2.png

View File

@ -1 +0,0 @@
../dataset/object_18_3.png

View File

@ -1 +0,0 @@
../dataset/object_30_1.png

View File

@ -1 +0,0 @@
../dataset/object_30_2.png

View File

@ -1 +0,0 @@
../dataset/object_30_3.png

View File

@ -11,10 +11,10 @@ import os
#################################################################
def ex1():
#one_a()
one_a()
one_b()
#one_c()
distances, selected_distances = one_d('./data/dataset', './data/dataset_reduced/', 10)
one_c()
distances, selected_distances = one_d('./data/dataset', 8)
one_e(distances, selected_distances)
def one_a() -> npt.NDArray[np.float64]:
@ -90,7 +90,12 @@ def one_c() -> None:
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.
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
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,
uz_image.DistanceMeasure.intersection_distance, uz_image.DistanceMeasure.hellinger_distance ]
imgs=[]
hists=[]
selected_dists=[]
img_names = os.listdir(directory)
all_dists = [[] for _ in range(len(methods))]
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)):
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}')
distances = []
for i in range(len(hists)):
distances.append(uz_image.compare_two_histograms(hists[0], hists[i], method))
# Firstly read all images
current_image = uz_image.imread(f'{directory}/{img_names[i]}', uz_image.ImageType.float64)
imgs.append(current_image)
current_image_histogram = uz_image.get_image_bins_ND(current_image, n_bins).reshape(-1)
hists.append(current_image_histogram)
# Then iterate through all methods and calculate distances
for j in range(len(methods)):
all_dists[j].append(uz_image.compare_two_histograms(h_compare_image, current_image_histogram, methods[j]))
indexes = np.argsort(distances)
selected_dists.append(distances)
for i in range(len(imgs)):
axs[0, i].imshow(imgs[indexes[i]])
axs[0, i].set(title=f'{img_names[indexes[i]]}')
axs[1, i].bar(np.arange(n_bins**3), hists[indexes[i]], width=2)
axs[1, i].set(title=f'd={distances[indexes[i]]}')
for i in range(len(all_dists)):
# Setup plot
fig, axs = plt.subplots(2, 6)
fig.suptitle(f'Comparrison between different measures, using:{methods[i].name}')
# Sort the distances
sorted_dists = np.sort(all_dists[i])
ixs=[]
# 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()
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 all_dists, selected_dists
return all_dists, selected_distances
def one_e(distances: list, selected_dists: list):
"""
@ -162,12 +174,11 @@ def one_e(distances: list, selected_dists: list):
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")
axs[1].plot(indexes,np.sort(distances[i]),markevery=[i for i in range(6)], markerfacecolor = "none", marker = "o", markeredgecolor = "orange")
plt.show()
############################
@ -358,8 +369,8 @@ def three_b():
def main():
#ex1()
#ex2()
ex3()
ex2()
# ex3()
if __name__ == '__main__':
main()