Overlaying data on an image in matlab - 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.

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:

Filling markers when plotting in a loop

I am trying to plot 31 different vectors in a single figure in MATLAB. I am using a circular marker 'o', for plotting every vector and I want to have every vector painted in a different color and also I want to fill the markers as well with the same color as the markers edges.
I am using the following code:
while (n<=31)
plot(x(n),y(n),'o',rand(1,3)) % Not filled markers
n=n+1;
end
The problem is that since I am using a random for the color selection, when I try to run the following code:
while (n<=31)
plot(x(n),y(n),'o','MarkerFaceColor',rand(1,3)) % Filled markers
n=n+1;
end
The marker edge and the marker filling have different colors. I don't know how to fix this, maybe I shouldn't use the random for selecting the color but I don't know how to fix it, to obtain the same color in the marker edge and in the filling.
I would recommend using scatter for this rather than creating n different plot objects that you then have to manage. Using the CData property it is possible to set a separate color for each of your data points.
colors = rand(31, 3);
x = rand(31,1);
y = rand(31,1);
s = scatter(x, y, 'filled', 'CData', colors);
'MarkerEdgeColor' is the property you want, in addition to FaceColor
while (n<=31)
c = rand(1,3);
plot(x(n),y(n),'o','MarkerFaceColor', c, 'MarkerEdgeColor', c);
n=n+1;
end

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

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.

How to define transparent element in colormap

I would like to define a transparent color within the color map how do I do that?
The reason I need this is that I have a multiple layers in my axes (produced both by imagesc and plot). I know I could simply first use imagesc and then plot but I want to draw the lines behind non-zero values of the imagesc representation.
To color the zeros white I use
jet = colormap('jet');
jet(1:2,:) = 1;
colormap(jet);
Now I would like to make white transparent.
The colormap doesn't have a fourth element for alpha, it's RGB only, so the way I do this sort of thing is to make a mask of the desired transparency layer - binary or greyscale will work - and then apply that to the image. To do this you need to store the handle of the image
% make random image
im = rand(100,100);
% make example alphamask
alphamask = im<0.3;
% store handle
hnd = imagesc(im);
% apply mask
set(hnd, 'AlphaData', alphamask);

Save figure with plots as a matrix for further processing in MATLAB

I have displayed an image and plotted a rectangle over all the useless areas. Now i need to save this image in the form of a matrix, so that i can use it further.
figure,imshow(colz);
hold on;
for i=1:num
if(i~=n)
img=rectangle('Position',box(i,:),'FaceColor','k');
end
end
This is a part of my whole code. I'm covering all the undesired parts with zeros. so now I'm left with my desired area and a background. How can i save the pixel values in another matrix so as to create a new image that contains this enhanced data?
Thanks
here try this
F = getframe(gcf);
% gcf is your figure currently displaying your image with the rectangle
%
y = F.cdata;
hold off
figure(2),imshow(y);