How to save an image with plotted lines and annotations

Hi, i am doing a batch processing task and for each image i will get a thresholded image with lines plotted ontop showing the initial point of spray and final point of spray and a value for the length of spray. However, i am needing to save all of this to one file like the image below. My matlab knowledge is depressingly basic. I was trying to use the imwrite function below as this will allow me to put all the processed images in the same folder, however, i do not know how to add the plotted lines and annotations to the thresholded image.Does anyone know how to do this? Thank you in advance!
% plot thresholded image
imagesc(BWpen);axis image; hold on;
% plot max penetration onto BWpen
plot(result(:,1),result(:,2),'r-.')
[And the line for nozzle exit]
% annotate with length of penetration
text(InjectorX-60,InjectorY-30,...
sprintf('%1.3f',meani)],'color','b','FontSize',14,'FontWeight','bold');
% attempt to write into folder
imwrite([BWpen], ['cropped\' filelist(k).name]);

1 commentaire

And sorry, secondly, how could i write the values of the maximum penetration for each image into a file so that i could later draw graphs from them? [where the value for maximum penetration is given by 'meani'.in the above code] Thank yoooouu!!

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 19 Mar 2014

0 votes

export_fig() (the most downloaded MATLAB app of all time) is probably the most common way. See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_save_my_figure.2C_axes.2C_or_image.3F_I.27m_having_trouble_with_the_built_in_MATLAB_functions.

14 commentaires

Thank you very much!
Will this work within a GUI as well?
Yes. Below is a snippet from a pulldown menu where I allow the user to save a screenshot of their GUI at that point in time:
% --------------------------------------------------------------------
function mnuFileSaveScreenshot_Callback(hObject, eventdata, handles)
% hObject handle to mnuFileSaveScreenshot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
try
filterSpec = '*.*';
defaultName = sprintf('%s\\Screenshot.png', handles.calibrationFolder);
dialogTitle = 'Save Screenshot?';
[baseFileName, folder] = uiputfile(filterSpec, dialogTitle, defaultName);
if baseFileName == 0
% User clicked cancel.
return; % Bail out.
end
fullFileName = fullfile(folder, baseFileName);
message = sprintf('Please wait...\n\nSaving screenshot image as:\n%s', fullFileName);
set(handles.txtInfo, 'String', message);
drawnow; % Needed to get it to refresh.
export_fig(fullFileName, handles.figMainWindow);
message = sprintf('Done!\n\nScreenshot image saved as:\n%s', fullFileName);
set(handles.txtInfo, 'String', message);
msgboxw(message);
% close(hFigure);
catch ME
message = sprintf('Error in mnuFileSaveScreenshot_Callback():\n%s', ME.message);
WarnUser(message);
end
return; % from mnuFileSaveScreenshot_Callback
for some reason when i am using
export_fig(sprintf('plot%d', k), '-a1', '-jpeg');
it overwrites every image saved in the for loop where
k = 1:numel(filelist)
however, i thought this was the command for multiple image files, any ideas? Thank you
Try this:
baseFileName = sprintf('plot%d', k)
fullFileName = fullfile(pwd, baseFileName)
fprintf('Exporting current axes to %s', fullFileName);
export_fig(fullFileName, '-a1', '-jpeg');
What do you see? Does fullFileName change every time, or not?
Jed
Jed le 27 Mar 2014
Modifié(e) : Jed le 27 Mar 2014
I still just get left with one file called plot512 and its a screenshot of the final image in the sequence. In the command window the following message is displayed repetitively on one line.
"Exporting current axes to H:\Individual Project\MATLAB\SprayExamples\2\3\plot512"
From looking in the little thumbnail window it looks as if its overwriting
Change this line:
fprintf('k = %d\nExporting current axes to %s', k, fullFileName);
For some reason your k is not incrementing in your loop.
Still no change unfortunately, its still just overwriting and calling the file plot512
Image Analyst
Image Analyst le 28 Mar 2014
Modifié(e) : Image Analyst le 28 Mar 2014
Jed, your responses are very very terse - almost no information for me to help you. Can you just attach your code? It seems like k is not changing though you did not tell me what the fprintf was giving out so I'll just have to run it myself to see what the values of k are. Is it possible you had an earlier loop over k, which left it at 512, and that this loop is actually over some different variable like i or j or something? That would explain it.
You can always use the paper clip to attach longer files.
Can you explain why you reassign k inside the k loop with this line:
k=y;
What is y? Is it 512? If so, then that's probably the source of your problem.
Aw i'm sorry that must have come in when i copied sections from different files to create a batch run. And yes the original image is 512x512pixels.I feel very silly, sorry for wasting your time. It now works perfectly. Thank you
Jed
Jed le 8 Avr 2014
Modifié(e) : Jed le 8 Avr 2014
Is there anyway of getting rid of the background of the gui here? i have looked through all of the different ways people talk of doing it but people either seem to say use getframe or exportfig, but not how you do it with exportfig and i know you stick strongly by export_fig and i think when questioned about it before you said use export_fig and its cropping function, but how do you actually do this cropping part?
i am still using the following code you sent me:
baseFileName = sprintf('plot%d', k);
fullFileName = fullfile(pwd, baseFileName);
fprintf('k = %d\nExporting current axes to %s', k, fullFileName);
export_fig(handles.axes1,fullFileName, '-a1', '-png');
and the picture saved looks like:
Thank you very much in advance
I don't know. I've just used it for capturing screenshots of the entire figure, not of just one axes on the figure. I'd try to contact Oliver Woodford, who wrote export_fig. Worst case, read it in, crop it (hopefully it's the same size every time) and re-save it.
Yea good idea, thanks

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Images dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by