0

Here is a part of the code that I am using:

elif noise_typ == "s&p":
      row,col,ch = image.shape
      s_vs_p = 0.5
      amount = 0.004
      out = np.copy(image)
      
      # Salt mode
      num_salt = np.ceil(amount * image.size * s_vs_p)

      coords = [np.random.randint(0, i - 1, int(num_salt))
              for i in image.shape]

      
      out[coords] = 1

      # Pepper mode
      num_pepper = np.ceil(amount* image.size * (1. - s_vs_p))
      coords = [np.random.randint(0, i - 1, int(num_pepper))
              for i in image.shape]
      out[coords] = 0
      return out

It comes from this question How to add noise (Gaussian/salt and pepper etc) to image in Python with OpenCV

I tried to apply it to one of my images like this:

image = cv2.imread('bunny.jpg')

noisy_image = noisy("s&p", image)

However, it gave me the following error:

IndexError: index 1487 is out of bounds for axis 0 with size 1280

The image dimensions are 1920*1280. So, I guess the rows and columns in coords are going out of bounds. How do I prevent that?

Thanks.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Real Noob
  • 1,369
  • 2
  • 15
  • 29

0 Answers0