How can I rotate multi-image files?

I'm working on a project about analysing image data. I have a folder where contains 8 thousands images. Each image file has to be rotated in each specific angle. The angle data are in an excel file. I am a MATLAB beginner, please help me. Here is my code but I don't understand where is wrong. The error message shows 'Subscripted assignment dimension mismatch. Error in ImageRotate newImage(i) = imrotate(I,wd(i));'. I tested it in a director where there is only one gif image and set 'wd = num(2,8);), But I still got the same error message.
%load all images from a directory. AllImageRead.m
myFolder = '/Users/anqili/Documents/MATLAB/gifImage/';
filePattern = fullfile(myFolder,'*.gif');
scrFiles = dir(filePattern);
for k = 1:length(scrFiles)
baseFileName = srcFiles(k).name;
fullFileName = strcat(myFolder,baseFileName);
I = imread(fullFileName);
end
%load rotation angles from xls. WDirection.m
[num,txt] = xlsread('sta_201209_ver18.xls');
wd = num(:8);
%Rotate Images. ImageRotate.m
for i = 1:length(wd)
newImage(i) = imrotate(I,wd(i));
figure, imshow(newImage);
end

 Accepted Answer

You're just repeatedly overwriting the variable I so when the loop exits, it holds only the very last image. Rearrange it like this:
%load rotation angles from xls. WDirection.m
[num,txt] = xlsread('sta_201209_ver18.xls');
wd = num(:8);
%load all images from a directory. AllImageRead.m
myFolder = '/Users/anqili/Documents/MATLAB/gifImage/';
filePattern = fullfile(myFolder,'*.gif');
scrFiles = dir(filePattern);
for k = 1:length(scrFiles)
baseFileName = srcFiles(k).name;
fullFileName = fullfile(myFolder,baseFileName);
originalImage = imread(fullFileName);
subplot(1,2,1);
imshow(originalImage);
rotatedImage = imrotate(originalImage, wd(k));
subplot(1,2,2);
imshow(rotatedImage);
% imwrite(.................. % To save it....
message = sprintf('Do you want to continue');
button = questdlg(message, 'Continue?', 'Yes', 'No', 'Yes');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'No')
break;
end
end

8 Comments

Thank you very much. Problem solved!
However, I have another question about the result. The new window 'Figure 1' contains my original image and rotated image shows only grey images(not the colour one), and the rotated image became smaller. I want to rotate all the images(8000 images) for one time instead of clicking the button many times one by one. Do you know how I can arrange the code?
Comment out the questdlg() line to have it not ask you. The rotated image can either be bigger with black triangles added, or it can be the same size with the corners that rotate out of the original field of view clipped off. The image is not smaller. If you rotate it, it has to shrink it down for display but the underlying image is actually bigger, not smaller.
But when I comment out the questdlg() line I only have the first image rotated and displayed.
Anqi Li
Anqi Li on 7 Jul 2014
Edited: Anqi Li on 7 Jul 2014
And how can I imwrite each rotated image with the original file name with and 'R' at the beginning?
Comment out the "if" block also because you must be hitting the break line. You can do a clear all because you must have button still in the workspace from a prior run of the script.
I put this imwrite line after I rotated the images. But I want each of them with an 'r' in front of of the original file name, instead of overwriting them. How can I replace the 'baseFileName' ?
imwrite(rotatedImage, baseFileName, 'gif');
Use sprintf() to build up any kind of filename you want.
I should point out that GIF files are indexed color images, but nothing is being read other than the index array. The colormap(s) must also be retrieved in the call to imread() (or a call to imfinfo()). If the image is still indexed color at the time it's saved, the map must also be passed to imwrite().
If it's desired to do any interpolation in the rotation, the image must be converted to RGB.
[inpict map] = imread('canoe.tif'); % read the map
% default is nearest-neighbor interpolation, which works
% but the matting color will be whatever the first map color is
outpict = imrotate(inpict,15);
imshow(outpict,map,'border','tight') % use the map
% smooth interpolation doesn't work with indexed inputs
outpict = imrotate(inpict,15,'bilinear');
imshow(outpict,map,'border','tight') % use the map
% convert to RGB
inpict = ind2rgb(inpict,map);
outpict = imrotate(inpict,15,'bilinear');
imshow(outpict,'border','tight') % don't need the map anymore

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 7 Jul 2014

Commented:

DGM
on 29 Jun 2023

Community Treasure Hunt

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

Start Hunting!