Write Large Image as PDF in MATLAB - matlab

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!

Related

MATLAB figure convert into PDF without cutting out parts

I didn't use to have problems with this figure, but after enlarging the font size with 'export setup' parts on the left and right side are cut out (if I convert the MATLAB figure into PDF). If I convert the MATLAB figure into JPEG format, the whole figure is displayed.
I made a new MATLAB figure with the old font size, but the parts are still cut out.
did you try print?
F = getframe(gcf);
orient portrait
print ('-dpdf', 'myimage.pdf')
alternatively to portrait you can also use orient landscape or orient fill. Especially fill should resize it so that it fits.

How to make a montage of different-sized images

I'm trying to make a montage of figures in Matlab, each figure comprising a row - I have about 12 rows in total. I tried making the whole thing with subplot but the resolution is limited by the screensize and too low. Then I tried saving each figure as an image, using export_fig, and arranging these with subplot but again the resolution is too low. So then I tried montage, but it expects the images to be the same size and they aren't quite (height varies slightly), and export_fig doesn't seem to have the option to control the crop size. If anybody has any solutions I would be grateful!
This is my suggestion:
First resize your images to the same size.
For example,
B = imresize(A, [512 512]);
Second you can use montage or you can use imgdisp
Imgdisp gives you more options than montage. You can consider using it..

The image size is changed when the image is too big

I am using an image with size 4000*3000 pixels. When I show this image through the imshow function, the program shows:
Image is too big to fit on screen : displaying at 67%.
After that, when I want to find size of the image with function size(), the number of column is always multiplied by 3 from the original image. For example, when my image is 563*1000, this function show me 563*3000.
Could anyone tell me how to fix this problem?

Getting screen units matlab for saving to pdf

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.

Image resize issue

This appears to be a trivial problem but the result is strange, totally lost where I am going wrong. There is an input RGB image which needs to be converted to gray scale and sized to 1000 x 1000 pixels. This is how I have done
img=imread('flowers.jpg');
flowers_gray=rgb2gray(img);
flowers_resize=imresize(flowers_gray,[1000 1000]);
but strangely the output image is not of 1000 by 1000 pixels. Moreover, matlab did not save the image (tried using SaveAs option and the File --->Export Setup) gray scale mode
and also the size was incorrect since when I opened the saved image by
img1=imread('flowers_resize.jpg')
s=size(img1)
it gave
s=586 665 3
And the image flowers_resize.jpg is saved with a white border surrounding it in the image folder. So, I went to Paint toolbox to select the image A1 and manually deleted the surrounding background and resized the image.But alas, it saved the image with 3 color channels and not in gray scale mode although the size was correct! Can somebody please point out the correct way of resizing to 1000 by 1000 pixels and saving in gray scale mode without the white border surrounding the saved output file? Thank you.
When you use the image export processing, you are saving the entire figure including the space around the figure (white space).
Instead, use the imwrite command. In your case:
imwrite(A1,'flowers_resize.jpg','jpg');