0

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

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
Majmun
  • 19
  • 5

1 Answers1

2

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)

enter image description here

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.

enter image description here

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143