0

I have an image of shape (455,500,3). What I want to do is to decrement all the odd values of the Blue channel, and only the Blue channel. I did that, but it doesn't work :

for i in range(im_modif[i,i,2]):
    if np.all(im_modif[:,:,2]%2!=0):
        im_modif[i][i][i]-1
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
  • What do you mean by "all the odd values of the Blue channel"? Do you mean the pixel at, say co-ordinate (1, 1) (so like a checkerboard pattern)? Or do you mean that you want to round all odd values in the blue channel down to the nearest even value? – Pam Mar 19 '20 at 13:29
  • For all pixels of the image, when we're facing odd values for the blue channel, we decrease this value by one –  Mar 19 '20 at 13:31

1 Answers1

3

Let's make a reproducible example:

import numpy as np

x = np.random.randint(0, 256, (100, 100, 3))

Now you can select all values from the third (blue) channel that result in 1 after the modulo operation with 2, and decrease all of them by 1:

x[..., -1][np.mod(x[..., -1], 2) == 1] -= 1

Now all the values are even (I guess this is what you wanted):

array([[162, 138, 222, ..., 200,  58, 216],
       [ 34,   2,  86, ..., 150, 122,  28],
       [104, 206, 238, ...,  40,  86, 238],
       ...,
       [ 24, 182,  28, ...,  86, 176,  28],
       [184, 206, 140, ...,  26,  22,  80],
       [238, 140, 142, ..., 216,  62,  80]])
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
  • Merci Nicolas! this is what I wanted! What does the three dots ... mean ? –  Mar 19 '20 at 13:44
  • it's the equivalent of repeating the `:` like this `[:, :, ;, -1]`. a better explanation is here https://stackoverflow.com/questions/772124/what-does-the-ellipsis-object-do – Nicolas Gervais Mar 19 '20 at 13:59