1

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.

Suever
  • 64,497
  • 14
  • 82
  • 101
  • have a look at the `fullfile` and `mkdir` help, if you still have problems come back with all your code from reading to writing – matlabgui Feb 29 '16 at 16:26

2 Answers2

2

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.

Suever
  • 64,497
  • 14
  • 82
  • 101
  • Thank you lots guys. Suever I used your help... I got this error message;: [ cell contents reference from a non cell array object]... That's for my output(cropped images). By the way I should have added, that my output variable is Logical. Please how do I make my Logical output a cell array? – Ogwuche Joseph Feb 29 '16 at 18:11
  • @OgwucheJoseph I don't see an error message in your comment. Please update. – Suever Feb 29 '16 at 18:13
  • I am getting error "Cell contents reference from a non-cell array object" ...for myoutput, which is a logical variable that contains cropped image – Ogwuche Joseph Feb 29 '16 at 19:06
  • @OgwucheJoseph as I said in my answer I assume that your images are a cell array. What are the dimensions of your logical variable? or rather, how do you store 20 of them? – Suever Feb 29 '16 at 19:07
  • i did imcrop in a loop for k=20images. When I run the codes... It completes it displaying all images. But when I run imshow it displays only one. Also when I do imwrite it saves only one. – Ogwuche Joseph Feb 29 '16 at 19:43
  • @OgwucheJoseph if you're doing the crop inside of the loop, then simply replaces `images{k}` above with your output variable and put the cropping code inside of my for loop. – Suever Feb 29 '16 at 19:44
  • Question is updated to be more specific to your issue. – Suever Feb 29 '16 at 19:47
  • Thank you Suever... I almost didn't notice. U will get back – Ogwuche Joseph Feb 29 '16 at 23:06
  • Thank you Suever... I almost didn't notice. So I did... It worked, but the resezedFolder is created as a Variable in the workspace. I wanted it created as a sub folder in my current dir. However if I can extract my images from the resizedFolder variable, its still okay. How do I extract my images. – Ogwuche Joseph Feb 29 '16 at 23:42
  • @OgwucheJoseph If you provide a relative path for the `resizedFolder` variable it will be in the current directory. To extract your images, you'll want to use `dir` and `imread`. That is really a different question than the one that you've posted here though. – Suever Feb 29 '16 at 23:45
  • Oh... I get you, that I am the one to provide a path for the folder to be created. You have really helped me on this one. It worked! Thank you Suever, and everyone. I am grateful. – Ogwuche Joseph Mar 01 '16 at 00:44
  • @OgwucheJoseph If this solution worked for your problem, consider marking it as the answer to help people who may stumble across this question with Google in the future! – Suever Mar 01 '16 at 03:27
0

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
  • It is best to not use `i` or `j` as loop indices in MATLAB http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab – Suever Feb 29 '16 at 19:50