Pimp uz_framework and add a3

main
Gasper Spagnolo 2022-11-13 14:02:26 +01:00
parent 616ce52732
commit 182432917f
10 changed files with 100 additions and 43 deletions

View File

@ -34,6 +34,14 @@ def one_a() -> npt.NDArray[np.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))
# loop through some numbers
for i in range(1, 10):
print(f'Using {i} bins')
lena_h = uz_image.get_image_bins_ND(lena, i)
lincoln_h = uz_image.get_image_bins_ND(lincoln, i)
print(uz_image.compare_two_histograms(lena_h, lincoln_h, uz_image.DistanceMeasure.euclidian_distance))
return lena_h
def one_b() -> None:
@ -71,24 +79,28 @@ def one_c() -> None:
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')
methods=[uz_image.DistanceMeasure.euclidian_distance, uz_image.DistanceMeasure.chi_square_distance,
uz_image.DistanceMeasure.intersection_distance, uz_image.DistanceMeasure.hellinger_distance ]
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')
for i in range(len(methods)):
fig, axs = plt.subplots(2,3)
fig.suptitle(f'{methods[i].name} between three images')
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)}')
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')
plt.show()
axs[1, 0].bar(np.arange(N_BINS**3), H1, width=3)
axs[1, 0].set(title=f'd(h1, h1) = {np.round(uz_image.compare_two_histograms(H1, H1, method=methods[i]), 2)}')
axs[1, 1].bar(np.arange(N_BINS**3), H2, width=3)
axs[1, 1].set(title=f'd(h1, h2) = {np.round(uz_image.compare_two_histograms(H1, H2, method=methods[i]), 2)}')
axs[1, 2].bar(np.arange(N_BINS**3), H3, width=3)
axs[1, 2].set(title=f'd(h1, h3) = {np.round(uz_image.compare_two_histograms(H1, H3, method=methods[i]), 2)}')
plt.show()
def find_position(a, ix):
for i in range(len(a)):
@ -257,6 +269,8 @@ def two_e():
k2 = np.array([0.1, 0.6, 0.4])
k2 = np.flip(k2)
print("hello")
s1 = signal.copy()
s2 = cv2.filter2D(signal, cv2.CV_64F, k1)
@ -290,10 +304,10 @@ def two_e():
def ex3():
three_a()
three_b()
three_c()
three_d()
three_e()
#three_b()
#three_c()
#three_d()
#three_e()
def three_a():
"""
@ -318,7 +332,7 @@ def three_a():
lena_gausssian_noise = uz_image.gauss_noise(lena_grayscale)
# Salt and pepper noise
lena_salt_and_pepper = uz_image.sp_noise(lena_grayscale)
kernel = np.array(uz_image.get_gaussian_kernel(2)) # MUST BE A 2D for TRANSPOSE
kernel = np.array([uz_image.get_gaussian_kernel(2)]) # MUST BE A 2D for TRANSPOSE
# Denoised
denosised_lena = cv2.filter2D(lena_gausssian_noise, cv2.CV_64F, kernel)
@ -427,8 +441,8 @@ def three_e():
"""
obama_image = uz_image.imread_gray('./data/images/obama.jpg', uz_image.ImageType.float64)
lincoln_image = uz_image.imread_gray('./data/images/lincoln.jpg', uz_image.ImageType.float64)
laplaced_obama = uz_image.filter_laplace(obama_image, 35)
gaussed_lincoln = uz_image.gaussfilter2D(lincoln_image, 5)
laplaced_obama = uz_image.filter_laplace(obama_image, 5)
gaussed_lincoln = uz_image.gaussfilter2D(lincoln_image, 35)
merged = uz_image.sum_two_grayscale_images(laplaced_obama, gaussed_lincoln)
@ -455,9 +469,9 @@ def three_e():
# ######## #
def main():
ex1()
#ex1()
#ex2()
#ex3()
ex3()
if __name__ == '__main__':
main()

View File

@ -225,12 +225,7 @@ def get_image_bins_ND(image: Union[npt.NDArray[np.float64], npt.NDArray[np.uint
"""
bs = []
hist = np.zeros((number_of_bins, number_of_bins, number_of_bins))
if image.dtype.type == np.uint8:
bins = np.linspace(0, 255, num=number_of_bins)
elif image.dtype.type == np.float64:
bins = np.linspace(0, 1, num=number_of_bins)
else:
raise Exception('Unsuported datatype!')
bins = np.linspace(np.min(image), np.max(image), num=number_of_bins)
for i in range(image.shape[2]):
v = image[:, :, i].reshape(-1)
@ -261,7 +256,7 @@ def compare_two_histograms(h1: npt.NDArray[np.float64], h2: npt.NDArray[np.float
return d.astype(float)
def apply_mask_on_image(image: Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]], mask: npt.NDArray[np.uint8]):
def apply_mask_on_image(image: Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]], mask: npt.NDArray[np.uint8]) -> Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]]:
"""
Accepts image and applys mask to image
"""
@ -321,7 +316,11 @@ def simple_convolution_improved(signal: npt.NDArray[np.float64], kernel: npt.NDA
return convolved_signal
def get_gaussian_kernel(sigma: float):
def get_gaussian_kernel(sigma: float) -> npt.NDArray[np.float64]:
"""
Accepts sigma
Returns gaussian kernel
"""
# https://github.com/mikepound/convolve/blob/master/run.gaussian.py
kernel_size = int(2 * np.ceil(3*sigma) + 1)
k_min_max = np.ceil(3*sigma)
@ -361,13 +360,13 @@ def gaussfilter2D(image: Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]],
returns: filtered_image
"""
filtered_image = image.copy()
kernel = np.array(get_gaussian_kernel(sigma))
kernel = np.array([get_gaussian_kernel(sigma)])
filtered_image = cv2.filter2D(filtered_image, cv2.CV_64F, kernel)
filtered_image = cv2.filter2D(filtered_image, cv2.CV_64F, kernel.T)
return filtered_image
def simple_median(signal: npt.NDArray[np.float64], width: int):
def simple_median(signal: npt.NDArray[np.float64], width: int) -> npt.NDArray[np.float64]:
"""
Accepts: signal & width
returns signal improved using median filter
@ -381,7 +380,7 @@ def simple_median(signal: npt.NDArray[np.float64], width: int):
signal[middle_element] = np.median(signal[i:i+width])
return signal
def apply_median_method_2D(image:Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]], width: int):
def apply_median_method_2D(image:Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]], width: int) -> Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]]:
"""
Accepts: image & filter width
returns: image with median filter applied
@ -408,7 +407,7 @@ def apply_median_method_2D(image:Union[npt.NDArray[np.float64], npt.NDArray[np.u
image[y][x] = np.mean(median_filter)
return image
def filter_laplace(image:Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]], sigma: float):
def filter_laplace(image:Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]], sigma: float) -> Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]]:
"""
Accepts: image & sigma
returns: image with laplace filter applied
@ -416,10 +415,10 @@ def filter_laplace(image:Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]],
# Prepare unit impulse and gauss kernel
unit_impulse = np.zeros((1, 2 * int(np.ceil(3*sigma)) + 1))
unit_impulse[0][int(np.ceil(unit_impulse.size /2)) - 1]= 1
gauss_kernel = get_gaussian_kernel(sigma)
assert(len(gauss_kernel) == len(unit_impulse[0]))
gauss_kernel = np.array([get_gaussian_kernel(sigma)])
assert(len(gauss_kernel[0]) == len(unit_impulse[0]))
laplacian_filter = unit_impulse - gauss_kernel
laplacian_filter = unit_impulse - gauss_kernel[0]
# Now apply laplacian filter
applied_by_x = cv2.filter2D(image, -1, laplacian_filter)
@ -427,7 +426,7 @@ def filter_laplace(image:Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]],
return applied_by_y
def gauss_noise(I, magnitude=.1):
def gauss_noise(I, magnitude=.1) -> npt.NDArray[np.float64]:
"""
Accepts: image & magnitude
Returns: image with gaussian noise applied
@ -439,7 +438,7 @@ def gauss_noise(I, magnitude=.1):
return I + np.random.normal(size=I.shape) * magnitude
def sp_noise(I, percent=.1):
def sp_noise(I, percent=.1) -> npt.NDArray[np.float64]:
"""
Accepts: image & percent
Returns: image with salt and pepper noise applied
@ -453,7 +452,7 @@ def sp_noise(I, percent=.1):
return res
def sp_noise1D(signal, percent=.1):
def sp_noise1D(signal, percent=.1) -> npt.NDArray[np.float64]:
"""
Accepts: signal & percent
Returns: signal with salt and pepper noise applied
@ -470,6 +469,5 @@ def sum_two_grayscale_images(image_a: Union[npt.NDArray[np.float64], npt.NDArray
Accepts: image_a, image_b
Returns: image_a + image_b
"""
# Merge image_a and image_b
return (image_a + image_b)/ 2

45
assignment3/a3_utils.py Normal file
View File

@ -0,0 +1,45 @@
import numpy as np
from matplotlib import pyplot as plt
def draw_line(rho, theta, h, w):
"""
Example usage:
plt.imshow(I)
draw_line(rho1, theta1, h, w)
draw_line(rho2, theta2, h, w)
draw_line(rho3, theta3, h, w)
plt.show()
"rho" and "theta": Parameters for the line which will be drawn.
"h", "w": Height and width of an image.
"""
c = np.cos(theta)
s = np.sin(theta)
xs = []
ys = []
if s != 0:
y = int(rho / s)
if 0 <= y < h:
xs.append(0)
ys.append(y)
y = int((rho - w * c) / s)
if 0 <= y < h:
xs.append(w - 1)
ys.append(y)
if c != 0:
x = int(rho / c)
if 0 <= x < w:
xs.append(x)
ys.append(0)
x = int((rho - h * s) / c)
if 0 <= x < w:
xs.append(x)
ys.append(h - 1)
plt.plot(xs[:2], ys[:2], 'r', linewidth=.7)

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

BIN
assignment3/images/pier.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 755 B

Binary file not shown.