Saving 2D index ranges in a single variable

15 visualizaciones (últimos 30 días)
fi
fi el 19 de En. de 2023
Editada: fi el 23 de En. de 2023
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?

Respuesta aceptada

Mathieu NOE
Mathieu NOE el 19 de En. de 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 comentario
fi
fi el 23 de En. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by