Storing Images from Cell Array into Different Files

조회 수: 9(최근 30일)
Eric Martz
Eric Martz 2019년 7월 26일
댓글: Eric Martz 2019년 8월 5일
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일
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년 8월 5일
Yes! Thank you, it works!

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by