Code covered by the BSD License  

Highlights from
Adjust the display resolution of an image

image thumbnail

Adjust the display resolution of an image

by

Eric Johnson

 

24 Aug 2008 (Updated )

Same as TRUESIZE, but works with multi-image figures.

truefit(h, pxArea)
function truefit(h, pxArea)
%TRUEFIT resizes an image axes to display at a particular resolution. Extends
%the functionality of TRUESIZE, which only works with single-image figures.
%
%   TRUEFIT(H, [MROW NCOL]) adjusts the display size of an image. H is the
%   handle of an image object or an axes that containins one.[MROW NCOL]
%   specifies the requested screen area (in pixels) that the image should be
%   displayed at.
%
%   TRUEFIT(H) uses the image's height and width for [MROW NCOL]. This results
%   in the display having one screen pixel for each image pixel.
%
%   The center of the axes' position rectangle will remain constant relative to
%   the object that contains it (i.e., its parent figure or UICONTROL). The axes
%   is "pinned" at its center.
%
%   Example
%   --------
%       % Load the 'cameraman' image
%       img = imread('cameraman.tif');
%
%       % Plot image, initially as large as possible.
%       h1 = imshow(img,'InitialMagnification','fit');
%
%       % Now resize to match the screen resolution
%       truefit(h1);

%   Eric C. Johnson <eric.johnson@mathworks.com>    10-Jul-2008
%   Copyright 1984-2008 The MathWorks, Inc.
%   This function is not supported by The MathWorks, Inc.
%   It is provided 'as is' without any guarantee of
%   accuracy or functionality.

% Is H an axes or image handle?
switch lower( get(h,'type') )            
    case 'axes'
        
        % Find image object handle from axes
        hAxes   = h;
        hImg    = findobj(hAxes,'type','image');
        
        % Verify that axes contains a single image plot object
        if isempty(hImg)
            error('MATLAB:truefit','No image objects found in axes.');
        elseif length(hImg) > 1
            error('MATLAB:truefit','Axes should contain only a image object.');
        end
        
    case 'image'
        
        hImg = h;
        hAxes = get(h,'Parent');
        
end

% Determine new display size
if nargin < 2
    % Use 1:1 image pixel to screen pixel ratio. Need to get the dimensions of
    % the image object. 
    cdata = get(hImg,'cdata');    
    [mrow,ncol] = size(cdata);
else
    % User provided display size
    mrow = pxArea(1);
    ncol = pxArea(2);
end

% Now set axes size to match [MROW NCOL]
% Temporarily change axes units to pixels
oldunits = get(hAxes,'Units');    
set(hAxes,'Units','pixels');

% Collect axes positioning information
oldpos = get(hAxes,'Position');

% Width and height are known from CDATA, however the lower left corner of
% the axes must be recomputed.
newleft = (oldpos(1) + oldpos(3)/2) - ncol/2;
newbot  = (oldpos(2) + oldpos(4)/2) - mrow/2;

newpos = [newleft newbot ncol mrow];

% Apply new position and restore original units
set(hAxes,'Position',newpos);
set(hAxes,'Units',oldunits);

Contact us