Change file names before saving

Hi,
I am cropping a several collaged images simulatonouesly. I am able to successfully crop the images but do not like how the file names are saved. Lets say I have two collages in my folder that I want to crop...I crop them but when I go to save them it saves them as IMG_001_001 to how many images are cropped from the first image and IMG_002_001 to how many images are cropped from the 2nd image. The amount of blobs are saved in array [152, 148]. So the total number of images being cropped is 300, how would I make it so when the images are saved it goes from IMG_001 to IMG_300?
%%
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 : original_images
for k = 1 : numberOfBlobs(n)
thisBlobsBoundingBox = blobMeasurements{n}(k).BoundingBox;
subImage = imcrop(OG_images{n}, thisBlobsBoundingBox);
folder = 'C:/Users/Corey/Desktop/new zoop 2';
thisBaseFileName = sprintf('Image%03d_%03d.tif', n, k); % I know i would take out %03d_%03d and the n and k
% but how do I replace that or how do I the total numberOfBlobs
thisFullFileName = fullfile(folder, thisBaseFileName);
imwrite(subImage, thisFullFileName,'tif');
end
end
end

Answers (1)

Image Analyst
Image Analyst on 21 Oct 2019
Edited: Image Analyst on 21 Oct 2019
Try this:
%%
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.
folder = 'C:/Users/Corey/Desktop/new zoop 2';
if strcmpi(reply, 'Yes')
for n = 1 : original_images
thisBlobsBoundingBox = blobMeasurements{n}(k).BoundingBox;
subImage = imcrop(OG_images{n}, thisBlobsBoundingBox);
thisBaseFileName = sprintf('Image%03d_%03d.tif', n, numberOfBlobs(n));
% but how do I replace that or how do I the total numberOfBlobs
thisFullFileName = fullfile(folder, thisBaseFileName);
imwrite(subImage, thisFullFileName,'tif');
end
end

7 Comments

Ends up only saving the last image from each collage. Without using
for k = 1 : numberOfBlobs(n)
I believe it won't loop over all the blobs
I guess I don't understand. Why do you want to loop over all the blobs? Don't you want to name the image according to how many blobs are in the image?
Explain what a collage is. How many images are you dealing with?
A collage is an image of many other images. I want to loop over all the blobs in the collaged image to get each individual blob in the image. By having each individual image I can use then to train a neural net, when it is in collaged format you can't do that. I have 3 seperate collaged images and I am able to crop out each individual blob from each collage. The naming convention I am currently using labels the blobs based on the image number and the number of what blob it is in the collaged imaged (im using bwlabel() to label them). The first image has 100 blobs, the second image has 125 and the third image has 110, so in total 335 images. Instead of numbering based on the number of what blob it is in a image I want to label it based on the number of total blobs across the 3 collaged images.
^I hope that helps
So you say you have "3 seperate collaged images". Does that mean that you have 3 labeled images, each with N images stitched together? Or do you mean you have 1 image with 3 labeled images stitched together?
And then you want to create 335 images, each with just one blob in it? Not sure why you want that naming convention.
Anyway, to measure "the number of total blobs across the 3 collaged images" You'd have to relabel each image to determine the number of blobs in each stitched/collaged/tiled image. Then sum those up over all the images that you have.
Hello! I have a similar problem, so I think I can explain his problem another way (and get help in the process).
Basically I have several images (say, I = 10) in which I have many blobs (say, b = 50). My code may be a bit different from that of OP, but in my case I want to separate each blob, measure intensity levels for each blob, then plot some graphs for each individual blob and save each graph separately. This way, much like OP, I would like to have I*b = 500 graphs saved separately.
What he seems to want is to save as 'filename001.png' or something like that, through to 'filename500.png'. My issue is slightly different as I want to save my graphs as 'filename_image1_blob001.png' through to 'filename_image1_050.png', then again from 'filename_image2_001.png' and so on. My code somehow already deals with the name of the file up to 'filename_imageX' but for the last part, whatever I do ends up giving me a random number and overwriting again and again on that one last file, leaving me with only one graph at the end when I should have 50 (per image, that is).
My code is something like:
savefolder = 'some path for a folder'
save([savefolder,matfilelist(itmat).name(1:end-4),'_Results.mat'],'variable');
savefig([savefolder,matfilelist(itmat).name(1:end-4),'_Figures.fig']);
saveas(fig,[savefolder,matfilelist(itmat).name(1:end-4),'_Figures.png']);
I tried something like '_Figures', num2str('%03d', num2str(filenumber)), '.fig' but it didn't really work. I hope this clarifies a bit? Thanks in advance!
@Louis-Philippe Guinard:
  • Use fullfile to create the complete filename, rather than concatenating strings.
  • Use fileparts to remove file extension from a filename, rather than buggy indexing.
  • Read the num2str documentation, you current usage makes little sense. Or even better, use sprintf.
pnm = 'some path for a folder';
[~,fnm] = fileparts(matfilelist(itmat).name);
tmp = sprintf('%03d',filenumber);
save( fullfile(pnm,[fnm,'_Results',tmp,'.mat']), 'variable');
savefig(fig, fullfile(pnm,[fnm,'_Figures',tmp,'.fig']));
saveas( fig, fullfile(pnm,[fnm,'_Figures',tmp,'.png']));
Come to think of it, I really did write that line weird... Thanks for the heads-up! And thanks for the save as-is, I tried what you sent me and now it works!

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 21 Oct 2019

Community Treasure Hunt

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

Start Hunting!