Image Noise Reduction in 3 Steps using Python
In this article, I will show you how to do noise reduction in 3 simple steps. We will be using a machine learning trained noise reduction model. It is one of the best noise reduction models I’ve found out there. I will share more about the model and how to apply it in the following paragraphs.
How can we tell if an image is noisy? That could be for another project idea, because our noise reduction model is not smart enough to calculate the noise. We have to determine the values ourselves, in this case best way is usually to try different values and find the best outcome. After some practice you will have some understanding, and will be much easier to find the best values. Let me tell you more about this project, and then let’s get started.
In this project, we will be using three python packages. The packages are as follows: OpenCV, Matplotlib, and NumPy. OpenCV is a very well-known kit for computer vision. As a prerequisite for OpenCV library, we will need to install Numpy. We transform pixels into arrays when reading an image; NumPy is going to do that in the behind scenes. When dealing with multi-dimensional arrays, NumPy is perfect.
Table of Contents
- Noise Reduction Model
- Step 1 – Installing Packages
- Step 2 – Importing Image
- Step 3 – Denoising the Image
- Comparing the Results
Noise Reduction Model
Here comes the project’s insightful part. Firstly, I’m going to share which algorithm we’re going to use for noise reduction. And then, I’m going to share how many parameters it has and what each one means.
The big yellow areas in the picture look alike. Plus, the small green regions look similar. The Non-local Means Denoise Algorithm selects a pixel, takes a small window around it, and then scans the image for similar windows, averages all the windows, and calculates the result to replace the pixel. This algorithm is described as non-local because the model searches the whole image to find mutual connections instead of just looking around an individual area. Isn’t that cool?
There are two most known functions of this algorithm. The first one is for grayscale images, and the second one is for colored images. In our case, we will use the one colored images.
The parameters for fastNlMeansDenoisingColored( src, dst, h, hcolor, templateWindowSize, searchWindowSize)
- src: the input image that we want to do noise reduction.
- dst: the destination if we want to export the result.
- h: the luminance component. (Larger h value removes more noise but can also remove image details).
- hcolor: the color component. (10 is the recommended value by the documentation for colored images).
- templateWindowSize: The pixel size of the area that the function will smooth. It should be an odd integer number. (7 is the recommended value by the documentation, which will fit for most cases).
- searchWindowSize: The pixel size of the area that the function will find and reference to. Affect performance linearly: greater searchWindowsSize means greater denoising time. Also it should be an odd integer number. (21 is the recommended value by the documentation, which will fit for most cases).
Step 1 – Installing Packages
We have to install two libraries so that our program works. The following libraries have to be installed: numpy and opencv-python. We can install them in one line using the PIP library manager:
pip install numpy opencv-python
Let’s go ahead and create a new notebook about Jupiter. Feel free to use the code writing environment you want. I will be using the Jupyter notebook for this project. Here’s my first notebook block, where we import the libraries we’ve just built.
import cv2 import numpy as np
Great! Now we can move to the next step, where we will import and read an image.
Step 2 – Importing Image
We will find a picture in this phase that we want to use for noise reduction. It can be a noisy picture to see the change better. But feel free to use a regular image too, there will still be smoothness happening.
Here is the image that I am planning to use:
And here is the line to read the image; we are using the imread method by OpenCV:
img = cv2.imread("test_image.png")
Now, let’s go ahead to the third and the final step, where we will see our noise reduction in action.
Step 3 – Denoising the Image
Looks great so far! The fun part of the project comes here. We will see how the picture will look after the reduction of noise. We will run the function with three different values to see how each one affects the final result. Feel free to check the first step to understand what each parameter stands for.
denoised_1 = cv2.fastNlMeansDenoisingColored(img,None,3,3,7,21) denoised_2 = cv2.fastNlMeansDenoisingColored(img,None,5,5,7,21) denoised_3 = cv2.fastNlMeansDenoisingColored(img,None,15,15,7,21)
The denoised images are assigned to different variables. Let’s save the results. We are going to use imwrite method by OpenCV. As you can see, we are passing the variable and the file name that we want to save as.
cv2.imwrite('image_1.png', denoised_1) cv2.imwrite('image_2.png', denoised_2) cv2.imwrite('image_3.png', denoised_3)
Comparing the Results
We can see them in order. The larger the value gets, the smoother the image becomes.
And here is a before and after images. I will choose the first one as my final result.
Congratulations! We have developed a program that uses the non-local method denoising algorithm to reduce an image’s noise. It is one of the best strategies to minimize image noise. Other denoising algorithms exist, but they are not as efficient as the denoising algorithm’s non-local means. Some of them are also known as strategies for blurring.
Working on hands-on programming projects like this one is the best way to sharpen your coding skills. I hope you enjoyed and learned something new today. Feel free to reach me if you have any questions or comments. You can even book a session with me to make a 1:1 meeting.
Follow my blog and YouTube channel to stay inspired. Thank you, 🙂
Hands-on Machine Learning Projects:
Building a Chatbot using Python