How to apply masks created by roipoly to movie?

7 views (last 30 days)
MF on 27 Feb 2022
Edited: AndresVar on 27 Feb 2022
I'm having issues understanding roipoly in general, so apologies if this is rudimentary and if I am making extremely basic errors.
I am trying to create four masks using roipoly in a forloop for one image (four areas of interest). After, I would like to create another loop inside this one that extracts pixel intensities in each ROI for each frame in a movie object and store this in a matrix (columns = # pixels in ROI, rows = pixel values for each frame).
Here is the code I have so far. It is extremely minimal, despite looking for similar questions here and watching tutorials. If anyone could advise a resource or solution, it would be greatly appreciated.
for i = 1:4
cell(i) = roipoly(I); % where 'I' is the image
% This doesn't appear to save four masks. Am I making a careless error?
for j = 1:length(movie)
pixelvalues = frame(mask);
% How would I create a matrix that stores pixel values?

Answers (2)

Image Analyst
Image Analyst on 27 Feb 2022
cell is a built-in function so don't use it as a variable name. The results of roipoly are not a single number. You need to use poly2mask() and then OR it with the main mask like
mask4 = false(size(I)); % initialize
then in the loop get your (x,y) coordinates from drawfreehand() or impoly() or whatever and then get the mask and OR it in
thisMask = poly2mask(x, y, rows, columns);
mask4 = mask4 | thisMask;
If you still can't figure it out after trying, write back with your new code.

AndresVar on 27 Feb 2022
Edited: AndresVar on 27 Feb 2022
You can store the roipoly result in a cell array like this
rois = cell(1,4)
for ii = 1:4
rois{ii} = roipoly(I);
Would it better to do get pixelvalues in a separate loop? otherwise you have to wait until movie is done collecting before you can get a new mask.
Also the pixelvalues matrix will be n-by-1, where n is the number of pixel in the ROI
mask = rois{1};
pixelvalues = frame(mask);

Community Treasure Hunt

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

Start Hunting!

Translated by