Overwritting problem in for loop

1 回表示 (過去 30 日間)
DIMITRIOS THEODOROPOULOS
DIMITRIOS THEODOROPOULOS 2019 年 3 月 16 日
コメント済み: Jan 2019 年 3 月 18 日
I simply crop images and save them with a number.For example i have Grain #1,Grain #2,Grain #3.If i move to another image and crop further images i cannot create images Grain #4 ,Grain #5 etc and instead there are overwritten images...Here is my code.Where is my mistake??
clear all;
clc;
addpath(genpath('C:\Users\User\Desktop\TEI'));
imtool close all;
k=input('How many times do you want to repeat the cropping?');
IMAGE = uigetfile('*.jpg','Pick an image');
A=imread(IMAGE);
figure,imshow(IMAGE);
folder=('C:\Users\User\Desktop\TEI\BEE_POLLEN _PROJECT\Scripts');
for i=1:k
f = sprintf('Select a ROI and double click to crop #%d',i);
uiwait(warndlg(f));
[B, rect] = imcrop(A);
baseFileName = sprintf('Grain #%d.jpg',i);
fullFileName = fullfile(folder, baseFileName);
baseFileName2 = sprintf('Grain #%d .jpg', (i+1));
fullFileName2 = fullfile(folder, baseFileName2);
if exist(fullFileName)
imwrite(B, fullFileName2);
else
imwrite(B, fullFileName);
end
if i==k
f=sprintf('FINISHED!!');
uiwait(warndlg(f));
end
end
  4 件のコメント
dpb
dpb 2019 年 3 月 16 日
At least give it a go with the supplied hints..."nothing ventured, nothing gained!"
Specific Q? if/when you have a specific problem, sure; just write code for another's job?, "not so much"...
Jan
Jan 2019 年 3 月 16 日
編集済み: Jan 2019 年 3 月 16 日
The clear all will impede the method of using a persistent variable. Because this brute clearing of everything has other severe disadvantages also, omitting it is a good idea in general.
Adding a set of folders automatically to Matlab's path can cause problems, when any of the included folders contain M-files:
addpath(genpath('C:\Users\User\Desktop\TEI'));
Do you do this only to access the images in these folders? Then leave the path untouched. You are using absolute file names already, so do not pollute the path without needs.

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

採用された回答

Jan
Jan 2019 年 3 月 16 日
編集済み: Jan 2019 年 3 月 16 日
nExisting = numel(dir(fullfile(folder, 'Grain #*.jpg')));
for i = 1:k
f = sprintf('Select a ROI and double click to crop #%d',i);
uiwait(warndlg(f));
[B, rect] = imcrop(A);
baseFileName = sprintf('Grain #%d.jpg', i + nExisting);
fullFileName = fullfile(folder, baseFileName);
if exist(fullFileName, 'file') % with 'file' !!!
error('Collision with filename: %s', fullFileName);
end
imwrite(B, fullFileName);
end
f = 'FINISHED!!'; % without sprintf
uiwait(warndlg(f));
Or use https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort (link) tofind the largest index in the file names and start counting there. (I'd prefer this.)
Another idea is to check the existence of a file name dynamically:
count = 1;
for i = 1:k
...
isFree = false;
while ~isFree
baseFileName = sprintf('Grain #%d.jpg', count);
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file') % with 'file' !!!
isFree = true;
end
count = count + 1;
end
end
It is not tricky, as you can see.
  2 件のコメント
DIMITRIOS THEODOROPOULOS
DIMITRIOS THEODOROPOULOS 2019 年 3 月 17 日
編集済み: DIMITRIOS THEODOROPOULOS 2019 年 3 月 17 日
Thank you very much!!!!!I.Very clever!II couldnt not find how to count the existing files and the first command in the first solution was the answer!!
...Now i will try the others!
Jan
Jan 2019 年 3 月 18 日
You are welcome.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by