How to save patches of all images in a single .mat file?
1 view (last 30 days)
Show older comments
i have a code that divides image into patches of size 32*32 and also divide the corresponding labeled image into same size of patches. i want to save and concatenate all the patches of all the images in a .mat file, but my code is saving only the patches of last image. please can someone help me with this problem?
a = dir('C:\Users\admin\Desktop\img\*.png');
b = dir('C:\Users\admin\Desktop\lbl\*.png');
for i = 1:length(a)
filename=strcat('C:\Users\admin\Desktop\img\',a(i).name);
image=imread(filename);
R = image(:,:,1);
G = image(:,:,2);
B = image(:,:,3);
Y = 0.299 * R + 0.587 * G + 0.114 * B;
U = -0.14713 * R - 0.28886 * G + 0.436 * B;
V = 0.615 * R - 0.51499 * G - 0.10001 * B;
image = cat(3,Y,U,V);
[image_patches] = hellopatches(image, 32, 20);
filenamel=strcat('C:\Users\admin\Desktop\lbl\',b(i).name);
label=imread(filenamel);
[labels] = hellopatches_label(label, 32, 20);
data = cat(1, image_patches,labels);
end
data = data';
data = shuffleRow(data);
save data.mat;
0 Comments
Answers (2)
neuroDuck
on 15 Jul 2020
Edited: neuroDuck
on 15 Jul 2020
Hi Hirra,
You are overwriting 'data' every time the loop runs. Simply index it - e.g. data(i) - or add it to a cat() to make every image accessible outside of the loop.
Kind regards.
10 Comments
Walter Roberson
on 18 Jul 2020
At present, you create a 3073 x 5625 array for the first image, and a 3073 x 5184 array for the second image.
We know from the above discussion that you do not want to put those two arrays as seperate entries in a cell array.
How would you like to store the 3073 x 5625 array together with the 3073 x 5184 arrary ? Would you want the result to be a 3073 x 10809 array?
I cannot seem to find your source code for hellopatches in the discussion, so I have attached it. It appears to me that since your images are not all the same size, that we should not be necessarily be expecting that the output of hellopatches will always have 3073 rows or 5625 columns.
Walter Roberson
on 15 Jul 2020
data = cat(1, image_patches,labels);
That code is overwriting all of data each iteration.
It looks to me as if data will not be a vector, but I do not seem to be able to locate any hellopatches() function so I cannot tell what shape it will be. As you are not doing imresize() I also cannot be sure that all of the images are the same size, so I cannot be sure that data will be the same size for each iteration. Under the circumstances you should be considering saving into a cell array.
Note: please do not post the same query multiple times. I found at least 9 copies of your query :(
10 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!