# how to draw circle in an image?

327 views (last 30 days)
Nikhil on 6 Nov 2012
Commented: Benjamin Drewry on 3 Oct 2019
Hello Everyone
I am currently working on the project of object recognition using matlab. but i am not getting how to draw circle with specific radius on specific image. i had calculated the centroid of the image, and using these as my center point i want to draw circle. please assist me.
Thanks
Benjamin Drewry on 3 Oct 2019

kittu on 25 Nov 2012
you can draw the circle using the following code:
%e
%x = x coordinates of the centroid
%y = y coordinates of the centroid
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
manish meena on 19 Jun 2016

Matthew Eicholtz on 29 Jan 2016
In case someone else stumbles upon this question like I did and is unsatisfied with the current answers, I have found that insertShape is a useful function for adding rasterized shapes to an image. Note that this requires the Computer Vision System Toolbox.
help insertShape

Image Analyst on 25 Nov 2012
See my demo. It does that. It will just require slight modifications to turn the ellipse into a circle (because there's an imellipse but no imcircle function so you can use bwperim if you want the outline), and to make the circle an outline rather than a solid (if that's what you want, but you didn't make that clear).
% Demo to write an ellipse and a line into the overlay of an image,
% and then to burn those overlays into the image.
%----- Initializing steps -----
% Clean up
clc;
clear all;
close all;
workspace; % Display the workspace panel.
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
% User said No, so exit.
return;
end
end
% Display images to prepare for the demo.
subplot(2, 4, 1);
imshow(monochromeImage);
title('Original Image');
subplot(2, 4, 2);
imshow(monochromeImage);
title('Original Image with ellipse in overlay');
subplot(2, 4, 5);
imshow(monochromeImage);
title('Original Image');
subplot(2, 4, 6);
imshow(monochromeImage);
title('Original Image with line in overlay');
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Image Analysis Demo','numbertitle','off')
%----- Burn ellipse into image -----
% Create elliptical mask, h, as an ROI object over the second image.
subplot(2, 4, 2);
hEllipse = imellipse(gca,[10 10 50 150]); % Second argument defines ellipse shape and position.
% Create a binary image ("mask") from the ROI object.
subplot(2, 4, 3);
imshow(binaryImage);
% Let's try to add some text. (Doesn't work)
% hText = text(50, 100, 'Line of Text');
% binaryImage = binaryImage & textMask;
% imshow(binaryImage);
% Burn ellipse into image by setting it to 255 wherever the mask is true.
monochromeImage(binaryImage) = 255;
% Display the image with the "burned in" ellipse.
subplot(2, 4, 4);
imshow(monochromeImage);
title('New image with ellipse burned into image');
%----- Burn line into image -----
% Create line mask, h, as an ROI object over the second image in the bottom row.
subplot(2, 4, 6);
hLine = imline(gca,[10 100],[10 100]); % Second argument defines line endpoints.
% Create a binary image ("mask") from the ROI object.
subplot(2, 4, 7);
imshow(binaryImage2);
% Burn line into image by setting it to 255 wherever the mask is true.
burnedImage(binaryImage2) = 255;
% Display the image with the "burned in" line.
subplot(2, 4, 8);
imshow(burnedImage);
title('New image with line burned into image');

Walter Roberson on 6 Nov 2012

Matt J on 25 Nov 2012