Changing file save name

3 次查看(过去 30 天)
no zoop
no zoop 2021-2-15
移动Voss 2024-1-2
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

采纳的回答

dpb
dpb 2021-2-15
移动:Voss 2024-1-2
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 个评论
no zoop
no zoop 2021-2-15
移动:Voss 2024-1-2
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 2021-2-15
移动:Voss 2024-1-2
Yeah, just a typo in cleaning up existing format string...

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by