フィルターのクリア

Change file names before saving

1 回表示 (過去 30 日間)
no zoop
no zoop 2019 年 10 月 21 日
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

回答 (1 件)

Image Analyst
Image Analyst 2019 年 10 月 21 日
編集済み: Image Analyst 2019 年 10 月 21 日
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 件のコメント
Stephen23
Stephen23 2020 年 2 月 15 日
編集済み: Stephen23 2020 年 2 月 15 日
@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']));
Louis-Philippe Guinard
Louis-Philippe Guinard 2020 年 2 月 18 日
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!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by