How can I save an image with its plotted centroids?

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)

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

I've tried to impletment getframe however I keep getting an error message and I'm not sure what I'm doing wrong. Here is the error message followed by code. Could you point out where I'm wrong please?
Error using graphicsversion Input was not a valid graphics object
Error in getframe (line 51) usingMATLABClasses = ~graphicsversion(parentFig, 'handlegraphics');
Error in BacteriaCounter (line 45) image = getframe('Figure 1');
image = getframe('Figure 1');
I've now used
image = getframe(gcf);
however I'm now getting the following error, do you know any solutions to this:
Error using imwrite (line 420) Expected DATA to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64, logical
Instead its type was struct.
Error in BacteriaCounter (line 51) imwrite(image, locFull2)
Please do not name a variable image as that interferes with use of the image function and confuses other readers.
If you look at the documentation of getframe(), you will see it returns a structure. That structure has two fields, 'cdata' and 'map'. For RGB images the cdata is the data you want to imwrite(). I do not think getframe() can return indexed images, but I could be wrong. The 'map' field is either empty (for RGB images) or the RGB color map (when the 'cdata' field is data for an indexed image.) For typical imwrite() calls you can just go ahead and pass in the fields:
imwrite(captured_frame.cdata, captured_frame.map, locFull2)
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
Thanks for your help, I was able to solve the issue using saveas :)

Sign in to comment.

Or you can use export_fig

4 Comments

Will have a look through and try and implement it, thank you! :)
Warning: getframe does NOT maintain the size of your original image. It gives you the displayed size, which can vary depending on how much room on your screen your figure takes up. Use getimage() to get the actual image instead of basically a screen capture. However, getimage() does not capture any graphics your put into the overlay so you'd have to burn those into the image, for example with the insertShape function that Walter mentioned.
See code that demonstrates this:
grayImage = imread('moon.tif');
[rows, columns, numberOfColorChannels] = size(grayImage)
imshow(grayImage);
hold on;
plot(100, 200, 'r+', 'LineWidth', 4, 'MarkerSize', 30);
frameStructure = getframe;
displayedImage = getimage();
[rows, columns, numberOfColorChannels] = size(frameStructure.cdata)
[rows, columns, numberOfColorChannels] = size(displayedImage)
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
frameStructure = getframe;
displayedImage = getimage();
[rows, columns, numberOfColorChannels] = size(frameStructure.cdata)
[rows, columns, numberOfColorChannels] = size(displayedImage)
thank you very much for your help! I was able to solve the issue using saveas!
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 Images in Help Center and File Exchange

Asked:

on 5 Jul 2016

Commented:

on 6 Jul 2016

Community Treasure Hunt

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

Start Hunting!