1

I have numpy array of HSV image data:

Shape:

(960, 1280, 3)

Data:

 [[ 90  53  29]
  [ 90  53  29]
  [ 68  35  29]
  ..., 
  [ 66  28 146]
  [ 58  21 145]
  [ 58  21 145]]

 [[ 90  53  29]
  [ 90  53  29]
  [ 75  35  29]
  ..., 
  [ 65  31 148]
  [ 69  18 144]
  [ 69  18 144]]]

I want to create a filter (eg "H < 20 or V > 200") and based on that filter to modify the array so that I can set HSV values to something I need, like [0 0 0].

I couldn't wrap my head around the indexing system, how this should be approached?

2 Answers 2

1

You create a mask array to pick the elements you want to change:

H = image[:,:,0]
V = image[:,:,2]
mask = (H < 20) & (V > 200)
image[mask] = 0
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I "knew" it must be simple.. :) What if I want to define something else as a value, eg [127 64 100]?
1

Assuming hsv as the input hsv image data, you can use some reshaping alongwith boolean indexing to set all three channels to a generic triplet like so -

newvals = np.array([127,64,100])

mask = (hsv[:,:,0] < 20) | (hsv[:,:,2]> 200)
hsv.reshape(-1,3)[mask.ravel()] = newvals

Sample run -

In [13]: hsv
Out[13]: 
array([[[155, 179, 207],
        [200,  52, 185],
        [241, 139, 232],
        [188, 149, 117]],

       [[145, 169, 116],
        [146, 134, 108],
        [ 74,  34, 121],
        [  9, 190,  91]],

       [[240, 207, 228],
        [140, 158, 124],
        [179, 154, 212],
        [ 79, 166, 131]]], dtype=uint8)

In [14]: newvals = np.array([127,64,100])
    ...: mask = (hsv[:,:,0] < 20) | (hsv[:,:,2]> 200)
    ...: hsv.reshape(-1,3)[mask.ravel()] = newvals

In [15]: hsv
Out[15]: 
array([[[127,  64, 100],
        [200,  52, 185],
        [127,  64, 100],
        [188, 149, 117]],

       [[145, 169, 116],
        [146, 134, 108],
        [ 74,  34, 121],
        [127,  64, 100]],

       [[127,  64, 100],
        [140, 158, 124],
        [127,  64, 100],
        [ 79, 166, 131]]], dtype=uint8)

Comments

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.