Obvezne stvari nrjene

main
Gasper Spagnolo 2022-11-05 10:56:51 +01:00
parent 1186b8c982
commit 314f13905d
2 changed files with 44 additions and 8 deletions

View File

@ -290,6 +290,7 @@ def two_e():
def ex3(): def ex3():
three_a() three_a()
three_b() three_b()
three_c()
def three_a(): def three_a():
""" """
@ -325,13 +326,13 @@ def three_a():
axs[0, 0].imshow(lena_grayscale, cmap='gray') axs[0, 0].imshow(lena_grayscale, cmap='gray')
axs[0, 0].set(title='Orginal image') axs[0, 0].set(title='Orginal image')
axs[0, 1].imshow(lena_gausssian_noise, cmap='gray') 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].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].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].imshow(desalted_lena, cmap='gray')
axs[1, 2].set(title='Desalted lena') axs[1, 2].set(title='Desalted Lena')
plt.show() plt.show()
@ -357,9 +358,29 @@ def three_b():
axs[0].set(title='Original') axs[0].set(title='Original')
axs[1].imshow(museo, cmap='gray') axs[1].imshow(museo, cmap='gray')
axs[1].set(title='Sharpened') axs[1].set(title='Sharpened')
plt.savefig('.')
plt.show() 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 # # SOLUTION #

View File

@ -294,7 +294,15 @@ def gaussfilter2D(imge: Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]], s
kernel = get_gaussian_kernel(sigma) kernel = get_gaussian_kernel(sigma)
kernel = cv2.filter2D(kernel, cv2.CV_64F, kernel) 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): def gauss_noise(I, magnitude=.1):
# input: image, magnitude of noise # input: image, magnitude of noise
@ -309,10 +317,17 @@ def sp_noise(I, percent=.1):
# output: modified image # output: modified image
res = I.copy() 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] = 1
res[np.random.rand(I.shape[0], I.shape[1]) < percent / 2] = 0 res[np.random.rand(I.shape[0], I.shape[1]) < percent / 2] = 0
return res 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