MATLAB Answers

## matlab newbie - trying to create an ROI mask from an average value of multiple images

Asked by Alex

### Alex (view profile)

on 2 Apr 2013

I am trying to create an ROI mask that will read in multiple image files that were taken over time, average all those files into a single array, create an ROI mask from the average image, then use that mask to go back to each individual image and analyze the image using only the ROI created from the average mask. Here is what I have so far:

```NUM=0;
for i=1:13
img(:,:,i)=read_LUM2;
Lmask=mean(img,3);
Lmask(Lmask<415)=0;
Lmask(Lmask~=0)=1;
Lmask=logical(Lmask);
NUM=NUM+1;
L(NUM)=mean2(imgL(Lmask));
end
avgL=L(1:13)'
```

The problem is that I can't figure out how to get Lmask to wait until all files have been read to average, and then still use Lmask in each iteration. Any suggestions?

## 1 Answer

### Image Analyst (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

on 2 Apr 2013

But your "average" mask is only up through image #i, not over all 13. Why is that? Also I don't know how read_LUM2 gives you a different image each time but I guess I'll just assume it does (like maybe there are persistent variables inside the function that remember that last value). Since the average image is only up to and including image #i, I'm not sure why you threshold each "average" image at 415. And these three lines:

```Lmask(Lmask<415)=0;
Lmask(Lmask~=0)=1;
Lmask=logical(Lmask);
```

could be done with one single line

```LMask = LMask >= 415;
```

Next, imgL(Lmask) is not a 2D matrix. It's a 1D list of all the pixels in the mask, so you can use mean() instead of mean2().

Finally, you don't need NUM at all. Just use i everywhere you use NUM.

But to do what you say, you need two loops. The first one to go over all the images and find the mean image. Then create the mask. Then have another loop to process all the images with the mask.

Alex

### Alex (view profile)

on 2 Apr 2013

to answer your questions about read_LUM2, this is a completely separate user created function that reads in the image file that I interactively select before hand. The filtering at 415 happens because I am trying to filter out values in the image that are less than 415 because only values that are 415 or greater are in my ROI that I am trying to create the mask for. Also I don't think I can condense those lines into one, because then I lose the logical array that I am using for the mask.

When I do two loops, I should get a 13x1 array for avgL that has 13 unique values in it, corresponding to the average value in the ROI of each image. Instead I get a 13x1 array with each value exactly the same.

Also the fact that the average mask is only up through image #i is the question I am trying to have answered. How do I get it to average all images and then apply that mask to each image individually?

Image Analyst

### Image Analyst (view profile)

on 2 Apr 2013

Of course you can condense those 3 lines into 1 like I showed you. Just try it and see. I did give you guidance to do one loop, then get the mean, then create the mask, and then another loop to mask the images. Are you saying that the mean image should include only pixels brighter than 415? These must be 16 bit images then since regular 8 bit images don't have values over 255.

So for each image you will create a mask, rather than sum the images and create the mask on that sum? Why?

Where did you post your images?

What operations are you going to perform on your second pass through when you "analyze the image using only the ROI created from the average mask"?

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

New to MATLAB?