How to apply image processing algorithms in ROI labeled based on binary mask in Matlab?
I have a binary mask that is labeled with the foreground of an image. Many image processing algorithms such as histogram equalization or the otsu method apply to the entire image. My question is how do I apply these image processing algorithms so that they can ONLY process the area marked by my binary mask?
For example, I
is a grayscale image and BW
is a binary mask. The code below processes the entire image, not a specific masked area BW
.
level = graythresh(I.*BW);
BW = im2bw(I.*BW,level);
source to share
The problem with your code is that you are just setting the image elements to zero. Instead, you should only pass the voxels of interest to the algorithm grayscale
. For example, if BW
it is not zero in ROI, you can say
level = graythresh(I(BW>0));
This will only select the items that you want to calculate the threshold. This is shorthand for
level = graythresh(I(find(BW>0)));
This second form of expression creates an intermediate array with indexes, which is usually slower than using logical indexing (which is what this type of index is called).
source to share