Saving image in correct format or how do I save only image-part of the figure?

1 view (last 30 days)
I want to save the "image part" of the figure as .jpeg. That is, everwything that is inside of the axis (without the axis and the gray parts). Note i dont want to change the number of pixels. For clarification I want to save only the image that is inside the red line below. Just a illustration below.
Please tell me its possible? If so, how?
Would solve a lot of issues with ZEMAX and MATLAB.
My code at the moment in MATLAB to plot detectordata from ZEMAX:
figure('units','normalized','outerposition',[0 0 1 1])
axis([1 800 1 800]);
shading flat;
xlabel('Column #');
ylabel('Row #');
detectorData here is a 800x800 double,.. but i want to save this as an image, just like the format I get if I save in ZEMAX, see table below.
Saving detectorData via MATLAB with imwrite(detectorData,fileNameTmp) when plotting it with imshow(detectorData,[]) gives the wrong format, see via MATLAB in table.
Plotting detectorData as surf and saving the surf figure with exportgraphics(gcf,'test2.jpeg') gives me the right format of the saved image but then the gray frame and axes is there also, which would coplicate analysis further on as I don't want to change the pixel size 800x800 and have other bright areas.
I am going to use the image for further analysis later and the using the saved matrix 800x800 double is not working. I need to change the scale to somewhere between 0 and 1, and then rescale it to 12 bit for my analysis later on. Thus, using this code to upload the ZEMAX image:
I0 = imread([imageloc,folderName,'infinity_lobe_',num2str(l),'mm.jpg']);
% I1 = rgb2gray(I0); % change from gray scale to rgb
I= im2double(I0)*2^12;% convert to double (max 1,0) with 2^12 bit scale
The image I save directly from ZEMAX and upload to MATLAB later on is in gray-scale, 800x800x3 uint8 format in MATLAB, which is what i want. According to imfinfo the ZEMAX image in the table below has Bit Depth 24 and truecolor with No.Of.Samples 3, which my code with imshow and imwrite does not give.
I know how the save it in ZEMAX but I need to do several simulations, so saving those manually is cumbersum.
  1 Comment
Happy PhD
Happy PhD on 16 Apr 2021
Edited: Happy PhD on 16 Apr 2021
I think the issue is the range of the image. When I plot it, I plot it like imshow, (I, []). Thus, plotting it in figure it looks nice but plotting the save image its look like a lot f information is gone and the bright parts are just white blobs.

Sign in to comment.

Answers (2)

Clayton Gotberg
Clayton Gotberg on 16 Apr 2021
This answer by Adam Danz shows how you can remove the borders and interactive parts of the figure window.
After you've done so, you should be able to use exportfig to get the same format as your ZEMAX images. You may need to change the size of the figure window to preserve the size of the saved image, but you will have your images without any pesky white space or axes.
figure('Menu','none','ToolBar','none'); % turns off the menu and toolbar (note this includes the save button)
axes('Units','Normalize','Position',[0 0 1 1]) %Expands the
placeholder % plotting command
If your application absolutely requires that you save only a certain part of a figure, you could use Sladjana Lazarevic's solution (in the hidden comments) to cut out a small part of the figure window with getframe and save that as an image.

Image Analyst
Image Analyst on 16 Apr 2021
Edited: Image Analyst on 16 Apr 2021
Is it really just an image, or do you have an image with graphics plotted over it in the overlay, or is it a graph created with things like plot() or scatter()?
If it's just an image, you can just simply use imwrite().
Otherwise, have you tried the exportgraphics() function?


Find more on Image Preview and Device Configuration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by