-
Notifications
You must be signed in to change notification settings - Fork 2
/
addImageToAxes.m
39 lines (29 loc) · 1.18 KB
/
addImageToAxes.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function [ imageObject ] = addImageToAxes( imageFilename, axesHandle, axesWidth )
%ADDIMAGETOAXES Adds an image to an axes
% Opens image filename and adds it to the axes
% Returns a handle to the image object.
% If axesWidth = 0 then use the image default pixel size.
% Open the file to get the imageData
[imgData,map,alphaChannel] = imread(imageFilename);
% Create an image object and make the parent the axes
imageObject = image(imgData, 'Parent', axesHandle, 'AlphaData', alphaChannel);
% Make unit of axes 'pixels'
% Visible off
set(axesHandle, 'Visible', 'on', 'Units', 'pixels');
% Get the current 'Position' of the Axes so that we can use the x and y.
currentPosition = get(axesHandle, 'Position');
% Get the number of rows and columns of the image
[rows_height,cols_width,depth] = size(imgData);
axesHeight = axesWidth*rows_height/cols_width;
% If axesWidth = 0
% axesWidth = num cols
% axesHeight = num rows
% else
% Use the axesWidth and aspect ratio to calc height
if axesWidth == 0
axesWidth = cols_width;
axesHeight = rows_height;
end
% set the new 'Position' on the axes
set(axesHandle, 'Position', [currentPosition(1), currentPosition(2), axesWidth, axesHeight]);
end