How can I rotate multi-image files?

4 views (last 30 days)
Anqi Li
Anqi Li on 7 Jul 2014
Commented: DGM on 29 Jun 2023
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

Image Analyst
Image Analyst on 7 Jul 2014
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
Image Analyst
Image Analyst on 9 Jul 2014
Use sprintf() to build up any kind of filename you want.
DGM
DGM on 29 Jun 2023
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 Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!