Masking images/matrixes

לק"י
Hello!
I got an image of some stained cells:
And I have a grayscale mask such as this:
(different shades are covering different cells as you can see).
I want to systematically go through each gray shade in the mask and to extract (or leave) the area in the cells image.
I looked up for a quick command, but haven't found one.
I tried to use find command, but I get vectors as a result and can't use it to "talk" with the original image matrix.
This is the code I'm using:
img=imread("4.7.23 ut cd4_prfrn acd3+11002c3.tif");
mask=imread("4.7.23 ut cd4_prfrn acd3+11002c3_cp_masks.png");
i=max(max(mask))
maskx=[];
masky=[];
[maskx, masky]=find(mask==i);
imgmasked=img(maskxy,masky)
(I'm using a for loop to go through all indexes, but it will come later, I want to succeed masking right now)
What is the right way to handle this problem please?
Thanks,
Amit.

2 Comments

Jon
Jon on 14 Jul 2023
Please attach your two files
לק"י
Hi jon,
I added them. The cell's picture was converted from TIF to png.
Thanks!

Sign in to comment.

Answers (1)

It looks like your mask is actually a labeled image since each blob seems to have a unique value (label). So in that case to get the area of each labeled blob you'd simply do
props = regionprops(labeledImage, 'Area');
allAreas = [props.Area] % Will give a vector of 5 areas.

5 Comments

לק"י
Hi Image analyst, thanks for the answer!
I don't need the area of each mask. I want to mask out from the original cell image the exact area found in each mask.
I'll try to ask it more clearly:
I have grayscale image of masks. each gray color represents a cell.
First I want to take the mask image and leave it only with one cell area mask. that means, if the range of intensities of the mask image is 5 (as in the example mask image), I want to zero out all values, except the value 1 for example, or 2, or 3 etc. I want to be left with only spefic value in the mask and zero all the rest.
It should look something like this:
[1 0 0 2 0
3 0 5 0 0
4 0 0 0 0] %--zero all values except the value 4 for example--->
[0 0 0 0 0
0 0 0 0 0
4 0 0 0 0]
Then I want to take this mask and zero out all the values in the cell's image according to mask.
It should go like this:
[25 3 14 8 0
2 24 0 0 0
65 12 73 72 0] %--zero all values except the pixel in the position corresponding to value 4 for in the zeroed mask--->
[0 0 0 0 0
0 0 0 0 0
4 0 0 0 0] %the zeroed mask I want to multiply/mask out by.
[0 0 0 0 0
0 0 0 0 0
65 0 0 0 0]
I hope I managed to explain myself better.
I know realize the last stage is just matrix multiplication. The first stage allways result in vectors (when using find command or other commadns).
Thanks,
Amit.
You can either have a loop over every value in your labeled image and mask off each blob to get the image pixels under that blob and have else be zero, so you have essentially a masked image, OR you can just get a list of pixel values for each blob all in one call to regionprops (no loop needed). It just depends if you really NEED the masked image or not, or if you just need the pixel values and don't necessarily need the image. Which way do you want to do it?
לק"י
Hi Image Analyst,
Thanks for the help!
I manged to do it like that:
imgthldtmp=zeros(size(imgmask));
thldfnd=find(imgfltrd>(thldmltplr(k)*pxllstavg));
imgthldtmp(thldfnd)=imgfltrd(thldfnd);
Is there a shorter command that could do it?
Thanks,
Amit.
Yes,
imgthldtmp=zeros(size(imgmask));
thldfnd= imgfltrd>(thldmltplr(k)*pxllstavg);
imgthldtmp(thldfnd)=imgfltrd(thldfnd);
Heck no. Shorter would be even more cryptic and hard to follow than it is now. No comments, cryptic variable names, etc. I don't know what you did but whatever, if it works for you, fine.

Sign in to comment.

Asked:

on 14 Jul 2023

Commented:

on 18 Jul 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!