3

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);
SimaGuanxing
  • 673
  • 2
  • 10
  • 29

2 Answers2

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).

Floris
  • 45,857
  • 6
  • 70
  • 122
  • 1
    See [logical indexing](https://nl.mathworks.com/help/matlab/math/matrix-indexing.html#bq7egb6-1) for more information. – m7913d May 01 '17 at 16:10
0

@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.