Search code examples
pythonarraysnumpyhsv

How to modify pixel values in numpy array of HSV image data?


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?


Solution

  • 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