"Subscripted assignment dimension mismatch" error

1 view (last 30 days)
I encounter an error while executing this code. im=imread(I); im=imresize(im,[256 256]; [m,n]=size(im); I=im; [m,n]=size(I); dummy=zeros(m+4,n+4); dummy=double(dummy); dummy(3:m+2,3:n+2)=double(I);% error occurs here [m,n]=size(dummy);

Answers (1)

Image Analyst
Image Analyst on 11 Jul 2015
Most likely the badly-named I is a color image. Read this: http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/ DON'T USE SIZE LIKE THAT.
Try this more robust code:
folder = pwd; % or wherever
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', 15, 'Interpreter', 'None');

Community Treasure Hunt

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

Start Hunting!