Saving 2D index ranges in a single variable
12 次查看(过去 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')
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);
Is there any way to do this, or are we stuck with having to use separate variables?
0 个评论
采纳的回答
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')
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!