main
Gasper Spagnolo 2022-11-06 16:55:31 +01:00
parent 074c92c93d
commit 9ef3629e00
2 changed files with 43 additions and 6 deletions

View File

@ -187,8 +187,8 @@ def one_e(distances: list, selected_dists: list):
def ex2():
two_b()
#two_d()
#two_e()
two_d()
two_e()
def two_b():
"""
@ -209,7 +209,7 @@ def two_b():
"""
signal = uz_text.read_data('./data/signal.txt')
kernel = uz_text.read_data('./data/kernel.txt')
convolved_signal = uz_image.simple_convolution_improved(signal, kernel)
convolved_signal = uz_image.simple_convolution(signal, kernel)
cv2_convolved_signal = cv2.filter2D(signal, cv2.CV_64F, kernel)
@ -287,6 +287,7 @@ def two_e():
################################
# EXCERCISE 3: Image Filtering #
################################
def ex3():
three_a()
three_b()
@ -454,9 +455,9 @@ def three_e():
# ######## #
def main():
#ex1()
ex1()
#ex2()
ex3()
#ex3()
if __name__ == '__main__':
main()

View File

@ -219,7 +219,10 @@ def get_image_bins(image: Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]]
return counts
def get_image_bins_ND(image: Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]], number_of_bins: int) -> npt.NDArray[np.float64]:
"""
Accepts image in the float64 format or uint8 and number of bins
Returns normailzed image histogram bins
"""
bs = []
hist = np.zeros((number_of_bins, number_of_bins, number_of_bins))
if image.dtype.type == np.uint8:
@ -239,6 +242,10 @@ def get_image_bins_ND(image: Union[npt.NDArray[np.float64], npt.NDArray[np.uint
return hist / np.sum(hist)
def compare_two_histograms(h1: npt.NDArray[np.float64], h2: npt.NDArray[np.float64], method: DistanceMeasure) -> float:
"""
Accepts two histograms and method of comparison
Returns distance between them
"""
if method == DistanceMeasure.euclidian_distance:
d = np.sqrt(np.sum(np.square(h1 - h2)))
@ -361,6 +368,10 @@ def gaussfilter2D(image: Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]],
return filtered_image
def simple_median(signal: npt.NDArray[np.float64], width: int):
"""
Accepts: signal & width
returns signal improved using median filter
"""
if width % 2 == 0:
raise Exception('No u won\'t do that')
@ -371,6 +382,10 @@ def simple_median(signal: npt.NDArray[np.float64], width: int):
return signal
def apply_median_method_2D(image:Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]], width: int):
"""
Accepts: image & filter width
returns: image with median filter applied
"""
if width % 2 == 0:
raise Exception('No u won\'t do that')
@ -394,6 +409,10 @@ def apply_median_method_2D(image:Union[npt.NDArray[np.float64], npt.NDArray[np.u
return image
def filter_laplace(image:Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]], sigma: float):
"""
Accepts: image & sigma
returns: image with laplace filter applied
"""
# 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
@ -409,6 +428,10 @@ def filter_laplace(image:Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]],
return applied_by_y
def gauss_noise(I, magnitude=.1):
"""
Accepts: image & magnitude
Returns: image with gaussian noise applied
"""
# input: image, magnitude of noise
# output: modified image
I = I.copy()
@ -417,6 +440,10 @@ def gauss_noise(I, magnitude=.1):
def sp_noise(I, percent=.1):
"""
Accepts: image & percent
Returns: image with salt and pepper noise applied
"""
# input: image, percent of corrupted pixels
# output: modified image
@ -427,6 +454,10 @@ def sp_noise(I, percent=.1):
return res
def sp_noise1D(signal, percent=.1):
"""
Accepts: signal & percent
Returns: signal with salt and pepper noise applied
"""
signal = signal.copy()
signal[np.random.rand(signal.shape[0]) < percent / 2] = 2
signal[np.random.rand(signal.shape[0]) < percent / 2] = 1
@ -435,5 +466,10 @@ def sp_noise1D(signal, percent=.1):
return signal
def sum_two_grayscale_images(image_a: Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]], image_b :Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]]) -> Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]]:
"""
Accepts: image_a, image_b
Returns: image_a + image_b
"""
# Merge image_a and image_b
return (image_a + image_b)/ 2