2022-10-26 16:08:01 +02:00
|
|
|
|
import numpy as np
|
|
|
|
|
import numpy.typing as npt
|
|
|
|
|
from matplotlib import pyplot as plt
|
|
|
|
|
import random
|
|
|
|
|
import cv2
|
|
|
|
|
import uz_framework.image as uz_image
|
|
|
|
|
|
|
|
|
|
#################################################################
|
|
|
|
|
# EXCERCISE 1: Exercise 1: Global approach to image description #
|
|
|
|
|
#################################################################
|
|
|
|
|
def ex1():
|
|
|
|
|
one_a()
|
2022-10-29 19:05:57 +02:00
|
|
|
|
one_b()
|
|
|
|
|
one_c()
|
2022-10-26 16:08:01 +02:00
|
|
|
|
|
2022-10-29 16:57:29 +02:00
|
|
|
|
def one_a() -> npt.NDArray[np.float64]:
|
2022-10-26 16:08:01 +02:00
|
|
|
|
"""
|
|
|
|
|
Firstly, you will implement the function myhist3 that computes a 3-D histogram
|
|
|
|
|
from a three channel image. The images you will use are RGB, but the function
|
|
|
|
|
should also work on other color spaces. The resulting histogram is stored in a 3-D
|
|
|
|
|
matrix. The size of the resulting histogram is determined by the parameter n_bins.
|
|
|
|
|
The bin range calculation is exactly the same as in the previous assignment, except
|
|
|
|
|
now you will get one index for each image channel. Iterate through the image pixels
|
|
|
|
|
and increment the appropriate histogram cells. You can create an empty 3-D numpy
|
|
|
|
|
array with H = np.zeros((n_bins,n_bins,n_bins)). Take care that you normalize
|
|
|
|
|
the resulting histogram.
|
|
|
|
|
"""
|
2022-10-29 19:05:57 +02:00
|
|
|
|
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
|
2022-10-26 16:08:01 +02:00
|
|
|
|
|
2022-10-29 19:05:57 +02:00
|
|
|
|
def one_b() -> None:
|
2022-10-29 16:57:29 +02:00
|
|
|
|
"""
|
|
|
|
|
In order to perform image comparison using histograms, we need to implement
|
|
|
|
|
some distance measures. These are defined for two input histograms and return a
|
|
|
|
|
single scalar value that represents the similarity (or distance) between the two histograms.
|
|
|
|
|
Implement a function compare_histograms that accepts two histograms
|
|
|
|
|
and a string that identifies the distance measure you wish to calculate
|
|
|
|
|
Implement L2 metric, chi-square distance, intersection and Hellinger distance.
|
2022-10-29 19:05:57 +02:00
|
|
|
|
Function implemented in uz_framework
|
|
|
|
|
"""
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def one_c() -> None:
|
|
|
|
|
"""
|
|
|
|
|
Test your function
|
|
|
|
|
Compute a 8×8×8-bin 3-D histogram for each image. Reshape each of them into a
|
|
|
|
|
1-D array. Using plt.subplot(), display all three images in the same window as well
|
|
|
|
|
as their corresponding histograms. Compute the L2 distance between histograms of
|
|
|
|
|
object 1 and 2 as well as L2 distance between histograms of objects 1 and 3.
|
|
|
|
|
|
|
|
|
|
Question: Which image (object_02_1.png or object_03_1.png) is more similar
|
|
|
|
|
to image object_01_1.png considering the L2 distance? How about the other three
|
|
|
|
|
distances? We can see that all three histograms contain a strongly expressed component (one bin has a much higher value than the others). Which color does this
|
|
|
|
|
bin represent
|
|
|
|
|
Answer:
|
|
|
|
|
"""
|
|
|
|
|
IM1 = uz_image.imread('./data/dataset/object_01_1.png', uz_image.ImageType.float64)
|
|
|
|
|
IM2 = uz_image.imread('./data/dataset/object_02_1.png', uz_image.ImageType.float64)
|
|
|
|
|
IM3 = uz_image.imread('./data/dataset/object_03_1.png', uz_image.ImageType.float64)
|
|
|
|
|
N_BINS = 8
|
|
|
|
|
|
|
|
|
|
H1 = uz_image.get_image_bins_ND(IM1, N_BINS).reshape(-1)
|
|
|
|
|
H2 = uz_image.get_image_bins_ND(IM2, N_BINS).reshape(-1)
|
|
|
|
|
H3 = uz_image.get_image_bins_ND(IM3, N_BINS).reshape(-1)
|
|
|
|
|
|
|
|
|
|
fig, axs = plt.subplots(2,3)
|
|
|
|
|
fig.suptitle('Euclidian distance between three images')
|
|
|
|
|
|
|
|
|
|
axs[0, 0].imshow(IM1)
|
|
|
|
|
axs[0, 0].set(title='Image1')
|
|
|
|
|
axs[0, 1].imshow(IM2)
|
|
|
|
|
axs[0, 1].set(title='Image2')
|
|
|
|
|
axs[0, 2].imshow(IM3)
|
|
|
|
|
axs[0, 2].set(title='Image3')
|
|
|
|
|
|
|
|
|
|
axs[1, 0].bar(np.arange(N_BINS**3), H1, width=3)
|
|
|
|
|
axs[1, 0].set(title=f'L_2(h1, h1) = {np.round(uz_image.compare_two_histograms(H1, H1, uz_image.DistanceMeasure.euclidian_distance), 2)}')
|
|
|
|
|
axs[1, 1].bar(np.arange(N_BINS**3), H2, width=3)
|
|
|
|
|
axs[1, 1].set(title=f'L_2(h1, h2) = {np.round(uz_image.compare_two_histograms(H1, H2, uz_image.DistanceMeasure.euclidian_distance), 2)}')
|
|
|
|
|
axs[1, 2].bar(np.arange(N_BINS**3), H3, width=3)
|
|
|
|
|
axs[1, 2].set(title=f'L_2(h1, h3) = {np.round(uz_image.compare_two_histograms(H1, H3, uz_image.DistanceMeasure.euclidian_distance), 2)}')
|
2022-10-29 16:57:29 +02:00
|
|
|
|
|
2022-10-29 19:05:57 +02:00
|
|
|
|
plt.show()
|
2022-10-26 16:08:01 +02:00
|
|
|
|
|
2022-10-29 19:05:57 +02:00
|
|
|
|
|
2022-10-26 16:08:01 +02:00
|
|
|
|
# ######## #
|
|
|
|
|
# SOLUTION #
|
|
|
|
|
# ######## #
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
ex1()
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|