rotating and saving images to folder: error during loop
古いコメントを表示
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!
採用された回答
その他の回答 (1 件)
Matt J
2020 年 1 月 14 日
0 投票
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
2020 年 1 月 14 日
As I said, the problem is not reproducible with the code you've posted. It has to be something about your environment. Perhaps you have a buggy version of your script higher up on the Matlab path? Try,
>>which -all myScript
Teshan Rezel
2020 年 1 月 14 日
Teshan Rezel
2020 年 1 月 14 日
Matt J
2020 年 1 月 14 日
Is this meant to hold the name of my ".m" file?
Yes.
Teshan Rezel
2020 年 1 月 14 日
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.
カテゴリ
ヘルプ センター および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!