entropy in MATLAB already computes the Shannon Entropy for the entire image. What you need to do is remove the pixels from the image that are the background first. As such, simply create a mask that will segment the image so that we remove the background pixels, then compute the entropy for the rest. As such, we can use a combination of im2bw and graythresh to find our mask. im2bw converts a grayscale image into a binary image, which is essentially our mask, and graythresh finds the optimum threshold to convert the grayscale image into binary. Therefore, we can simply do this: