I have a greyscale image (I), and would like to apply different filters to different regions of that image in matlab (R2015b) (random and irregular shaped regions). I have a binarized version of what I would like the first filter applied to (attached). I'm not sure the best way to make a mask...I can load this binary image and use bwconncomp to locate connected components and define them as single vectors which won't work with poly2mask. Any suggestions for A. how to get a mask out of this binary image and B. how to use this mask to apply a filter to that part of the greyscale image?
Thanks in advance!
MASK=imread('/Users/bk/Desktop/FIJI_image/mask4.tif');
BACK=imcomplement(MASK);
I=imread('/Users/bk/Desktop/FIJI_image/Orig.tif');
I(~MASK)=0;
SE=ones(13,13);
A=stdfilt(I, SE);
minZ=min(min(A));
maxZ=max(max(A));
Low_High=[minZ maxZ];
var5=255/maxZ;
B=uint8(A*var5);
C=(imadjust(B,stretchlim(B),[]));
imtool(C);
A binary image is a mask.
Given a grayscale image I
and a binary image M
with the same size, you can get the image I
filtered by the mask M
using:
J = I;
J(~M) = 0;
This is just masking. For filtering you can apply a filter on I
with imfilter
or any other one of MATLAB's filter functions. For example:
h = fspecial('motion', 50, 45);
K = imfilter(I, h);
Now you can get the original values of the pixels which are not in M
:
K(~M) = I(~M);
So now K
have filtered pixels in the locations where M
is true, and untempered pixels in the locations where M
is false.
Code for the example you added:
inputDir = 'temp2';
I = imread(fullfile(inputDir, 'PJlUm.png'));
M = imread(fullfile(inputDir, 'ewSPv.png'));
M = logical(M); % Convert M to a logical matrix, i.e. a mask.
Imasked = I;
Imasked(~M) = 0;
ImaskedAndStretched = Imasked;
ImaskedAndStretched(M) = imadjust(ImaskedAndStretched(M),stretchlim(ImaskedAndStretched(M)),[]);
IstretchedAtMask = I;
IstretchedAtMask(M) = ImaskedAndStretched(M);
figure;
subplot(3,2,1);
imshow(I);
title('Input Image');
subplot(3,2,2);
imshow(M);
title('Mask');
subplot(3,2,3);
imshow(Imasked);
title('Image Masked');
subplot(3,2,4);
imshow(ImaskedAndStretched);
title('Image Masked & Stretched');
subplot(3,2,5);
imshow(IstretchedAtMask);
title('Image Stretched At Mask');