Subtracting image elipses from each other using numpy masks

Viewed 118

I am trying to calculate the area within a specific region by utilizing masks in shape of elipses, and taking the mean of the values inside the mask. Like this:

enter image description here

This is an eye image that is originally:

enter image description here

What i want to do is calculate the area of the sclera and iris separately. The way i want to do it is by generating masks, one just for the iris a second for the entire eye and a third to subtract the iris mask from the entire eye mask to obtain the sclera mask, where the first mask is just the iris region, the second area is the entire eye and the third will be a subtraction, which is exactly i am tring to do. Through subtraction achieve the area of the sclera separately.

The problem is that my mask function returns me boolean values, this what i was trying to do:

from PIL import Image 
  
img = Image.open(r'imgpath')  
  
 
h_1 = 16
k_1 = 31
a_1 = 7
b_1 = 17

#elipse function
def _in_ellipse(x, y, h, k, a, b):
    z = ((x-h)**2)/a**2 + ((y-k)**2)/b**2
    if z < 1:
        return True
    else:
        return False
    
in_ellipse = np.vectorize(_in_ellipse)
img = np.asarray(img)
mask = in_ellipse(*np.indices(img.shape), h_1,k_1,a_1,b_1)

#Visualize the mask size
plt.imshow(mask)
plt.show()


#See if its inside the boundaries
plt.imshow(np.where(mask, img, np.nan))
plt.show()

mask_mean = np.nanmean((np.where(mask, img, np.nan)))

What i am trying to do is before calculating the mean values, i want to grab the mean value of the sclera alone, an attempt was through subtraction of the two areas, but elipse function does not return pixel values as expected, it returns boolean values:

mask:

array([[False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       ...,
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False]])
1 Answers

From what I understand, to generate the sclera_mask from eye_mask and iris_mask, both of which are of boolean type, your idea of subtraction translates to a logical, element-wise XOR operation on the two masks:

sclera_mask = np.logical_xor(eye_mask, iris_mask)

More on this in the docs: numpy.logical_xor

Related