Organizing images in datastore and accessing them with parameters

4 visualizaciones (últimos 30 días)
Alejandro Fernández
Alejandro Fernández el 14 de Abr. de 2021
Editada: per isakson el 15 de Abr. de 2021
Hello everyone. I had the following question and was wondering if someone could help me solve it. Let me explain below.
In a certain folder I have several images and all of them have a name of this type: IMG_0X_0Y.bmp
In which X can take any integer value between 0 and 360 and Y can take any value between 0 and 100.
What I would like to do is to be able to save all those images in a structure of type:
imds = imageDatastore(location)
And that saying the value of the variable Y and of the variable X I can load the information of that concrete image.

Respuestas (1)

per isakson
per isakson el 15 de Abr. de 2021
Editada: per isakson el 15 de Abr. de 2021
The statement
imds = imageDatastore(location)
creates an instance of the class, ImageDatastore. My first idea was to subclass, ImageDatastore. and add a new reading method. However, that isn't possible, since the class is sealed, which mean that we cannot inherit from it.
An alternative is to make a wrapper that has an instance of the class, ImageDatastore. as a property value.
%% create sample files
for yy = 10:10:100
for xx = 10:10:360
copyfile( 'Img_000X_000Y.bmp', sprintf( 'Img_%03dX_%03dY.bmp', xx, yy ) )
%% create a list of file specifications
sad = dir( 'd:\m\cssm\AF\Img*.bmp' );
ffs = fullfile( {sad.folder}, {} );
%% and test the wrapper
wrp = Wrapper( ffs );
[img,ffs] = read_xy( wrp, 250, 70 );
outpts "the concrete image" and its file name
file =
classdef Wrapper < handle
function this = Wrapper( filespecs )
this.image_datastore = imageDatastore( filespecs );
cac = regexp( filespecs, 'd:\\m\\cssm\\AF\\Img_(\d{3})X_(\d{3})Y.bmp', 'tokens','once' );
cac = vertcat( cac{:} );
cac = cellfun( @(sx,sy) [str2double(sx),str2double(sy)], cac(:,1), cac(:,2), 'uni',false );
this.loc = struct( 'xy',cac, 'ffs',reshape( filespecs, [],1 ) );
function [img,ffs] = read_xy( this, x, y )
[~,pos] = ismember( [x,y], vertcat( this.loc.xy ), 'rows' );
% img = imread( this.loc(pos).ffs );
img = readimage( this.image_datastore, pos );
ffs = this.loc(pos).ffs;
If its enough to fullfill a bare minimum the requirements of your question¨ the class imageDatastore is not needed. Comment out all rows that refer to the property image_datastore and use imread(). Replace ismember() by ismembertol() so that the x a y values don't need to exact.


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


Community Treasure Hunt

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

Start Hunting!

Translated by