I am able to get matlab to resize and save an individual image, but I'm wondering how I can get it to do this for all images in a folder without having to type in the names one by one. The names of the saved & original images must stay the same if that makes any difference. Could I load the images into an array and then have it loop through each image? Sorry if this is a dumb question!

 Akzeptierte Antwort

Jon
Jon am 28 Sep. 2015

0 Stimmen

Yep. Try the following function:
dc = dir('*.jpg');
but set the filetype to whatever the images are. Make sure your working directory is the one with the images, obviously. The 'dc' variable will be structure that contains the file information, which will allow you to automate the loading, resizing, and saving of each image.

10 Kommentare

Victoria Lawlor
Victoria Lawlor am 28 Sep. 2015
Bearbeitet: Victoria Lawlor am 28 Sep. 2015
thanks so much for your reply! I just started learning matlab so I don't really understand what to do once I've used that function to load all of the images. The code for resizing one image looks like this:
function[]=resize_and_save(image_name,input_path,output_path)
%load image
image=imread([input_path image_name],'jpg');
%resize image
newimage=imresize(image,[352 260]);
%save new image
imwrite(newimage,[output_path image_name '.jpg'])
So I would replace the line that loads one image with the dir function, but then how can I get it to resize all of the images after that? Would I use the dc variable in the imresize and imwrite functions instead of image?
Jon
Jon am 28 Sep. 2015
Bearbeitet: Jon am 29 Sep. 2015
I would create a new script that looks something like this:
input_path = 'whatever';
output_path = 'whatever';
cd(input_path)
dc = dir('*.jpg'); % loads all the image infos
for i = 1:numel(dc) % loop through all your images to resize
resize_and_save(dc(i).name,input_path,output_path);
end
Then in your 'resize_and_save' function, I think you can remove the '.jpg' from each call, since it should be included in the dc().name.
Victoria Lawlor
Victoria Lawlor am 29 Sep. 2015
Wow thank you it worked that's really cool!
Image Analyst
Image Analyst am 29 Sep. 2015
There's no need for cd since you're using the input and output folders explicitly. See the FAQ: http://matlab.wikia.com/wiki/FAQ#Where_did_my_file_go.3F_The_risks_of_using_the_cd_function.
Jon
Jon am 29 Sep. 2015
Bearbeitet: Jon am 29 Sep. 2015
I disagree. The cd function is indeed necessary because the following command (dir) depends on the current path to return the image file properties.
Like the FAQ says, cd often causes problems. It is not needed before you call dir(). Here is the code without cd():
input_path = 'c:\whatever';
output_path = 'd:\whatever';
dc = dir([input_path, '*.jpg']); % loads all the image infos
Thorsten
Thorsten am 29 Sep. 2015
Bearbeitet: Thorsten am 29 Sep. 2015
You can call dir with the directory you like to examine; then you do not need cd, like this:
dir('/Applications/MATLAB_R2012a.app/toolbox/images/imdemos/*.png')
Jon
Jon am 29 Sep. 2015
I skimmed through the 'dir' help file and didn't pick up that this was possible. I learned something today. I use cd often without any problems, so never occurred to me to avoid it.
Image Analyst
Image Analyst am 29 Sep. 2015
For example, let's say your images were in C:\whatever, and your m-files were in C:\m-files. Now if you ran the script and called cd('c:\whatever') and then tried to call resize_and_save() function, it would not find it (unless you had made sure that folder was on the path) because the current folder is no longer C:\m-files.
I literally have hundreds of projects each in their own folder (because they're unrelated). I do not want to clutter my path with hundreds of folders, some of which I rarely use anymore, so usually I do not add my folder to the search path.
Jon
Jon am 29 Sep. 2015
I see. I've just been doing
cd('c:\whatever')
load data
cd('working_path')
I guess it's a little uglier and has the potential for problems if I'm running code line-by-line.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Image Analyst
Image Analyst am 28 Sep. 2015

0 Stimmen

See the FAQ for code to do this:
Put your calls to imread(), imresize() and imwrite() inside the loop.

1 Kommentar

Victoria Lawlor
Victoria Lawlor am 28 Sep. 2015
Like I mentioned before I haven't used matlab very much. I understand how I would load the whole file but I'm still not sure how I would have it go through each image individually. If the dir function loads the images into an array then maybe the issue is more that I don't understand how to go through each part of an array and perform some action.
I'm not sure what arguments I would put in the imread, imresize, and imwrite functions since before I had the image name.

Melden Sie sich an, um zu kommentieren.

anis martina
anis martina am 26 Mär. 2024
Bearbeitet: anis martina am 26 Mär. 2024

0 Stimmen

resize multiple image using in matlab image import in file code

1 Kommentar

Image Analyst
Image Analyst am 26 Mär. 2024
See the attached demo. It computes the average RGB image. If the second and subsequent images are of a different size than the first image, it will resize them to match the first image. You can easily not sum the images (because you don't need the mean) and add a call to imwrite to save them to disk, just create the full file name using fullfile and put them in different folder just in case you have some kind of mistake that you don't destroy the original.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Convert Image Type finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by