How to join or merge two Image Data Stores?

110 ビュー (過去 30 日間)
kowshik Thopalli
kowshik Thopalli 2017 年 2 月 26 日
編集済み: Matt J 2022 年 6 月 28 日
If I have two image datastores and I wish to concatenate them. How can I do that?
Thanks
  1 件のコメント
Sherin Aly
Sherin Aly 2018 年 1 月 13 日
It was a bit tricky but I was able to merge 2 datastores as follows:
imds1 = imageDatastore(location of DS 1,'IncludeSubfolders',true,'LabelSource','foldernames');
imds2 = imageDatastore(imds2 location,'IncludeSubfolders',true,'LabelSource','foldernames');
% create a dummy dataset with the same size of the combined datastores (if ds1 has 100 images and ds2 has 100 images then dummy has 200 images. I made a folder with some photos with equal number of the 2 datastores)
dummy = imageDatastore('Dummy data store locaation','IncludeSubfolders',true,'LabelSource','foldernames');
% note: below this line you can use any datastore variable you want to merge including the ones you get after splitting training and testing
dummy.Files=[imds1.Files;imds2.Files];
dummy.Labels=[imds1.Labels;imds2.Labels];
% Now dummy is the new combined datastore and you can use all datastore functions

サインインしてコメントする。

採用された回答

Zhiqiang Qiu
Zhiqiang Qiu 2018 年 1 月 22 日
I know it is an ancient problem, but I just to make it a better reference for other people.
imds1 = imageDatastore('imds1 location', ...);
imds2 = imageDatastore('imds2 location', ...);
imds_combine = imageDatastore({'imds1 location', 'imds2 location'}, ...);
Just combine the address of these two locations. The following parameters are all the same.
  1 件のコメント
habtamu miheretie
habtamu miheretie 2018 年 9 月 7 日
that is cool answer! i have very similar problem. when i create the data store the files are not in the same order as in the folder. how can i make that possible? i need it because the label i use is coming from another .csv file. so i need to match the order to process the images easily. can anyone help me on this please?

サインインしてコメントする。

その他の回答 (5 件)

Hana
Hana 2018 年 1 月 18 日
I met the same problem and saw your post. Finally I find out a way:
imds = imageDatastore(cat(1,imds1.Files,imds2.Files))
imds.Labels = cat(1,imds1.Labels,imds2.Labels)
It works for me.
  5 件のコメント
Joseph Garrett
Joseph Garrett 2022 年 1 月 9 日
perfect. thank you so much!!

サインインしてコメントする。


berkan
berkan 2017 年 5 月 22 日
allImages.Files=[test1.Files;test2.Files]; allImages.Labels=[test1.Labels;test2.Labels];
  1 件のコメント
Sascha D. Krauß
Sascha D. Krauß 2017 年 7 月 3 日
Unfortunately, this produces a struct instead of a new datastore - and I could not find a way to convert it back. This means, that you cannot use the datastore specific functions like "countEachLabel" etc. Does anyone know another solution?

サインインしてコメントする。


KSSV
KSSV 2017 年 2 月 27 日
A = imread('ngc6543a.jpg');
B = imread('peppers.png');
%%resize the images so that they have same dimensions
A_resize = imresize(A,[size(B,1) size(B,2)]) ;
%%join them
AB = [A_resize B] ;
imshow(AB) ;
  1 件のコメント
kowshik Thopalli
kowshik Thopalli 2017 年 2 月 27 日
Hi, Thanks for the answer, however I am looking to merge two
imageDatastore

サインインしてコメントする。


Aasim Khurshid
Aasim Khurshid 2020 年 12 月 30 日
編集済み: Matt J 2022 年 6 月 28 日
There is a better way to do this,
imds = imageDatastore({fullfile(imgFolder1Adress),fullfile(imgFolder2Adress),fullfile(imgFolder3Adress)},...
'IncludeSubfolders',true,'FileExtensions','.png','LabelSource','foldernames');

Matt J
Matt J 2022 年 6 月 28 日
編集済み: Matt J 2022 年 6 月 28 日
Perhaps by wrapping them in a TransformedDataStore (R2019a and higher),
imdsnew = transform(imds1,imds2, @(x) x)

Community Treasure Hunt

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

Start Hunting!

Translated by