필터 지우기
필터 지우기

rotating and saving images to folder: error during loop

조회 수: 6 (최근 30일)
Teshan Rezel
Teshan Rezel 2020년 1월 14일
댓글: Teshan Rezel 2020년 1월 15일
Hi folks,
I'm trying to read all files in a folder, rotatet them 90 degrees, then save them to the same folder. I need to do this 3 times (90, 180, 270 degrees).
The following is my code.
folder = "C:\Users\ME\Pictures\Filler\";
S = dir(fullfile(folder,'*.png'));
for k = 1:250
currentfilename = fullfile(folder,S(k).name);
I = imread(currentfilename);
I2 = imrotate(I, 90);
I2Name = sprintf('%d -1.png', k);
I2Path = fullfile(folder, I2Name);
imwrite(I2, I2Path);
I3 = imrotate(I, 180);
I3Name = sprintf('%d -2.png', k);
I3Path = fullfile(folder, I3Name);
imwrite(I3, I3Path);
I4 = imrotate(I, 270);
I4Name = sprintf('%d -3.png', k);
I4Path = fullfile(folder, I4Name);
imwrite(I4, I4Path);
end
Running it seems to throw up some errors. Some of the files are repeated or rotated incorrectly. Can anyone spot the reason please? I don't really know enough about matlab to see what I've done wrong!

채택된 답변

Image Analyst
Image Analyst 2020년 1월 15일
Teshan:
I believe the problem comes about because you are, in subsequent runs, processing images from a prior run. You should either skip those images, like I did in my code below, or write those images to a folder different than your input folder.
folder = pwd;
S = dir(fullfile(folder,'*.png'));
fprintf('Found %d PNG files.\n', length(S));
hFig = figure;
hFig.WindowState = 'maximized';
angles = [90, 180, 270];
for k = 1 : 3
originalFilename = fullfile(folder, S(k).name);
[~, baseFileNameNoExt, ~] = fileparts(lower(originalFilename))
originalImage = imread(originalFilename);
fprintf('\nRead in %s.\n', originalFilename);
subplot(2, 2, 1);
imshow(originalImage);
title(['Original image : ', S(k).name], 'FontSize', 15);
% Skip files that are the outputs from prior runs.
% They will contain '-n.png'
if endsWith(baseFileNameNoExt, ' -1') || ...
endsWith(baseFileNameNoExt, ' -2') || ...
endsWith(baseFileNameNoExt, ' -3')
continue; % Skip this image.
end
for angleIndex = 1 : 3
thisAngle = angles(angleIndex);
rotatedImage = imrotate(originalImage, thisAngle);
subplot(2, 2, angleIndex + 1);
imshow(rotatedImage);
% Create new name with -1, -2, or -3 appended to the original base file name.
rotatedName = sprintf('%s -%d.png', baseFileNameNoExt, angleIndex);
title(rotatedName, 'FontSize', 15);
fullFileName = fullfile(folder, rotatedName);
imwrite(rotatedImage, fullFileName);
fprintf(' Wrote out %s.\n', fullFileName);
end
promptMessage = sprintf('Do you want to Continue processing,\nor Quit processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit')
break;
end
end
close(hFig);
  댓글 수: 3
Image Analyst
Image Analyst 2020년 1월 15일
Sorry, for testing I just did the first 3. Change
for k = 1 : 3
to
for k = 1 : length(S)
Teshan Rezel
Teshan Rezel 2020년 1월 15일
thank you very much, that's solved it!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2020년 1월 14일
The code works fine for me. If I had to guess, I would say it is because you ran the code previously while debugging and never cleaned the folder of the results of previous runs.
  댓글 수: 7
Teshan Rezel
Teshan Rezel 2020년 1월 14일
right, I've tried that but it still says it can't be found. Not sure what I'm doing wrong
Matt J
Matt J 2020년 1월 14일
That seems to say that it is not on your path. I.e., it will only be detected and run if your current Matlab folder is the folder where your mfile actually resides.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Holidays / Seasons에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by