Changing file save name

3 views (last 30 days)
no zoop
no zoop on 15 Feb 2021
Moved: Voss on 2 Jan 2024
I have 10 images (collages), within each image there is anywhere from 20-200 smaller images. In total there are 3000 images between all 10 images.
Currently my code crops the small images from the collages and I know have 3000 images. When I save each of the small cropped image is saves it like... NAME%collage1%001, NAME%collage1%002,...,NAME%collage10%200.
I would like to save each of the small cropped images as... NAME%001, NAME%002,..., NAME%3000.
sum_blob = sum(numberOfBlobs) % I use this to get the sum of the number of blobs on each of the 10 collage images
message = sprintf('Would you like to crop out and save each individual images?');
reply = questdlg(message, 'Extract Individual Images?', 'Yes', 'No', 'Yes');
% Note: reply will = '' for Upper right X, 'Yes' for Yes, and 'No' for No.
if strcmpi(reply, 'Yes')
for n = 1 : non_binary_images
for k = 1 : numberOfBlobs(n) % Loop through all blobs.
% Find the bounding box of each blob.
thisBlobsBoundingBox = blobMeasurements{n}(k).BoundingBox; % Get list of pixels in current blob.
% Extract out this zoop into it's own image.
subImage = imcrop(OG_images{n}, thisBlobsBoundingBox);
% Saving the cropped image
folder = 'folder';
thisBaseFileName = sprintf('NAME%4d_%03d%.tif', n, k);
thisFullFileName = fullfile(folder, thisBaseFileName);
imwrite(subImage, thisFullFileName,'tif');
end
end
end

Accepted Answer

dpb
dpb on 15 Feb 2021
Moved: Voss on 2 Jan 2024
Just add a counting variable that is incremented inside the inner loop--
...
j=0;
for n = 1 : non_binary_images
for k = 1 : numberOfBlobs(n) % Loop through all blobs.
j=j+1;
...
thisBaseFileName = sprintf('NAME%04d%.tif', j);
...
  2 Comments
no zoop
no zoop on 15 Feb 2021
Moved: Voss on 2 Jan 2024
This is great! Thank you.
I did however find when using
'NAME%04d%.tif'
the second % ended up saving the images as a file instead of a tif, I fixed this by removing the second %.
'NAME%04d.tif'
dpb
dpb on 15 Feb 2021
Moved: Voss on 2 Jan 2024
Yeah, just a typo in cleaning up existing format string...

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!