Remove noise from image with OpenCV

share link

by Abdul Rawoof A R dot icon Updated: Mar 27, 2023

technology logo
technology logo

Solution Kit Solution Kit  

Noise is a random variable with a mean of 0. Let us consider the noisy pixel P = p + n, where p is the pixel value, and n is the noise. The mean filter blurs an image to remove noise and involves finding the mean of the pixel values within a n x n kernel. The mean then replaces the pixel intensity of the centre element. This will eliminate some noise in the image and smooths the edges. 



Noise in photography can be defined as a random variation in the image signal. Many factors, including poor lighting conditions, high ISO settings, long exposure times, and heat, can cause noise. Noise can also be introduced into an image during the editing process. 

  • NumPy - a Python library used for working with arrays. It has functions for working in the domain of linear algebra, Fourier transform, and matrices. 
  • OpenCV - to offer a common infrastructure for computer vision applications and to accelerate the usage of machine perception in commercial products. 



Here is an example of how to remove noise from an image with OpenCV in Python: 

Fig 1: Preview of the output that you will get on running this code from your IDE.

Fig 2: Example image that we used.

Code

In this solution we're using NumPy and OpenCV library.

Instructions

Follow the steps carefully to get the output easily.

  1. Install PyCharm Community Edition on your computer.
  2. Open terminal and install the required libraries with following commands.
  3. Install NumPy - pip install numpy.
  4. Install OpenCV - pip install opencv-python.
  5. Create a new Python file(eg: test.py).
  6. Copy the snippet using the 'copy' button and paste it into that file.
  7. Run the file using run button.


Note: Use image from your local as input image.


I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.


I found this code snippet by searching for 'remove noise from image opencv python' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in PyCharm 2022.3.3.
  2. The solution is tested on Python 3.9.7.
  3. NumPy version 1.24.2.
  4. OpenCV version 4.7.0.72.


Using this solution, we are able to remove noise from image with OpenCV in Python with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to remove noise from image with OpenCV in Python.

Dependent Libraries

numpyby numpy

Python doticonstar image 23755 doticonVersion:v1.25.0rc1doticon
License: Permissive (BSD-3-Clause)

The fundamental package for scientific computing with Python.

Support
    Quality
      Security
        License
          Reuse

            numpyby numpy

            Python doticon star image 23755 doticonVersion:v1.25.0rc1doticon License: Permissive (BSD-3-Clause)

            The fundamental package for scientific computing with Python.
            Support
              Quality
                Security
                  License
                    Reuse

                      opencvby opencv

                      C++ doticonstar image 69456 doticonVersion:4.7.0doticon
                      License: Permissive (Apache-2.0)

                      Open Source Computer Vision Library

                      Support
                        Quality
                          Security
                            License
                              Reuse

                                opencvby opencv

                                C++ doticon star image 69456 doticonVersion:4.7.0doticon License: Permissive (Apache-2.0)

                                Open Source Computer Vision Library
                                Support
                                  Quality
                                    Security
                                      License
                                        Reuse

                                          You can also search for any dependent libraries on kandi like 'NumPy' and 'OpenCV'.

                                          Support

                                          1. For any support on kandi solution kits, please use the chat
                                          2. For further learning resources, visit the Open Weaver Community learning page.


                                          See similar Kits and Libraries