简体   繁体   中英

adding noise to an area in the image

As part of image preprocessing I want to corrupt an image by adding random pixel values to a part of the image, specified with a mask. I'm working with python. Are there any common ways to do this, or maybe is there a paper published with this information? All help very much appreciated

Random pixel values are just random integers between 0 and 255 (for color pictures). So you can just pick a random pixel on an image, and replace it by 3 random RGB values. Let's say you have a picture (all black so we can visualize):

import numpy as np
import matplotlib.pyplot as plt

pic = np.full((10, 10, 3), 0)

在此处输入图片说明

Then you can replace a coordinate inside the dimensions of the image (10 by 10 here) by 3 random RGB values between 0 and 255.

pic[np.random.randint(0, 10, 5), 
    np.random.randint(0, 10, 5 )] = \
    np.random.randint(0, 256, (5, 3))

The logic is as follows: take 5 random points in X, Y and replace them with 3 random values between 0 and 255.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM