adjust axes as the loaded image size.
1 次查看(过去 30 天)
显示 更早的评论
I want to load different size of images on axes. I want to know that When image is loaded, how the size of axes is adjusted as the size of loaded image.
0 个评论
采纳的回答
Image Analyst
2012-9-7
It does its best to adjust the size of the displayed image given the initial size of the axes and the size and aspect ratio of the image being displayed. I don't know the exact algorithm. If you want fixed axes sizes, and fixed magnification then you can set up those with calls to set() and imshow() respectively.
2 个评论
Image Analyst
2012-9-8
编辑:Image Analyst
2012-9-8
See my demo:
% Demo to display an image with random size and location.
% By ImageAnalyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', 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);
% Display the original gray scale image.
imshow(grayImage, []);
title('Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo To display image in random location and random sizes','numbertitle','off')
axis on;
keepGoing = true; % Keep going until user cancels out.
maxRuns = 30; % Fail safe to prevent going on forever.
thisRun = 1;
while keepGoing && thisRun < maxRuns
% Make it a random size:
randomWidth = max(0.1, rand(1));
randomHeight = max(0.1, rand(1));
% Make it show at a rnadom location.
% Make it fit on screen.
randomX = (1 - randomWidth) * rand(1);
randomY = (1 - randomHeight) * rand(1);
myRect = [randomX, randomY, randomWidth, randomHeight];
% Do the sizong and positioning.
set(gca, 'units','normalized', 'outerposition', myRect);
% Notify user and allow opportunity to exit.
locationMessage = sprintf('X = %.4f\nY = %.4f\nWidth = %.4f\nHeight = %.4f\n', myRect);
promptMessage = sprintf('%s\n\nDo you want to display another random size at another random location?', locationMessage);
button = questdlg(promptMessage, 'Continue?', 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
keepGoing = false;
break;
end
thisRun = thisRun + 1; % Increment failsafe.
end
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!