Storing Images from Cell Array into Different Files

Hi,
I have already divided a picture into 100 smaller pictures and stored each image in a 10x10 cell array. I am now trying to save them to my computer as a file using imwrite.
[m,n] = size(Pgray);
Blocks = cell(m/100,n/100);
counti = 0;
for i = 1:100:m-99
counti = counti + 1;
countj = 0;
for j = 1:100:n-99
countj = countj + 1;
Blocks{counti,countj} = Pgray(i:i+99,j:j+99);
end
end
I think I need to create a for loop that does this for each cell, but I'm not sure how:
imwrite(Blocks{1,1}, 'Image1.jpg')
I want it to do this for every image of cell array size 10x10 so that it stores them into a folder and is labelled 'Image(1 through 100).jpg'.
Thanks in advance!

1 件のコメント

Eric Martz
Eric Martz 2019 年 7 月 26 日
Just to clarify, it's dividing the pictures how I want it to. All i need help with is how to write a for loop to store them in a folder.

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

 採用された回答

awezmm
awezmm 2019 年 7 月 27 日

0 投票

Here is a for loop that generates a different filename in each iteration:
for i = 1:100
newfilename = strcat('Image', num2str(i), '.jpg')
imwrite(img, newfilename)
end
In this example, strcat combines multiple strings into one string and num2str converts a number to string.
Make sure you are imwriting these files in the correct output directory. An easy way to do this is by putting the path to the output directory in which you want to save stuff, using the cd command, before the for loop.
cd('your_path_to_outputdirectory')

7 件のコメント

Eric Martz
Eric Martz 2019 年 7 月 29 日
Hi awezmm,
Thanks for your answer. It correctly named the images, but does not access the cell array correctly. Imwrite does not work for cell arrays, but using cell2mat only returns the full image again. I want to have it imwrite each of the 100 different images.
for i = 1:100
newfilename = strcat('Image', num2str(i), '.jpg');
for j = 1:10
for k = 1:10
imwrite(cell2mat(Blocks(j,k)), newfilename);
end
end
end
This code almost works, but it only stores the last iteration and makes every Image the same block of the original image.
Any suggestions?
awezmm
awezmm 2019 年 7 月 30 日
imwrite(Blocks{j,k}, newfilename) does not work for you?
What is the error?
Eric Martz
Eric Martz 2019 年 7 月 30 日
It only stores the final image, when j and k equal 10 (the bottom right corner of the image). It stores this image for all 100 image files instead of assigning a different block to each image.
awezmm
awezmm 2019 年 7 月 31 日
Are you sure that each image in Blocks {j, k} is different? An easier way to split images into split images would be to use this package: https://www.mathworks.com/matlabcentral/fileexchange/35085-mat2tiles-divide-array-into-equal-sized-sub-arrays
Eric Martz
Eric Martz 2019 年 8 月 2 日
Yes, they are all different. I tried the mat2tiles and it works the same as my function, so they both return the desired images in a 10x10 cell. I just can't figure out the correct for loop to imwrite them into a folder.
awezmm
awezmm 2019 年 8 月 3 日
Ok I think I understand the problem. Your Blocks Cell is two dimensional and it may be confusing to go through every element. You need to make it into a 1-D vector
First, create the Blocks cell array {} like you normally did.
After that make the Blocks cell one dimension by doing:
oneDBlocks = Blocks(:)
Then you can a for loop easily to write all the images using oneDBlocks
for i = 1:100
newfilename = strcat('Image', num2str(i), '.jpg')
imwrite(oneDBlocks{i}, newfilename)
end
Here is a full example where I take a 100x100 image and divides it into 100 separate 10x10 images and save it in a directory
% making a 100 x 100 image using preloaded Matlab logo image
img = imread('logo.tif');
img = imresize(img, [100 100])
% using mat2tiles to create a cell array that is holding all the subimages that are 10x10 each
Blocks = mat2tiles(img, [10, 10])
% making Blocks into 1D vector
% I think the absence of this is what is causing you trouble
oneDBlocks = Blocks(:)
% making sample output directory
mkdir("example1Dout")
%looping through 1 Dimensional vector and saving each element
cd("example1Dout")
for i = 1:length(oneDBlocks)
newfilename = strcat('Image', num2str(i), '.jpg')
imwrite(oneDBlocks{i}, newfilename)
end
Let me know if you are still having trouble.
Eric Martz
Eric Martz 2019 年 8 月 5 日
Yes! Thank you, it works!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeImages についてさらに検索

質問済み:

2019 年 7 月 26 日

コメント済み:

2019 年 8 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by