0

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

enter image description here

enter image description here

enter image description here

user3470496
  • 141
  • 7
  • 33

1 Answers1

1

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');

The output: Output for example

Shaked
  • 475
  • 1
  • 3
  • 12
  • Thanks, this is similar to what I was doing initially..the issue I get when I do this (I'm using a stdfilt() filter and then do contrast stretching) is that I get a strange output from the processing around the edge of where the region of interest borders the background, also the contrast stretching seems skewed. Is there a way to keep the filter only convolving within the mask and ignoring the edges, and similarly to have the contrast stretching limited to the mask/region of interest? Thanks! – user3470496 Dec 28 '15 at 19:34
  • The borders of the mask are "strange" because you are using a filter on one side of the border but not on the other side of it. You can blur the borders using `imgaussfilt` on pixels near the border. You can find the border pixels using `bwperim` or `bwboundaries` on the mask `M`. You can "widen" the border pixels using `imdilate`. – Shaked Dec 29 '15 at 07:14
  • Regarding the contrast stretching - You can stretch only the filtered pixels using the mask. i.e. `I(M) = imadjust(I(M),stretchlim(I(M)),[]);`. This is possible because the contrast stretching operation doesn't require neighborhood information, meaning the order and "shape" of the pixels in the image doesn't affect the result. – Shaked Dec 29 '15 at 07:18
  • I've added the code I'm running and the output after running the filter/contrast stretch. It definitely looks like the stdev filter is incorporating finding strong standard deviation between the 0 background and the image within the mask rather than only filtering within the confines of the mask. – user3470496 Dec 29 '15 at 17:37
  • Can you please add the input image? – Shaked Dec 30 '15 at 13:14
  • yup you got it, added – user3470496 Dec 30 '15 at 16:04