Slmaan

Task 2: Noise Reduction

Apr 18th, 2023 (edited)
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. import cv2 as cv
  2. import matplotlib.pyplot as plt
  3.  
  4. def load_images():
  5.     """
  6.    Load the two noisy images from the file system
  7.    """
  8.     img1 = cv.imread('Noisyimage1.jpg')
  9.     img2 = cv.imread('Noisyimage2.jpg')
  10.     return img1, img2
  11.  
  12. def apply_filters(img):
  13.     """
  14.    Apply averaging and median filters to an image
  15.  
  16.    Parameters:
  17.        img: the input image
  18.  
  19.    Returns:
  20.        A tuple of the filtered images
  21.    """
  22.     blur = cv.blur(img, (5, 5))
  23.     median = cv.medianBlur(img, 5)
  24.     return blur, median
  25.  
  26. def display_images(images, titles):
  27.     """
  28.    Display the input images with the corresponding titles using pyplot
  29.  
  30.    Parameters:
  31.        images: a list of images to display
  32.        titles: a list of titles to display with each image
  33.    """
  34.     for i, (image, title) in enumerate(zip(images, titles)):
  35.         plt.subplot(2, 3, i + 1)
  36.         plt.imshow(image, 'gray')
  37.         plt.title(title)
  38.         plt.xticks([])
  39.         plt.yticks([])
  40.     plt.show()
  41.  
  42. if __name__ == '__main__':
  43.     img1, img2 = load_images()
  44.  
  45.     blur1, median1 = apply_filters(img1)
  46.     blur2, median2 = apply_filters(img2)
  47.  
  48.     titles = ['Noisy image 1', 'Averaging', 'Median',
  49.               'Noisy image 2', 'Averaging', 'Median']
  50.     images = [img1, blur1, median1, img2, blur2, median2]
  51.  
  52.     display_images(images, titles)
  53.  
Advertisement
Add Comment
Please, Sign In to add comment