How to apply image processing algorithms on the ROI labeled based on a binary mask in Matlab?

老子叫甜甜 提交于 2019-12-12 14:38:54

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!