Get matrix from plot and subplot - matlab

I already had some figure that I saved before, Like this:
Now after some days, I need the original size of these images for copy these in my paper. I want to extract the main matrix of these three image for save these again with imwrite function.
I searched this problem on the internet but the people says I have to use getframe and frame2im functions. But how? I want the original matrix. Can anyone tell me how to extract the main matrix from the figured image in matlab??

Try using the following code:
imgs = findobj(gcf,'Type','image');
images = cell(1,numel(imgs));
for i = 1:numel(imgs)
images = get(imgs(i),'CData');
end
The image matrices should now be stored in the separate cells of images.

Related

How to use isosurface() for images?

I am new to matlab. I have few binary images, as shown below, which I have to display with isosurfaces as a 3d object. I could not understand what input I need to pass to this function, when I have some images. I am referring this documentation.
I have total 22 images which are slightly diffrent from this image:
If your 22 images are all images from the same volume, you can concatenate them into a 3D array with the dimensions of [nRows, nCols, 22]. How you do this really depends upon the format your data is currently in.
You can then pass this 3D matrix (images) directly to isosurface along with an isovalue (I'll assume 1 for demonstration purposes);
FV = isosurface(images, 1);

Montage in Matlab - save and show

When I run this code:
fileFolder = fullfile(matlabroot,'toolbox','images','imdemos');
dirOutput = dir(fullfile(fileFolder,'AT3_1m4_*.tif'));
fileNames = {dirOutput.name}'
zImg=montage(fileNames, 'Size', [2 5]);
imwrite(zImg,'C:\Users\xc\Desktop\ATMtemp.png')
I get the montage image in a new figure, but can I cancel it and just store it in memory?
Furthermore, I cannot save the montage. Any reason why and how can I do it without using getframe as I do not want to show the figure generated?
The montage function in MATLAB's image processing toolbox is for display purposes only and so it only shows a figure. The only way that you'd be able to get the image data from this figure is if you assign a handle to a function as output (which is zImg in your case), then use the getframe/cdata idiom you have suggested. However, this will give you a white border as you have also noticed.
If you want to create an image that is doing the same thing as montage, you can construct what montage is doing yourself. An alternative to montage would be to read in all of the images in a cell array, then arrange them in a montage manually. I'm going to assume that you are stacking the images in row-major format, so the rows are being populated one row at a time. That means images 1 to 5 will be the first row while images 6 to 10 will be the second row.
The trick to get it into a 2D matrix is that you need to use reshape. reshape will populate elements in column-major format, so you need to construct the transpose of your result, then transpose that when you're done. After, use cell2mat to eliminate the cell arrays and make a final 2D matrix.
As such, do something like this:
%// Your code to get all of the image file names
fileFolder = fullfile(matlabroot,'toolbox','images','imdemos');
dirOutput = dir(fullfile(fileFolder,'AT3_1m4_*.tif'));
fileNames = {dirOutput.name};
%// Create a 1D cell array that will store all of the images
images = cell(1, numel(fileNames));
%// Read in the images yourself and populate the cell array
for idx = 1 : numel(fileNames);
images{idx} = imread(fileNames{idx});
end
%// Reshape the cell array so that it's a 2 x 5 matrix, then
%// convert the 2D cell array into a final 2D matrix.
zImg = cell2mat(reshape(images, [5, 2]).');
%// Write to file
imwrite(zImg,'C:\Users\xc\Desktop\ATMtemp.png')
I'm not 100% sure I understand what you're asking, but if you want to plot a bunch of figures and save them to file without having the figure windows flashing by, you could use figure('Visible', 'Off').

Convert just the content of a figure to an image in MATLAB

I am currently using getframe() and frame2im in MATLAB to convert a figure of a plot to an image.
I just realized that this is working almost like a screenshot of the figure, with all the axes and labels taken into account as well.
How can I convert JUST the contents of the figure (aka the "plot") into an image?
I don't really want to save all of them to file first.
You can use the getframe / cdata idiom. What this will do is that if you call getframe on the current frame in focus without any parameters, it will return a structure to you that contains a structure element called cdata. This stores the RGB pixel array of just the figure contents themselves. The axes and labels are not captured - only what is painted onto the figure is captured.
Here's an example to get you started:
im = imread('cameraman.tif');
imshow(im);
h = getframe;
out = h.cdata;
figure;
imshow(out); %// Should give you the contents within the imshow frame.
FWIW, I also answered this same question here, though it was for a different situation: keep new image when drawing lines by dragging the mouse in matlab
As far as i know, cdata DOES NOT WORK. I had a major problem recently with the same thing - the only work around i could find is to crop each image after using getframe and cdata - this will work fine for images that are all the same size (ugly as it is - you just need to find the grey edges in the image), but if the images are all different, this wont work (well, it wont work well. there might be some way to automatically adjust the crop size)

Matlab: How can I get an image handle for a matrix to be used in createMask?

I'm working with netCDF matrices representing satellite imagery. I'm trying to make a binary mask so I can analyze ROIs within the matrix. I've made an imellipse to use as the mask.
Now, the function createMask requires an image handle for the underlying data. How can I get an image handle for my data matrix? I've displayed in as a contourf plot.
The createMask() function works without an image handle as well. To get the binary mask just do like this:
imshow(Img,[]);
e = imellipse();
mask = createMask(e);
If you want to do it using an image handle, you can create an image handle by doing:
h_img = imshow(Img,[]);
You can then use this handle in the createMask() function. (But it is not really neccesary)

Matlab Imread resizes tif file

so I'm using the imread function in matlab and when I save the TIFF file and open it in photoshop, it has a white border and I can't understand why. I want to maintain its resolution as a 512 by 512 image. Any ideas why? And how I can fix that?
Here's a sample code:
B = imread('W_noise1.tif');
for n = 1:5,
B = medfilt2(B);
end
B = filter2(fspecial('average',3),B)/255;
imshow(B)
Are you sure it's an issue with imread? I'd be surprised if it is.
See this link about medfilt2 where it explains that "medfilt2 pads the image with 0s on the edges, so the median values for the points within [m n]/2 of the edges might appear distorted."
EDIT: I tried to replicate your problem. This is an issue with print where it puts a white frame around the image after you save it. This functionality, print is made for printing plots. If you want to save the image, you should use imwrite.