├── demoImg ├── .DS_Store ├── gaussian.bmp └── saltpepper.bmp ├── README.md └── imgProcess.py /demoImg/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwjoel/DeNoise/HEAD/demoImg/.DS_Store -------------------------------------------------------------------------------- /demoImg/gaussian.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwjoel/DeNoise/HEAD/demoImg/gaussian.bmp -------------------------------------------------------------------------------- /demoImg/saltpepper.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwjoel/DeNoise/HEAD/demoImg/saltpepper.bmp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Noise Reduction in Images 2 | 3 | ## Overview 4 | NoiseReduction is a Python program that reduces noise in images using two methods: local averaging and median filtering. The program utilizes matplotlib and numpy to read noisy images (`saltpepper.bmp` and `gaussian.bmp`) and suppress the noise using the mentioned methods. 5 | 6 | ### Local Averaging 7 | Local averaging is a common method to denoise images where a small square neighborhood of odd size (e.g. 3x3, 5x5) around each pixel from the noisy image is taken, and the average of this neighborhood is computed. A new 'denoised' image is then formed where each pixel is the average from the local neighborhood of the corresponding pixel in the noisy image. Special considerations are needed for border pixels as the neighborhood concept may not work precisely for these pixels. 8 | 9 | ### Median Filtering 10 | Median filtering is an alternative method to denoise images where a small square neighborhood of odd size (e.g. 3x3, 5x5) around each pixel from the noisy image is taken, and the median value of this neighborhood is computed. A new 'denoised' image is then formed where each pixel is the median from the local neighborhood of the corresponding pixel in the noisy image. Special considerations are needed for border pixels as the neighborhood concept may not work precisely for these pixels. 11 | 12 | ## Getting Started 13 | 14 | 1. Clone the repository. 15 | 2. Install the required dependencies (matplotlib and numpy). 16 | 3. Run the program with your noisy images. 17 | 18 | ## Contributing 19 | 20 | We appreciate any contributions to improve NoiseReduction. Please feel free to submit a pull request, report a bug, or suggest a new feature. Refer to the [contributing guidelines](link-to-contributing-guidelines) for more information on how to contribute to this project. 21 | 22 | ## License 23 | 24 | This project is licensed under the [MIT License](link-to-license). 25 | -------------------------------------------------------------------------------- /imgProcess.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pylab as plt 3 | 4 | def denoise_image(img_path = "", filter_size = 3, method = ""): 5 | 6 | """ (str, int, str) -> list 7 | Perform noise reduction on the image. 8 | """ 9 | 10 | #Global exception handling of the function. 11 | try: 12 | #Check whether the passed parameters are complete. 13 | if img_path == "" or method == "": 14 | raise Exception('Missing Parameter') 15 | temp_area, processed_img, half_filter = [], [], filter_size // 2 16 | #Import pictures and convert them into numpy arrays. 17 | img_data = plt.imread(img_path) 18 | #According to the length and width of the picture, create an empty array with the same shape to save the result. 19 | processed_img = np.zeros((len(img_data),len(img_data[0]))) 20 | #According to the length and width of the picture, traverse each pixel. 21 | for height in range(len(img_data)): 22 | for width in range(len(img_data[0])): 23 | #According to the block size, scan the pixels around a specific pixel on height. 24 | for point_y in range(filter_size): 25 | #Check if the pixel reached the border of the image 26 | if height + point_y - half_filter < 0 or height + point_y + half_filter > len(img_data) - 1: 27 | #If so, use white to replace it. 28 | temp_area.append(255) 29 | else: 30 | #According to the block size, scan the pixels around a specific pixel on width. 31 | for point_x in range(filter_size): 32 | #Check if the pixel reached the border of the image 33 | if width + point_x - half_filter < 0 or width + point_x + half_filter > len(img_data[0]) - 1: 34 | #If so, use white to replace it. 35 | temp_area.append(255) 36 | else: 37 | #Imported the pixels around each pixel into a new array. 38 | temp_area.append(img_data[height + point_y - half_filter][width + point_x - half_filter]) 39 | #Determine the method used for image noise reduction. 40 | if method == "median_filter": 41 | #Sort the pixels to ensure that the middle value is taken. 42 | temp_area.sort() 43 | #Then pass the intermediate value to a new array to save the result. 44 | processed_img[height][width] = np.median(temp_area) 45 | elif method == "local_average": 46 | #Find the average of the new array, and then pass it to a new array, used to save the results. 47 | processed_img[height][width] = np.mean(temp_area) 48 | else: 49 | raise Exception('Wrong Parameter') 50 | #Reset the variables that temporarily store pixels. 51 | temp_area = [] 52 | #Return the processed image array. 53 | return processed_img 54 | #Through the exception. 55 | except Exception as e: 56 | print(e) 57 | 58 | def plot_example(filter_size): 59 | #Plot the pictuire. 60 | plt.figure(figsize=(12, 6)) 61 | plt.subplot(2,3,1) 62 | plt.title('Salt & pepper noise image', fontsize=10) 63 | plt.imshow(plt.imread("saltpepper.bmp"), cmap="gray") 64 | plt.subplot(2,3,2) 65 | plt.title('Salt & pepper noise image - local averaging', fontsize=10) 66 | plt.imshow(denoise_image("saltpepper.bmp", filter_size, "local_average"), cmap="gray") 67 | plt.subplot(2,3,3) 68 | plt.title('Salt & pepper noise image - median filtering', fontsize=10) 69 | plt.imshow(denoise_image("saltpepper.bmp", filter_size, "median_filter"), cmap="gray") 70 | plt.subplot(2,3,4) 71 | plt.title('Gaussian noise image', fontsize=10) 72 | plt.imshow(plt.imread("gaussian.bmp"), cmap="gray") 73 | plt.subplot(2,3,5) 74 | plt.title('Gaussian noise image - local averaging', fontsize=10) 75 | plt.imshow(denoise_image("gaussian.bmp", filter_size, "local_average"), cmap="gray") 76 | plt.subplot(2,3,6) 77 | plt.title('Gaussian noise image - median filtering', fontsize=10) 78 | plt.imshow(denoise_image("gaussian.bmp", filter_size, "median_filter"), cmap="gray") 79 | plt.show() 80 | 81 | plot_example(3) --------------------------------------------------------------------------------