uz_assignments/assignment2/solution.py

54 lines
2.1 KiB
Python

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()
#one_b()
def one_a() -> npt.NDArray[np.float64]:
"""
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.
"""
test_image = uz_image.imread('./data/images/museum.jpg', uz_image.ImageType.float64)
bins = uz_image.get_image_bins_ND(test_image, 10)
return bins
def one_b():
"""
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.
"""
test_image = uz_image.imread('./data/images/museum.jpg', uz_image.ImageType.float64)
bins = uz_image.get_image_bins_ND(test_image, 10)
uz_image.compare_two_histograms(bins[0], bins[1], uz_image.DistanceMeasure.chi_square_distance)
# ######## #
# SOLUTION #
# ######## #
def main():
ex1()
if __name__ == '__main__':
main()