According to the formula that is shown below, I need to calculate an average threshold value by dividing the sum of intensity values in segment on the number of pixels in segment.
where Xi' is a binary mask (structure_mask), |Xi'| is a number of ones (xi_modulus).
I(x,y) is a pixel intensity.
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
...
...
structure_mask = np.logical_and(magnitude_mask, intensity_mask).astype(np.uint8)
xi_modulus = np.count_nonzero(structure_mask.all(axis=2))
intensity_sum # = ??
How to calculate the sum of intensities with numpy?
EDITED: Based on the @HansHirse's answer I've tried to do the following:
thresh_val = np.mean(img_gray[structure_mask])
And I've got IndexError: too many indices for array
Where structure_mask was of shape (1066, 1600,1) and img_gray -
(1066,1600)
UPDATED: Just a dummy mistake. Shape mismatch was fixed by proper indexing
structure_mask = np.logical_and(magnitude_mask, intensity_mask)[:, :, 0]
