Organizing images in datastore and accessing them with parameters

조회 수: 11 (최근 30일)
Alejandro Fernández
Alejandro Fernández 2021년 4월 14일
편집: per isakson 2021년 4월 15일
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.

답변 (1개)

per isakson
per isakson 2021년 4월 15일
편집: per isakson 2021년 4월 15일
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.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by