필터 지우기
필터 지우기

how to draw circle in an image?

조회 수: 214 (최근 30일)
Nikhil
Nikhil 2012년 11월 6일
답변: DGM 2023년 2월 12일
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

채택된 답변

kittu
kittu 2012년 11월 25일
you can draw the circle using the following code:
%e
%r= desired radius
%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);
  댓글 수: 1
manish meena
manish meena 2016년 6월 19일
thank you for your help

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Matthew Eicholtz
Matthew Eicholtz 2016년 1월 29일
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
Image Analyst 2012년 11월 25일
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.
hasIPT = license('test', 'image_toolbox');
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');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Display images to prepare for the demo.
monochromeImage = imread('pout.tif');
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.
binaryImage = hEllipse.createMask();
% Display the ellipse mask.
subplot(2, 4, 3);
imshow(binaryImage);
title('Binary mask of the ellipse');
% Let's try to add some text. (Doesn't work)
% hText = text(50, 100, 'Line of Text');
% textMask = hText.createMask();
% 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 -----
burnedImage = imread('pout.tif');
% 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.
binaryImage2 = hLine.createMask();
% Display the line mask.
subplot(2, 4, 7);
imshow(binaryImage2);
title('Binary mask of the line');
% 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
Walter Roberson 2012년 11월 6일

Matt J
Matt J 2012년 11월 25일
편집: DGM 2023년 2월 12일
See also imellipse()

DGM
DGM 2023년 2월 12일
As of R2018b, this can be done interactively using ROI tools: drawcircle() drawellipse(), and createMask().
Alternatively, if you want a simple antialiased circle, that's a different story.
% let's say you have an image
inpict = imread('peppers.png'); % uint8 (RGB)
sz = size(inpict);
% create a mask with a circle
r = 125;
c = [100 100]; % [y x]
mask = drawaacircle(sz(1:2),c,r);
% use the mask to set the color in the target image
fgcolor = [1 0.7 0];
outpict = mask.*reshape(fgcolor,1,1,[]) + (1-mask).*im2double(inpict);
outpict = im2uint8(outpict);
% do it again, but make the circle semitransparent purple
% create a mask with a circle
r = 100;
c = [300 400]; % [y x]
mask = drawaacircle(sz(1:2),c,r);
mask = 0.5*mask; % 50% opacity
% use the mask to set the color in the target image
fgcolor = [0.7 0.3 1];
outpict = mask.*reshape(fgcolor,1,1,[]) + (1-mask).*im2double(outpict);
outpict = im2uint8(outpict);
% display the result
imshow(outpict)
% create a crude antialiased circle mask
function circ = drawaacircle(sz,c,r)
xx = 1:sz(2);
yy = (1:sz(1)).';
circ = sqrt((xx-c(2)).^2 + (yy-c(1)).^2);
circ = min(max(-(circ-r)/2,0),1);
end

Community Treasure Hunt

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

Start Hunting!

Translated by