Clear Filters
Clear Filters

adjust axes as the loaded image size.

2 views (last 30 days)
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.

Accepted Answer

Image Analyst
Image Analyst on 7 Sep 2012
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 Comments
Nan
Nan on 8 Sep 2012
Thanks for your answer. To use set() function, I think I initially have to get the dimension of the input image. Although I can get the size of image by using s.bytes, I don't know how can I get the dimension of image and how to set these dimension on the axes sizes. Please give some advices.
Image Analyst
Image Analyst on 8 Sep 2012
Edited: Image Analyst on 8 Sep 2012
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

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!