diff --git a/assignment2/solution.py b/assignment2/solution.py index c57dcb3..972bf5c 100644 --- a/assignment2/solution.py +++ b/assignment2/solution.py @@ -290,6 +290,7 @@ def two_e(): def ex3(): three_a() three_b() + three_c() def three_a(): """ @@ -325,13 +326,13 @@ def three_a(): axs[0, 0].imshow(lena_grayscale, cmap='gray') axs[0, 0].set(title='Orginal image') axs[0, 1].imshow(lena_gausssian_noise, cmap='gray') - axs[0, 1].set(title='Gaussian noise') + axs[0, 1].set(title='Gaussian noise applied') axs[1, 1].imshow(denosised_lena, cmap='gray') - axs[1, 1].set(title='Denoised lena') + axs[1, 1].set(title='Denoised Lena') axs[0, 2].imshow(lena_salt_and_pepper, cmap='gray') - axs[0, 2].set(title='Salt and Pepper') + axs[0, 2].set(title='Salt and Pepper applied') axs[1, 2].imshow(desalted_lena, cmap='gray') - axs[1, 2].set(title='Desalted lena') + axs[1, 2].set(title='Desalted Lena') plt.show() @@ -357,9 +358,29 @@ def three_b(): axs[0].set(title='Original') axs[1].imshow(museo, cmap='gray') axs[1].set(title='Sharpened') - plt.savefig('.') plt.show() - + +def three_c(): + signal = np.zeros(40) + signal[15:20] = 1.0 + + fig, axs = plt.subplots(1, 4) + axs[0].plot(signal) + axs[0].set(title='Original') + + signal_sp = uz_image.sp_noise1D(signal, 0.05) + axs[1].plot(signal_sp) + axs[1].set(title='Corrupted') + + kernel = uz_image.get_gaussian_kernel(1.4) + signal_gauss = cv2.filter2D(signal_sp, cv2.CV_64F, kernel) + axs[2].plot(signal_gauss) + axs[2].set(title='Gauss') + + signal = uz_image.simple_median(signal_sp, 3) + axs[3].plot(signal) + axs[3].set(title='Median') + plt.show() # ######## # # SOLUTION # diff --git a/assignment2/uz_framework/image.py b/assignment2/uz_framework/image.py index 6282cd0..58083d0 100644 --- a/assignment2/uz_framework/image.py +++ b/assignment2/uz_framework/image.py @@ -294,7 +294,15 @@ def gaussfilter2D(imge: Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]], s kernel = get_gaussian_kernel(sigma) kernel = cv2.filter2D(kernel, cv2.CV_64F, kernel) - +def simple_median(signal: npt.NDArray[np.float64], width: int): + signal = signal.copy() + if width % 2 == 0: + raise Exception('No u won\'t do that') + + for i in range(len(signal) - int(np.ceil(width/2))): + middle_element = int(i + np.floor(width/2)) + signal[middle_element] = np.median(signal[i:i+width]) + return signal def gauss_noise(I, magnitude=.1): # input: image, magnitude of noise @@ -309,10 +317,17 @@ def sp_noise(I, percent=.1): # output: modified image res = I.copy() - res[np.random.rand(I.shape[0], I.shape[1]) < percent / 2] = 1 res[np.random.rand(I.shape[0], I.shape[1]) < percent / 2] = 0 return res +def sp_noise1D(signal, percent=.1): + signal = signal.copy() + signal[np.random.rand(signal.shape[0]) < percent / 2] = 2 + signal[np.random.rand(signal.shape[0]) < percent / 2] = 1 + signal[np.random.rand(signal.shape[0]) < percent / 2] = 4 + signal[np.random.rand(signal.shape[0]) < percent / 2] = 0.4 + return signal +