Ogwuche Joseph
Ogwuche Joseph

Reputation: 23

Saving logical output images into a new (mkdir) folder

I used imresize to resize about 20images. I want the resized images to be stored in a new folder I created. Here is the code I used:

imwrite(myoutput, 'resized.png');

Now my problem us that, I only get one image written to the working directory, named 'resized.PNG'. I want all the 20 resized images to be saved.

I also want them saved in a new folder defined by mkdir(resizedFolder)... Which I don't know how to do.

Here is an excerpt from my codes:

dirD=dir('*.jpeg');
for k=1:length(dirD);  %k=20      %technically
    %i ran a long code to find a       %rectangular boundary, and cropped.

    CropIm=imcrop(I, thisBlobsBoundingBox);
    resizedIm=imresize(CropIm, 0.1);
end

Now, I want resizedIm to be stored in resizedFolder as individual images which should give me 20 images.

Upvotes: 1

Views: 566

Answers (2)

Suever
Suever

Reputation: 65430

You're going to want to use fullfile to combine the directory and filenames. Also you will want to create a custom filename for each image. Below I assume that all of your images are in a cell array.

resizedFolder = '/path/to/folder';

% Create folder if it doesn't exist
if ~exist(resizedFolder, 'dir')
    mkdir(resizedFolder);
end

dirD = dir('*.jpeg');

for k = 1:numel(dirD);
    % Your code to get the boundary
    CropIm = imcrop(I, thisBlobsBoundingBox);
    resizedIm = imresize(CropIm, 0.1);

    % Create a custom filename for this image.
    filename = sprintf('resized%02d.png', k);
    imwrite(resizedIm, fullfile(resizedFolder, filename));
end

This will create images in the folder that you specify with the filenames being: resized01.png, resized02.png, ...

Update: I have updated my response to be more specific to your initial code.

Upvotes: 2

James Edward West III
James Edward West III

Reputation: 46

I would store your current directory to a variable like

curPath = cd;
newPath = newDirectory;
cd(curPath);
cd newPath;

After this, your working directory is the new folder. Run the loop to save your files.

Make sure you are iterating the file names.

for i = 1:20
    imwrite(myoutput(i), ['resized', num2str(i),'.png']);
end

Upvotes: 0

Related Questions