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!

 采纳的回答

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 个评论

Thanks for this! The only thing is that it works for images 1, 10 and 100 only. Apologies for this, but is there a way to make it work for all 250 images please?
Sorry, for testing I just did the first 3. Change
for k = 1 : 3
to
for k = 1 : length(S)
thank you very much, that's solved it!

请先登录,再进行评论。

更多回答(1 个)

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 个评论

Hi Matt, the code itself works fine and runs without a problem but the outputs are wrong. I've cleaned the folder, clc + clear the console line, closed matlab and restarted again and ran it. The outputs are still wrong.
The first image is rotated and labelled fine. The 2-250th images are labelled wrong.
Hope that clarifies the issue
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
Hi Matt, it returns "myScript not found"
Is this meant to hold the name of my ".m" file?
If it helps, my array of numbers seems to be really weird:
Columns 1 through 22
104 97 91 95 88 84 94 91 93 92 89 85 82 87 96 105 113 103 116 113 116 113
101 96 93 115 132 126 113 95 114 105 94 84 82 88 95 101 101 102 115 111 115 121
103 93 85 96 110 114 119 109 108 103 94 90 89 88 89 87 98 120 129 124 113 115
101 94 81 75 77 91 103 91 88 92 94 90 85 85 90 96 113 131 119 114 107 108
98 93 86 92 99 100 100 98 94 89 85 86 93 98 103 104 112 110 93 95 101 107
97 96 93 96 101 106 107 96 97 94 93 92 97 101 106 109 105 96 93 96 104 109
95 93 98 106 110 115 113 98 104 99 95 94 95 99 100 101 106 99 101 102 107 110
95 96 101 108 111 110 107 105 101 100 98 98 98 98 98 97 90 97 101 108 108 107
98 93 95 107 113 110 107 98 105 104 102 99 100 98 95 93 97 99 102 105 109 112
95 95 105 108 110 110 101 99 104 102 99 100 102 104 104 107 106 105 105 106 105 106
90 94 105 100 102 106 100 99 101 94 94 97 95 97 104 104 104 107 106 105 106 108
90 92 98 100 96 98 97 90 90 80 78 82 86 91 94 96 99 99 98 98 98 101
92 91 101 101 88 82 78 67 60 52 44 48 58 65 71 76 85 94 97 91 88 92
87 82 90 79 58 51 42 39 36 29 30 30 28 43 64 70 76 80 81 82 84 86
89 84 82 60 41 38 31 34 33 28 35 38 36 58 83 86 73 57 52 66 77 76
84 85 72 48 39 39 34 36 35 32 29 33 50 71 81 78 51 45 41 49 65 77
82 77 71 47 41 35 32 28 33 34 27 28 52 54 41 35 41 39 37 39 50 60
77 74 74 52 37 32 24 20 31 31 29 25 37 44 53 57 50 45 39 37 38 44
72 77 53 38 33 28 25 22 30 30 30 27 39 50 62 69 75 67 56 45 37 34
77 75 49 39 30 29 28 30 28 29 26 24 41 50 60 70 75 70 61 48 39 33
84 75 41 38 32 33 31 35 27 27 27 32 50 58 62 66 74 73 68 59 46 36
85 82 50 57 59 50 40 34 34 31 42 52 64 68 73 71 81 85 77 58 46 49
92 85 72 70 59 57 48 40 40 44 60 69 72 72 81 84 85 81 78 75 67 52
93 93 77 73 68 68 63 53 48 57 74 83 90 82 83 86 86 87 88 87 82 76
96 95 95 85 85 78 79 73 70 73 79 82 83 84 84 84 80 85 84 87 80 86
98 94 100 89 91 91 84 84 79 78 74 73 75 80 79 76 78 77 83 85 83 80
93 88 96 88 88 87 81 79 77 75 73 71 73 75 74 73 78 80 81 72 77 79
91 85 103 93 93 89 87 82 80 74 70 69 70 69 68 71 74 81 83 82 81 73
88 75 112 100 98 96 92 90 101 85 72 70 71 67 67 71 76 83 82 83 78 73
80 69 106 100 95 88 86 79 72 64 63 71 75 73 76 81 88 81 77 78 75 74
77 74 99 103 97 88 77 70 56 55 57 60 58 52 49 52 58 66 65 53 51 52
78 77 98 105 102 98 76 73 55 52 45 40 37 41 43 44 45 55 47 36 40 30
67 60 98 105 95 77 61 60 47 39 34 34 37 44 47 42 37 44 38 29 30 29
65 44 107 84 61 47 48 42 33 36 40 60 80 71 55 50 55 55 51 48 47 36
66 52 76 52 53 53 48 53 60 66 89 112 119 117 109 100 96 97 83 63 55 54
65 66 75 75 62 59 69 82 98 112 126 131 131 133 134 130 131 125 110 86 65 56
78 73 74 78 80 79 87 103 118 127 131 131 135 131 128 130 126 116 111 99 75 56
80 76 84 86 86 89 104 117 122 121 125 128 125 123 120 116 110 103 94 74 52 43
87 84 87 84 85 87 97 108 114 120 124 124 122 118 111 103 84 66 46 33 31 36
85 88 108 102 102 90 94 104 126 131 130 125 113 94 73 56 39 40 35 29 28 33
91 93 114 88 96 99 98 110 134 134 126 103 72 50 35 27 26 36 43 33 32 31
96 96 87 74 69 84 95 110 125 121 92 57 45 36 31 30 25 27 30 30 27 26
96 81 47 62 60 77 87 100 105 81 58 57 50 40 39 49 33 29 23 30 28 29
90 84 30 31 37 50 61 78 72 49 43 44 47 48 61 58 41 41 36 34 28 29
84 85 33 27 27 30 40 48 42 35 30 41 46 54 52 49 41 45 45 35 27 24
89 89 35 31 31 32 39 39 34 31 29 32 48 53 52 45 44 40 41 33 33 28
83 100 55 36 32 33 31 34 30 28 26 30 41 54 56 55 55 48 45 46 46 40
88 92 78 56 36 32 30 31 29 34 31 27 38 54 65 70 63 64 65 67 57 47
72 67 105 70 47 34 25 27 32 38 27 24 33 45 60 64 68 72 74 74 73 65
65 66 110 77 52 28 27 17 22 24 19 20 25 41 60 63 67 70 70 66 64 62
75 75 97 93 56 39 26 23 17 19 19 23 29 44 62 65 73 75 78 78 71 58
81 85 90 77 67 42 30 29 23 24 20 24 33 49 65 73 81 77 74 69 58 45
88 91 82 84 62 47 42 40 34 28 29 35 48 65 77 86 88 83 75 66 58 52
86 106 81 74 68 63 66 60 51 36 38 50 63 83 97 98 92 81 69 65 68 69
71 81 69 64 67 67 77 76 72 48 48 58 73 91 100 97 77 69 66 74 84 87
45 49 63 64 58 68 74 84 80 61 64 78 95 101 91 82 65 69 75 84 89 91
44 45 60 50 50 63 66 66 72 73 81 95 98 81 59 50 60 65 73 81 78 66
47 42 66 61 61 67 73 65 66 83 86 76 56 34 36 42 63 68 63 45 40 45
51 40 70 72 73 81 90 79 77 87 83 56 41 40 37 39 61 62 51 41 38 36
70 54 87 89 85 100 109 101 83 69 52 45 42 38 41 49 64 59 46 41 41 47
86 84 86 86 105 117 104 95 81 77 62 49 46 47 53 62 60 57 53 44 52 68
82 91 86 94 105 109 94 77 66 65 66 63 62 59 53 57 51 61 67 73 81 84
85 91 94 102 111 108 84 61 40 42 48 64 71 62 58 62 61 78 85 91 92 86
90 88 101 105 112 104 71 49 39 34 34 46 63 72 75 83 78 85 97 98 92 92
84 84 93 91 105 94 66 45 36 29 43 50 58 55 60 56 76 91 95 92 90 95
84 78 90 94 99 95 63 43 35 32 51 75 79 59 48 40 68 94 103 96 88 95
Is this meant to hold the name of my ".m" file?
Yes.
right, I've tried that but it still says it can't be found. Not sure what I'm doing wrong
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 的更多信息

产品

版本

R2019b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by