Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Remove pixels(BGR) within a certain threshold of the selected pixel(BGR)

I want to remove pixels in an image within a certain threshold of a particular BGR pixel.

Similar to the Magic Wand or Fuzzy Tool

I found this old MATLAB implementation here

This are the steps mentioned in the post

Add the square of difference of the RGB value at particular location with selected pixel value

c_r, c_g and c_b are the pixel values at a particular location

ref_r, ref_g and ref_b is the selected pixel

((c_r - ref_r).^2 + (c_g - ref_g).^2 + (c_b - ref_b).^2) <= tolerance^2;

I'm trying to implement this is python here's my code

I have a group of pixels values(BGR) stored in a numpy array in __pixels

__b,__g,__r = 0,0,0
#go through each pixel
for index,x in np.ndindex(__pixels.shape):
    if x == 0:
        __b = __pixels[index,x]
    elif x == 1:
        __g = __pixels[index,x]
    elif x == 2:
        __r = __pixels[index,x]
        #Change the color of w
        print(__pixels.shape[0]," ",index)
        img[np.where(((np.square(img[0]-__b))+(np.square(img[1]-__g))+(np.square(img[2]-__r))) <= TOLs).all(axis=2)] = [255, 255, 255]

I'm getting this error

img[np.where(((np.square(img[0]-__b))+(np.square(img[1]-__g))+(np.square(img[2]-__r))) <= TOLs).all(axis=2)] = [255, 255, 255]
AttributeError: 'tuple' object has no attribute 'all'

Can anyone guide me?