Update, I've updated the answer to work with binary or gray scale images. Notice that image intensities are now just scalars instead of (R, G, B) values and all images, masks and structure elements are 2d-arrays instead of 3d arrays. You may need to adjust the value of white_pixel
(or otherwise modify this code to suit your needs).
import numpy as np
from skimage.morphology import binary_dilation
# Setup
coordx = [110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 100, 101, 102,
103, 104, 105, 106, 107, 108, 109, 110]
coordy = [110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120]
img = np.random.random((128, 128))
img[110, 110] = 1.
img[109, 110] = 1.
# values grater than white_pixel will get detected as white pixels
white_pixel = 1
mask = np.zeros((128, 128), dtype=bool)
mask[coordx, coordy] = 1
structure = np.ones((7, 7))
mask = binary_dilation(mask, structure)
is_white = (img * mask) >= white_pixel
# This will tell you which pixels are white
print np.where(is_white)
# This will tell you if any pixels are white
print np.any(is_white)
Original answer:
You only need to use numpy.where
if you wan to know which pixels are white. I would just multiply the image by a mask and use np.any
, something like this:
# Setup
coordx = [110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 100, 101, 102,
103, 104, 105, 106, 107, 108, 109, 110]
coordy = [110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120]
white_pixel = np.array([255, 255, 255])
img = np.random.randint(0, 256, (128, 128, 3))
img[110, 110, :] = 255
img[109, 110, :] = 255
mask = np.zeros((128, 128, 1), dtype=bool)
mask[coordx, coordy] = 1
structure = np.ones((7, 7, 1))
mask = binary_dilation(mask, structure)
is_white = np.all((img * mask) == white_pixel, axis=-1)
# This will tell you which pixels are white
print np.where(is_white)
# This will tell you if any pixels are white
print np.any(is_white)