Saving 2D index ranges in a single variable

12 ビュー (過去 30 日間)
fi
fi 2023 年 1 月 19 日
編集済み: fi 2023 年 1 月 23 日
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 Center および File ExchangeImages についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by