Search code examples
pythonopencvimage-processingmask

opencv: extract pixels with r>b>g


I am trying to extract from an image only pixels with r>b>g with opencv in python . I don't want to use .split() because it is too slow. Can someone help me ?

I have tried things like that: (but too slow)

b,g,r = cv2.split(resized)

ma1 = np.logical_or(r>b,b>g)

EDIT, I want to do something like this:

img[img[2]>img[1] and  img[1]>img[0]]=0

Solution

  • I'm not sure if this gives expected result but this works for me without error

    img = cv2.imread(...)
    
    img[ (img[:,:,2] > img[:,:,1]) & (img[:,:,1] > img[:,:,0]) ] = 0
    
    cv2.imshow('image', img)
    

    I don't have to split it.

    Because cv2 uses BGR instead of RGB so I had to compare in different order.

    I could convert to RGB using

    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    

    but then I would have to convert it back to BGR to display it.


    BTW: cv2 uses numpy array to keep img

    print( type(img) )
    
    <class 'numpy.ndarray'>