Saving 2D index ranges in a single variable

14 次查看(过去 30 天)
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?

采纳的回答

Mathieu NOE
Mathieu NOE 2023-1-19
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 个评论
fi
fi 2023-1-23
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by