How to read a resized image?

1 次查看(过去 30 天)
pooja
pooja 2015-1-31
Example -
B = imread('D.jpg');
A = imresize(B,0.5)
C = imread('A') - gives an error

回答(2 个)

David Young
David Young 2015-1-31
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 2015-1-31
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

类别

Help CenterFile Exchange 中查找有关 MATLAB Report Generator 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by