Saving 2D index ranges in a single variable

12 views (last 30 days)
Let's say I have an image stored as a matrix, and want save a rectangular region of interest that I can easily isolate from the image.
One simple way to do this would be this:
% Input image
rawImage = rand(50, 100);
% Save a 6px x 3px ROI
roi_yrange = [30:32];
roi_xrange = [1:3];
% Isolate the ROI from image and perform some kind of further
% analysis, e. g. calculate the mean value
r = rawImage(roi_yrange, roi_xrange); % isolated portion of image
avr_brightness_in_roi = mean(r, 'all')
avr_brightness_in_roi = 0.5230
That works fine, however, you always need two variables to store the ranges. Is there some way to store them in a single array, and do something like rawImage(roi_range)?
Obviously, concatenating the ranges into a matrix doesn't work if they have different lengths.
You can concatenate them into a cell array, but then indexing no longer works:
roi_range = {[30:32], [1:3]};
r = rawImage(roi_range);
Unable to use a value of type cell as an index.
Is there any way to do this, or are we stuck with having to use separate variables?

Accepted Answer

Mathieu NOE
Mathieu NOE on 19 Jan 2023
hello
why not this :
% Input image
rawImage = rand(50, 100);
% Save a 6px x 3px ROI
% roi_xrange = [1:3];
% roi_yrange = [30:32];
xyrang = [1 3 30 32]; % first two values are x min / max, second two values are y min / max
% Isolate the ROI from image and perform some kind of further
% analysis, e. g. calculate the mean value
% r = rawImage(roi_yrange, roi_xrange); % isolated portion of image
r = rawImage([xyrang(3):xyrang(4)], [xyrang(1):xyrang(2)]); % isolated portion of image
avr_brightness_in_roi = mean(r, 'all')
  1 Comment
fi
fi on 23 Jan 2023
That works, but isn't really what I was looking for – I specifically wanted to be able to do something like rawImage(roi_range) to make that indexing call short and readable.
But I guess there isn't really any way to achieve that and your answer answers my original question, so I'll mark this is solved.

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!