How can I save an image with its plotted centroids?
Show older comments
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
on 5 Jul 2016
0 votes
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
Nathan Costin
on 5 Jul 2016
Nathan Costin
on 5 Jul 2016
Walter Roberson
on 5 Jul 2016
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)
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
on 6 Jul 2016
Image Analyst
on 5 Jul 2016
0 votes
4 Comments
Nathan Costin
on 5 Jul 2016
Image Analyst
on 5 Jul 2016
Edited: Image Analyst
on 5 Jul 2016
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)
Nathan Costin
on 6 Jul 2016
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.
Categories
Find more on Images 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!