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

 채택된 답변

Image Analyst
Image Analyst 2014년 7월 7일

0 개 추천

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

Anqi Li
Anqi Li 2014년 7월 7일
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?
Image Analyst
Image Analyst 2014년 7월 7일
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.
Anqi Li
Anqi Li 2014년 7월 7일
But when I comment out the questdlg() line I only have the first image rotated and displayed.
Anqi Li
Anqi Li 2014년 7월 7일
편집: Anqi Li 2014년 7월 7일
And how can I imwrite each rotated image with the original file name with and 'R' at the beginning?
Image Analyst
Image Analyst 2014년 7월 7일
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');
Image Analyst
Image Analyst 2014년 7월 9일
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

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Images에 대해 자세히 알아보기

질문:

2014년 7월 7일

댓글:

DGM
2023년 6월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by