How do I crop a 2-d surface plot in MATLAB, to remove axes and white margins? - matlab

I have written a program to process and print a 81x81 2D surface plot, which needs to be used as an input. Saving the plot made by Matlab includes side axes as well as white margins on the sides.
How do I crop this image to get just the (81pixels)x(81pixels) output as an image?

Try placing this after your figure code, it will remove the margins around your figure.
set(gca,'units','pixels') % set the axes units to pixels
xx = get(gca,'position'); % get the position of the axes;
set(gcf,'units','pixels') % set the figure units to pixels
yy = get(gcf,'position'); % get the figure position;
set(gcf,'position',[yy(1) yy(2) xx(3) xx(4)]) % set the position of the figure to the length and width of the axes
set(gca,'units','normalized','position',[0 0 1 1]) % set the axes units to pixels

You can avoid using surf plot and just save your array as image. So, you have 81x81 array. I'll use this one for the example:
a = magic(81);
Now you need to normalize image in order to have values from 0 to 255:
a = (a - min(min(a)))/(max(max(a))-min(min(a)))*255;
Finally you use imwrite to save your array as image.
imwrite(a,jet(256),'SO_4.png')
The first argument is your 81x81 array. The second argument is colormap. You can specify any colormap you want, but I like the heatmap jet(256). You can skip this argument, in this case the image would be grayscale. From here you can find what colormaps are available and what they look like. The last argument is image name. The result is shown below:
Hope that helps, good luck.

Related

Multiple Plot Matlab including image

I use the tiledlayout(2,2) command to create a plot in Matlab. The first three title fields filled with normal plots. The fourth and last field is a field with an image. I used the commands IMG = 'mypic.tif'and imshow(IMG).I wonder why I can not change the size of my image. Is this not possible with the tiledlayoutcommand? I have looked up the documentary for tiledlayout and imshow(), but found nothing which helps me.
It indeed seems as if this is not possible using the tiledlayout functionality. However, it is possible using the subplot functionality. Below, I will give an example how this works.
Consider the following script:
% Generate some dummy data for the four plots
x = linspace(0, 2*pi, 25);
y = sin(x);
% Load sample image
image = imread('ngc6543a.jpg');
% Generate the figure
fig = figure(1);
% The three subplots
for i = 1:3
subplot(2,2,i);
plot(x, y);
end
% The image
subplot(2,2,4);
imshow(image);
% Increase size of the image
size_factor = 1.4; % 1.0 is original size
im_ax = fig.Children(1); % axes belonging to image
dims = im_ax.Position(3:4); % current dimensions
dims_new = size_factor * dims; % scale old dimensions
dxdy = (dims_new - dims) / 2; % offset for left bottom corner of image
im_ax.Position = [im_ax.Position(1:2) - dxdy, dims_new]; % update position
In this script, we start by generating some dummy data for the three 'normal' plots and loading a sample image (this image came with my MATLAB installation). Subsequently, we create a figure. After the figure has been created, I add the three dummy plots to the figure using a for loop and the subplot functionality. Then, we plot the image using imshow in the fourth subplot.
Now, the interesting part begins. First of all, I define a scale factor for the image (size_factor). Then, I retrieve the axes in which the image is plotted and store it in im_ax. From this axes, I retrieve the last two elements of the Position field and store them in dims. These two elements define the size of the axes. Based on the value of size_factor, I calculate the new size of the axes and store this in dims_new. To ensure that the axes (and thus the image) remains centered, we need to calculate by how much we need to shift the left bottom corner of the axes (whose coordinates are stored in the first two elements of the Position field). This result is stored in dxdy. The last thing we do is simply updating the Position field of the axes in which the image is plotted.
Now, to show you some results:
size_factor equals 1.0:
size_factor equals 1.4:
size_factor equals 0.55:

How do I multiply the xticks and yticks with constant and have it displayed in the figure in Matlab 2019?

I've got a picture (a matrix of intensities for every pixel). When plotting this with matlab's imagesc the x-,y-axis are obviously in pixels. I know that 1 pixel corresponds to 0.6250 millimeter in both x- and y-direction.
How can I change only the scale in axis in the figure to show millimeters instead of pixels, i.e how do I multiply the xticks and yticks with 0.6250 and have it displayed in the figure?
I have tried:
new_x = xticks*pixel_spacing(2);
set(gca,'xticklabels', new_x)
but this results in the error
"Unable to use a value of type 'matlab.graphics.axis.Axes' as an index."
imagesc has a syntax where you can specify the scaling of the pixels (see the documentation).
x = [1,size(img,2)] * pixel_spacing;
y = [1,size(img,1)] * pixel_spacing;
imagesc(x,y,img);
(assuming img is your image to display)
The advantage over changing the tick labels is that other things plotted on top of the image will use the same coordinates, as will retrieving mouse cursor location and so forth. This might or might not be relevant, but it’s good to know.
I figured it out.
xticklabels(xticks*pixel_spacing(2))
yticklabels(yticks*pixel_spacing(1))

Matlab: (1) Plotting multiple canvases, (2) holding them separately, (3) montage all

I've got 10 grayscale images. I'd like to plot a simple YELLOW line over each image separately, then show them all over one plot (montage style).
I tried to draw all images first, but that made plotting lines very tricky (X,Y axes weren't standard for plotting over each separate image).
I thought about burning the line over the image, but I don't have the computer vision toolkit (easy functions to do this), otherwise it seemed complicated to both convert the grayscale to color and get it to burn the image.
I thought I might be able to use the function newplot to create a temporary plot space for each image, draw the line with a simple plot(...) call, then save it and just montage(...) all the individual plots at the end.
Is this possible? I've never played with the function newplot or tried to loop through individual plots, saving them up for a call to montage(...) this way, but it seems like a logical/simple approach.
I finally worked it out with subplot, subimage, and plot, using subplot with the position arguments does what I want easily enough. Using subplot kept the axis relative to the subplot I was on so I could plot the line with a standard fplot/plot call. The trick was normalizing the position to percentages vs. thinking of it in terms of pixels.
here's some code demoing it:
% Loop through this code, each time moving the subplot by position
LOOP {
% calculate left & bottom position as percentages (0..1)
subplot( 'Position', [ left bottom (1/cols) (1/rows) ] );
hold on
% (1) Draw the image
subimage(tmpImg, [0 255]);
axis off;
% (2) Plot the line over the original image
F = #(x) polyval(p, x);
fplot(F, [1 dimX 1 dimY], '-y');
}

How to add an image thumbnail as(or beside) a plot marker in MATLAB?

I am running Isomap Dimensionality reduction in MATLAB on a series of images. I want to plot the image's thumbnail beside the point on the manifold corresponding to it.
I am currently using 2 differnt isomaps http://isomap.stanford.edu/ and http://robotics.cs.brown.edu/projects/stisomap/ .
The imagesc function can take arguments which dictate where the image is drawn, so I would use this. Here's an example of imagesc being drawn on top of a plot:
% Draw plot
vals=rand(2,100);
plot(vals(1,:),vals(2,:),'x');
hold on;
% Draw image
im=imread('moon.tif');
xs=linspace(0.1, 0.2, size(im, 2) );
ys=linspace(0.1, 0.2, size(im, 1) );
colormap gray;
imagesc(xs,ys,im)
Which looks like this:
Note the first two arguments to imagesc which define the range over which the image is drawn. Obviously you'll want to change the arguments to linspace, which will define the position and size of the image, and you'll need to take account for the aspect ratio if the image isn't square, but hopefully this will get you along the right lines.

Overlaying data on an image in matlab

I have two plots I want to overlay on top of each other as shown in the link below:
The inputs are the two images on the left and the output is the image on the right. Here's the code I used:
reference = imread('ref_foam.png');
figure, imshow(reference);
hold on;
h = imshow(data,[]);
hold off
colormap jet;
alphamap = zeros(size(reference,1),size(reference,2));
for i = 0:size(data,1)-1
for j = 0:size(data,2)-1
if(~(data(i+1,j+1) == 0))
alphamap(i+1,j+1) = 0.75;
end
end
end
set(h, 'AlphaData', alphamap);
Anytime there's a zero in the data array, it sets that transparency to zero or else it will set the transparency to 0.75.
Now, my questions are: How can I get the colormap to apply to only the data array? In this example it works but if I convert "reference" to a grayscale, the colormap applies to that as well. The input for the colormap is an axes handle, is there anyway to input the image's handle (h) so that it only applies to the top (data) array? Also, I'd like to implement a colorbar as well. Is there anyway to apply the colorbar only to the data array? Thanks.
You probably want to use the subimage command - it lets you create images on the same figure with different colormaps.
Once create and modified as in your script, overlay the images by setting their axes positions on top of each other.