“Python-OpenCV” How to Add Noise on Image. Degree Adjustable .

AI
higashi
higashi

Hi, I’m higashi.

This page introduces how to add noise on image by using python-openCV as shown below.

Overview of the process of adding noise

The above image has large noise, but it is adjustable to without being noticed.

 

I think it is useful for making teacher data for AI.

 

So, let’s get started!”

 

Sponsored Links

Necessary Libraries.

This work is conducted by using below libraries.

★OpenCV

★Numpy

If it is not installed on your python, please install first.

 

Sponsored Links

Sample Code of Adding Noise on Image

The shown below is the program.

On this program, original image file is saved as ‘base.jpg’ and added noise image will save as ‘noise.jpg’ in program execution folder.

#import libraries
import numpy as np
import cv2

#loading original image 
img=cv2.imread('base.jpg',cv2.IMREAD_GRAYSCALE)
h,w=img.shape[:2]

#adding noise(please adjust noise_level)
noise_level=150
noise=np.random.randint(0,noise_level,(h,w))
img=img+noise
cv2.imwrite('noise.jpg',img)

By changing ‘noise_level’ on the program, you can adjust the noise degree.

* Note that too large value of ‘noise_level’ will be white image.

 

Sponsored Links

Execution Result of Sample Code

At first, this is original Image.(noise_level=0)

sample image(noise_lebel=0)

 

noise_level=20.

sample image(noise_lebel=20)

noise_lebel=150

sample image(noise_lebel=150)

noise_lebel=300

sample image(noise_lebel=300)

higashi
higashi

It seems to be fine.

 

That’s all. Thank you!!

コメント