How can I save an image with its plotted centroids?

2 次查看(过去 30 天)
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)

回答(2 个)

Walter Roberson
Walter Roberson 2016-7-5
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 个评论
Thorsten
Thorsten 2016-7-5
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 2016-7-6
Thanks for your help, I was able to solve the issue using saveas :)

请先登录,再进行评论。


Image Analyst
Image Analyst 2016-7-5
Or you can use export_fig
  4 个评论
Nathan Costin
Nathan Costin 2016-7-6
thank you very much for your help! I was able to solve the issue using saveas!
Image Analyst
Image Analyst 2016-7-6
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Read, Write, and Modify Image 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by