Organizing images in datastore and accessing them with parameters

11 Ansichten (letzte 30 Tage)
Alejandro Fernández
Alejandro Fernández am 14 Apr. 2021
Bearbeitet: per isakson am 15 Apr. 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.

Antworten (1)

per isakson
per isakson am 15 Apr. 2021
Bearbeitet: per isakson am 15 Apr. 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 ) )
end
end
%% create a list of file specifications
sad = dir( 'd:\m\cssm\AF\Img*.bmp' );
ffs = fullfile( {sad.folder}, {sad.name} );
%% and test the wrapper
wrp = Wrapper( ffs );
[img,ffs] = read_xy( wrp, 250, 70 );
outpts "the concrete image" and its file name
file =
'd:\m\cssm\AF\Img_250X_070Y.bmp'
where
classdef Wrapper < handle
properties
image_datastore
loc
end
methods
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 ) );
end
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;
end
end
end
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.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by