Clear Filters
Clear Filters

How can I save an image with its plotted centroids?

2 views (last 30 days)
I have found the center of some cells in an image and successfully plotted the centroids over these positions. However when I attempt to save the image only the original image is saved. Is there a way of saving the image with the centroids plotted? The code I am using is below and starts after I have converted the image 'I' to a binary image and done some processing to remove the noise:
baccenter = regionprops(imclean4, 'centroid');
centroids = cat(1, baccenter.Centroid);
measurements = regionprops(imclean4, 'BoundingBox', 'Area');
centroiddata = regionprops('table', imclean4, 'Centroid')
areadata = regionprops('table', imclean4, 'Area')
imshow(I)
hold on
plot(centroids(:,1),centroids(:,2), 'b*')
hold off
number_of_bacteria = numel(centroiddata)
locPath2 = '\\userfs\nac515\w2k\MATLAB\Tables and Pictures';
prompt2 = 'What would you like the image file to be called? ';
[~,locName] = fileparts(input(prompt2,'s'));
locFull2 = fullfile(locPath2,[locName,'.jpg']);
imwrite(I, locFull2)

Answers (2)

Walter Roberson
Walter Roberson on 5 Jul 2016
You can use getframe to get a copy of what was rendered to the screen, and then imwrite() it to a file.
If you have Computer Vision Toolbox then you can instead not plot at all, and instead use insertShape to insert markers into the image array, and then imwrite() the image array.
  5 Comments
Thorsten
Thorsten on 5 Jul 2016
You can use the following little convenience function to get things done:
function figwrite(filename)
if nargin == 0 % write to default file
filename = 'fig.png';
if exist('fig.png', 'file')
s = input(['Overwrite existing file ' filename '? Y/N >>'], 's');
if ~strcmpi(s, 'Y')
disp('No file written.')
return
end
end
end
F = getframe(gcf);
if isempty(F.colormap)
imwrite(F.cdata, filename);
else
imwrite(F.cdata, F.colormap, filename);
end
Nathan Costin
Nathan Costin on 6 Jul 2016
Thanks for your help, I was able to solve the issue using saveas :)

Sign in to comment.


Image Analyst
Image Analyst on 5 Jul 2016
Or you can use export_fig
  4 Comments
Nathan Costin
Nathan Costin on 6 Jul 2016
thank you very much for your help! I was able to solve the issue using saveas!
Image Analyst
Image Analyst on 6 Jul 2016
OK, fine if you don't care about the size. Like getframe, you don't get the same size as the original image. See the note for saveas in the help "Starting in R2016a, the size of saved figures match the size of the figure on the screen by default." So it saves the whole figure, not just the axes control only, and the size is different. But if you're okay with that, then have at it.

Sign in to comment.

Categories

Find more on Read, Write, and Modify Image 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!