matlab, multiple axes or scales for image pixels a

2019-08-13 10:51发布

问题:

In matlab I have created an image of 64x64 pixels, with varying values for each pixel.

But I would also like to display the real scale of the image on the plot. The real size is 1 meter and I would like to have two x-axis scales and two y-axis scales to show both pixel positions and real distance.

How would i do this?

回答1:

Here is an example using an approach from my answer to a previous question on SO. That's the output for some random image with 64 pixels on each dimension:

The following code creates a square figure that contains three axes. The width and height are manually set to be equal. This is needed for the axes to be aligned properly. After this, the actual image is drawn in a new axes a using imshow. Note that we need to provide the 'Parent'-property to draw on a specific axes. Then we add the real world axes on the bottom b and on the left side c. Their 'position'-properties are set to be of almost zero height or width respectively. By looking at the used values for the positioning, we see that axes a is set to be on top/right side of the two other axes.

Here is the complete code:

% some image data
img = rand(64);

% define variables
imgsize = 500;  % size on screen
xreal = 1;      % meter
yreal = 1;      % meter

% create square figure
figure('Position',[0,0,imgsize,imgsize]);

% pixel axes and actual plot
a=axes('Position',[.2 .2 .7 .7]);
set(a,'Units','normalized');
iptsetpref('ImshowAxesVisible','on');
imshow(img,'Parent',a);

% real world axis (below)
b=axes('Position',[.2 .1 .7 1e-12]);
set(b,'Units','normalized');
set(b,'Color','none');
set(b,'xlim',[0 xreal]);

% real world axis (left)
c=axes('Position',[.1 .2 1e-12 .7 ]);
set(c,'Units','normalized');
set(c,'Color','none');
set(c,'ylim',[0 yreal],'YDir','reverse');

% set labels
xlabel(a,'Pixels')
xlabel(b,'Real distance (m)')
ylabel(a,'Pixels');
ylabel(c,'Real distance (m)');
title(a,'A nice title to this noisy image');