问题
I have a binary mask which labeled the foreground of image. Many image processing algorithms such as histogram equalization or otsu method deal with the whole image. My question is how to apply those image processing algorithms so that they can ONLY process the region which my binary mask labeled?
For example, I
is the grayscale image and BW
is the binary mask. The code below still process the whole image rather than the specific region labeled by the BW
mask.
level = graythresh(I.*BW);
BW = im2bw(I.*BW,level);
回答1:
The problem with your code is that you are just setting elements of the image to zero. Instead, you should only pass the voxels of interest to the grayscale
algorithm. For example, if BW
is nonzero in the ROI, you can say
level = graythresh(I(BW>0));
That will select only the elements you want for the threshold calculation. It is shorthand for
level = graythresh(I(find(BW>0)));
This second form of the expression creates an intermediate array with the indices - which is usually slower than using logical indexing (which is what this kind of index is called).
回答2:
@SimaGuanxing, you can also achieve the same by following:
level = graythresh(I(BW));
But you have to make sure that BW is a matrix of the same size as I with logical values as entries.
来源:https://stackoverflow.com/questions/43722268/how-to-apply-image-processing-algorithms-on-the-roi-labeled-based-on-a-binary-ma