0

I got an rgb image (h and w are not constant) and I want to paint green all the pixels that are within certain range (rgb range). I got most of the code working, the thing i struggle with: assume I got the following image:

arr = np.zeros((4,4,3))
arr[0,0] = [2,3,4]
arr[1, 1] = [1, 2, 3]

lets assume the condition is all the pixels that meet the following rule:

t = np.all((arr >= [1,2,3])&(arr <= [1,4,4]),axis = 2)
print(t)
[[False False False False]
[False  True False False]
[False False False False]
[False False False False]]

each value in t represents a pixel in arr, if it's true i want to change the corresponding pixel in arr to be [10,10,10]. meaning i want the output to be:

[[[2. 3. 4.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]]

 [[0. 0. 0.]
  [10. 10. 10.]
  [0. 0. 0.]
  [0. 0. 0.]]

 [[0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]]

 [[0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]]]

Im asking for a numpy-ish way to do this.

1 Answer 1

1

IIUC you want to do:

arr[t] = [10,10,10]

Example:

>>> arr = np.zeros((4,4,3))
>>> arr[0,0] = [2,3,4]
>>> arr[1,1] = [1,2,3]
>>> t = np.all((arr >= [1,2,3])&(arr <= [1,4,4]),axis = 2)
>>> arr[t] = [10,10,10]
>>> arr
array([[[ 2.,  3.,  4.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [10., 10., 10.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]]])
Sign up to request clarification or add additional context in comments.

1 Comment

@YaronScherf you should check your t, I added the complete example from your OP and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.