Ben
Ben

Reputation: 57

How to change the pixel value from Nan to zero and One?

I have an one image array of shape (4, 64, 256).
At pixel [63, 0] I found nan values.

output[:, 63, 0] -----> array([nan, nan, nan,  1.])

So after using output = np.nan_to_num(output), succesfully I got:

output[:, 63, 0] -----> array([0., 0., 0., 1.])

Then I am unable to get like this:

output[:, 63, 0] -----> array([0., 0., 1., 1.])

I tried new = output[np.where(output[2, 63, 0] == 0), [2, 63, 0]] = 1

but it gives 1 as output instead of array([0., 0., 1., 1.])

please help me solve this.

Upvotes: 0

Views: 526

Answers (1)

JohnTanner
JohnTanner

Reputation: 66

It depends on what you want to accomplish:

  1. if you just want to set a single "0" to "1", just use: output[2,63,0] = 1
  2. if you want all "0" be "1" use: output = np.where(output == 0, 1, output) (see: https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.where.html)
  3. if you want just the "0" in one channel be "1", use: output[2,:,:] = np.where(output[2,:,:] == 0, 1, output[2,:,:])

Hope this helps!

Upvotes: 2

Related Questions