This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Printing a MATLAB plot in exact dimensions on paper
How do I save a plotted image and maintain the original image size in MATLAB?
I have recently been trying to create a custom-sized graph in MATLAB and save it automatically using the saveas function. In particular, I am having issues saving the files in the size that I create them. Roughly speaking, my code is as follows:
mygraph = figure('Position',[1,20,1280,1024]);
% creates a figure positioned 1 px from the left of the screen
% 20 px from the bottom of the screen
% that is 1280 px in length and 1024 px in height
% some code to create graph
saveas(mygraph,'mygraphfilename','emf')
% saves figure as mygraphfilename.emf.
So far, my code works fine in that it can create a custom sized graph on my screen, but it seems to save the pictures themselves in a default size. The weird thing is that if I do not use the saveas function and save the figure manually, then the image retains its size.
For clarification purposes, I'm currently saving the graphs as emf, though I'm also open to using jpg/png/bmp if works fine too.
Try setting the following:
set(mygraph, 'PaperPositionMode','auto') %# WYSIWYG
print -dmeta -r0 file.emf
Related
I am trying to write a large image to PDF (141"x24" at 300 DPI, so 42300 pixels x 7200 pixels). I can write the image as a png without a problem using imwrite(). However, imwrite() does not provide PDF as an output option. So, the alternatives I have seen online all do something like this:
pdffig = figure;
set(pdffig,'Units','Inches','Position',[0 0 141 24],'PaperSize',[141 24]);
pdfaxs = axes;
imshow(Im,'Parent',pdfaxs);
print(pdffig,'largeimage.pdf','-dpdf');
This code is creating a figure, setting some properties to make the figure 141x24 inches in size, showing the image on the figure axis, and then printing the figure contents to pdf.
Unfortunately, what happens is that the figure is resized to fit my screen when imshow() is called and then the printed pdf will have a small version of the image in the center of a large white pdf. What I actually want is for the image to take up the entire 141"x24" pdf. I have tried rearranging when the window size is set and when imshow() is called, but that doesn't help. Note that this works if the desired PDF size fits on my screen (e.g. if I was printing a 12"x12" PDF).
Any suggestions are appreciated!
I am using a therm-app camera to take infra-red photos of bats. I would like to draw around parts of the bat and find the hottest, coldest and average temperature and do further analysis.
The software that comes with the camera doesn't let me draw polygons so I would like to load the image in another program such as MATLAB or maybe imageJ (also happy to use Python or other if that would work).
The camera creates 4 files total:
I have a .jpg file, however when I open this in MATLAB it just appears as an image and I think it is just opening as a normal image, not sure how to accurately get the temperatures from this. I used the following to open it:
im=imread('C:\18. Bats\20190321_064039.jpg');
imshow(im);
I also have three other files, two are metadata (e.g. show date-time emissivity settings etc.) and one is a text file.
The text file appears to show the temperature of every pixel in the image.
e.g. (for a photo that had a minimum temperature of 15deg and max of 20deg it would be a text file with a minimum value of 1500 and maximum value of 2000)
1516 1530 1530 1540 1600 1600 1600 1600 1536 1536 ........
This file looks very useful, just wondering if there is some way I can open this as an image, probably in a program like MATLAB, which I think has image analysis so that I could draw around certain parts of the image (e.g. the wing of the bat) and find the average, max, min etc.
Has anyone had experience with this type of thing, can I just assign colours to numbers somehow? Or maybe other people have done it already and there is a much easier way. I will keep searching on the internet also and try to find out.
Alternatively maybe I need to open the .jpg image, draw around different parts, write a program to find out which pixels I drew around, find these in the txt file and then do averaging etc? Or somehow link the values in the text file to the .jpg file.
Sorry if this is the wrong place to ask, I can't find an image processing site on stack exchange.
All help is really appreciated, I will continue searching on the internet in the meantime.
the following worked in the end, it was much much easier than I thought it would be. Now a big fan of MATLAB, I thought it could take days to do this.
Just pasting here in case it is useful to someone else. I'm sure there is a more elegant way to write the code, however this is the first time I've used MATLAB in 20 years :p Use at your own risk, I haven't double checked I'm getting the correct results yet (though will do before I use it for anything important).
edit, since writing this I've found that the output .txt file of temperatures is actually sensor temperatures which need to be corrected for emissivity and background temperature to obtain the target temperatures. (One way to do this is to use the software which comes free with the camera to create new output .csv files of temperatures and use those instead).
Thanks to bla who put me on the right track with dlmread.
M=dlmread('C:\18. Bats\20190321_064039\20190321_064039_temps.txt') % read in the text file as a matrix (call it M)
% note that file seems to be a list of temperature values for each pixel
% e.g. 1934 1935 1935 1960 2000 2199...
M = rot90( M , 1 ) % rotate M anti-clockwise by 1*90 (All the pictures were saved sideways for some reason so rotate for easier viewing)
a = min(M(:)); % find the minimum temperature in the image
b = max(M(:)); % find the maximum temperature in the image
imresize(M,1.64); % resize the image to fit the computer screen prior to showing it on the screen
imshow(M,[a b]); % show image on the screen and fit the colours so that white is the value with the highest temperature in the image (b) and black is the lowest (a).
h = drawpolygon('FaceAlpha',0); % Let the user draw a polygon around the region of interest (ROI)
%(this stops code until polygon is drawn)
maskOfROI = h.createMask(); % For each pixel in the image assign a binary number, pixels inside the polygon (ROI) area are given 1 outside are 0
selectedValues = M(maskOfROI); % Now get the image values for all pixels where the mask value is '1' (i.e. all pixels within the polygon) and call this selectedValues.
averageTemperature = mean(selectedValues); % Get the mean of selectedValues (i.e. mean of the temperatures inside the polygon area)
maxTemperature = max(selectedValues); % Get the max of selectedValues
minTemperature = min(selectedValues); % Get the min of selectedValues
My figure has a lot of details. I found when generating a report, Matlab reduced the size of my figure. In the PDF, I needed to zoom to 200% to see the details of my figure clearly. I wanted to change the size of my figure when generating my report, but I couldn't find the option in the GUI. How should I do it? Thanks!
Take a look at the export_fig file on file exchange (https://www.mathworks.com/matlabcentral/fileexchange/23629-export-fig). This allows you to produce high resolution figures. For example, assuming that the figure you want to save is the current figure (gcf) then you could save it as follows:
export_fig fileName -pdf -m2
Where -m2 indicates magnification factor.
See the mathworks discussion about saving a figure at a specific size. For example, you can print a bar graph to a whole pdf page:
bar([1 10 7 8 2 2 9 3 6])
print('-fillpage','FillPageFigure','-dpdf')
I would like to export this picture to pdf format:
P2_tilde =
0.1029 0.4118 0.0245 0.1814 0.2794
0.3925 0.0234 0.0280 0.4626 0.0935
0.0928 0.1237 0.2680 0.2990 0.2165
0.0699 0.2219 0.0182 0.5106 0.1793
0.2611 0.0887 0.0837 0.3251 0.2414
figure('color',[1,1,1])
hBar2=bar3(P2_tilde);
colormap('pink')
set(hBar2,{'CData'},C);
set(gca,'xticklabel',surfaces)
set(gca,'yticklabel',surfaces)
surfaces={'Equipment','Patient','Hygiene products','Near-bed','Far-bed'};
colorbar
zlabel('Probability');
colormap('pink')
colorbar('location','southOutside')
set(gca,'xticklabel',surfaces)
set(gca,'yticklabel',surfaces)
surfaces={'Equipment','Patient','Hygiene products','Near-bed','Far-bed'};
zlabel('Probability');
Want to export to PDF format:
currentScreenUnits=get(gcf,'Units') % Get current screen units
currentPaperUnits=get(gcf,'PaperUnits') % Get current paper units
set(gcf,'Units',currentPaperUnits) % Set screen units to paper units
plotPosition=get(gcf,'Position') % Get the figure position and size
set(gcf,'PaperSize',plotPosition(3:4)) % Set the paper size to the figure size
set(gcf,'Units',currentScreenUnits) % Restore the screen units
print -dpdf ptilde % PDF called "ptilde.pdf"
Gives something completely off the page. Any thoughts how to center the figure on the canvas and make it only the size of the figure? Otherwise how can I trim it?
The PaperSizer parameter needs to be the actual size of the paper it will be printed on (the pdf file that will be displayed) not the size that the Matlab figure appears on the screen. For example, if you change it to this:
set(gcf,'PaperSize',[9,11])
you'll get something that looks reasonable.
I couldn't quite replicate your figure (errors in your code: need a definition for C) but use
f=figure('color',[1,1,1]);
%rest of figure code....
set(f,'PaperPositionMode','auto')
print -dpdf ptilde
Also, I'm not sure how to crop the pdf from within MATLAB, but if you want a cropped vector graphic use -depsc or -depsc2 as the print flag. see MATLAB help on print.
I'm making a GUI in MatLab that asks the user to upload a video file.
Next I want to play it in axes with a fixed window size . However, if the uploaded file is large, Matlab will expand the axes and take over most of my GUI. Is there a way to shrink the image to make it fit the axes?
Does anyone know how to solve this?
Usually Matlab axes are not supposed to change their position if the image is too big.
I can think of two possible problems:
The axes were large from the beginning, but showed small image with margins if the image is small enough
The command of showing the image that you are using is custom and it changes the axes size.
This question is old, but I stumbled across this (looking for something else) so perhaps it will help someone to see what I did.
I wanted to resize pretty large images (1024x 100k-200k pixels) so that my GUI can quickly demonstrate various color operations on a view of these large data sets. I just manually sub-sampled my data as follows (functions below).
Note that this example is an image. To spatially sub-sample a video, I have looped through the video and done something similar in the past on each frame.
[plotWidthPixels, plotHeightPixels] = getPlotAreaPixels(handles.figure1, handles.axes1);
[nSamplesPerLine nLines] = size(iqData);
colInds = decimateToNumber(nLines,plotWidthPixels);
rowInds = decimateToNumber(nSamplesPerLine,plotHeightPixels);
iqDataToPlot = iqData(rowInds,colInds);
First, I got the axis size in pixels:
function [plotWidthPixels, plotHeightPixels] = getPlotAreaPixels(figHandle, axisHandle)
set(figHandle,'Units','pixels')
figSizePix = get(figHandle,'Position');
set(axisHandle,'Units','normalized')
axSizeNorm = get(axisHandle,'Position');
axisSizePix = figSizePix.*axSizeNorm;
plotWidthPixels = ceil(axisSizePix(3)-axisSizePix(1));
plotHeightPixels = ceil(axisSizePix(4)-axisSizePix(2));
Then I used that to decimate the width and height of my image by getting sub-sets of indices that are (crudely approximately) evenly spaced:
function inds = decimateToNumber(lengthOfInitialVector, desiredVectorLength, initialIndex)
if nargin < 3
initialIndex = 1;
end
if (lengthOfInitialVector-initialIndex+1) > desiredVectorLength*2
inds = round(linspace(initialIndex,lengthOfInitialVector,desiredVectorLength));
else
inds = initialIndex:lengthOfInitialVector;
end