How to read a resized image?

1 view (last 30 days)
pooja
pooja on 31 Jan 2015
Answered: Image Analyst on 31 Jan 2015
Example -
B = imread('D.jpg');
A = imresize(B,0.5)
C = imread('A') - gives an error

Answers (2)

David Young
David Young on 31 Jan 2015
You do not need to read A. It is already in memory. You can just do
C = A;
if you want to assign it to a new variable.

Image Analyst
Image Analyst on 31 Jan 2015
See what imprecise variable names causes? The badly named A is an image, not a filename string. imread() takes filename strings, not image arrays, so you can't pass it "A". If you had a descriptive name for your variable, you probably would have realized that.
Like David said, you already have the resized image in memory in variable "A" and if you want a copy of it in "C" you can just say C=A;. If you want to save A to disk, you can use imwrite, and then you would use the filename string in imread() to recall it from disk. But it's not necessary in this small script.
Better, more robust code would look like
originalImage = imread('D.jpg');
resizedImage = imresize(originalImage, 0.5); % Resize originalImage by half in each dimension.
% Save it to disk
baseFileName = 'Resized D.png';
folder = pwd; % Wherever you want to store it.
fullFileName = fullfile(folder, baseFileName);
imwrite(resizedImage, fullFileName);
% No need to create C at all.
% Now recall the resized image (say, in a different function or script)
baseFileName = 'Resized D.png';
folder = pwd; % Wherever you want to store it.
fullFileName = fullfile(folder, baseFileName);
% Check if it exists and read it in if it does, warn if it does not.
if exist(fullFileName , 'file')
smallImage = imread(fullFileName);
else
warningMessage = sprintf('Warning: file not found:\n%s', fullFileName);
uiwait(warndlg(warningMessage));
end

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!