How can I draw a circle on an image in MATLAB?

67,816

Solution 1

You could use the normal PLOT command with a circular marker point:

[x_p,y_p] = find(points);
imshow(im);         %# Display your image
hold on;            %# Add subsequent plots to the image
plot(y_p,x_p,'o');  %# NOTE: x_p and y_p are switched (see note below)!
hold off;           %# Any subsequent plotting will overwrite the image!

You can also adjust these other properties of the plot marker: MarkerEdgeColor, MarkerFaceColor, MarkerSize.

If you then want to save the new image with the markers plotted on it, you can look at this answer I gave to a question about maintaining image dimensions when saving images from figures.

NOTE: When plotting image data with IMSHOW (or IMAGE, etc.), the normal interpretation of rows and columns essentially becomes flipped. Normally the first dimension of data (i.e. rows) is thought of as the data that would lie on the x-axis, and is probably why you use x_p as the first set of values returned by the FIND function. However, IMSHOW displays the first dimension of the image data along the y-axis, so the first value returned by FIND ends up being the y-coordinate value in this case.

Solution 2

This file by Zhenhai Wang from Matlab Central's File Exchange does the trick.

%----------------------------------------------------------------
% H=CIRCLE(CENTER,RADIUS,NOP,STYLE)
% This routine draws a circle with center defined as
% a vector CENTER, radius as a scaler RADIS. NOP is 
% the number of points on the circle. As to STYLE,
% use it the same way as you use the rountine PLOT.
% Since the handle of the object is returned, you
% use routine SET to get the best result.
%
%   Usage Examples,
%
%   circle([1,3],3,1000,':'); 
%   circle([2,4],2,1000,'--');
%
%   Zhenhai Wang <[email protected]>
%   Version 1.00
%   December, 2002
%----------------------------------------------------------------

Solution 3

Funny! There are 6 answers here, none give the obvious solution: the rectangle function.

From the documentation:

Draw a circle by setting the Curvature property to [1 1]. Draw the circle so that it fills the rectangular area between the points (2,4) and (4,6). The Position property defines the smallest rectangle that contains the circle.

pos = [2 4 2 2];
rectangle('Position',pos,'Curvature',[1 1])
axis equal

So in your case:

imshow(im)
hold on
[y, x] = find(points);
for ii=1:length(x)
  pos = [x(ii),y(ii)];
  pos = [pos-0.5,1,1];
  rectangle('position',pos,'curvature',[1 1])
end

As opposed to the accepted answer, these circles will scale with the image, you can zoom in an they will always mark the whole pixel.

Share:
67,816
Nathan Fellman
Author by

Nathan Fellman

SOreadytohelp

Updated on December 26, 2020

Comments

  • Nathan Fellman
    Nathan Fellman over 3 years

    I have an image in MATLAB:

    im = rgb2gray(imread('some_image.jpg');
    % normalize the image to be between 0 and 1
    im = im/max(max(im));
    

    And I've done some processing that resulted in a number of points that I want to highlight:

    points = some_processing(im);
    

    Where points is a matrix the same size as im with ones in the interesting points.

    Now I want to draw a circle on the image in all the places where points is 1.

    Is there any function in MATLAB that does this? The best I can come up with is:

    [x_p, y_p] = find (points);
    
    [x, y] = meshgrid(1:size(im,1), 1:size(im,2))
    r = 5;
    
    circles = zeros(size(im));
    
    for k = 1:length(x_p)
        circles = circles + (floor((x - x_p(k)).^2 + (y - y_p(k)).^2) == r);
    end
    
    % normalize circles
    circles = circles/max(max(circles));
    
    output = im + circles;
    
    imshow(output)
    

    This seems more than somewhat inelegant. Is there a way to draw circles similar to the line function?

  • Zaid
    Zaid over 14 years
    You might want to add hold off to the code as well for sake of completeness.
  • Zaid
    Zaid over 14 years
    @gnovice: Are you sure about switching the x_p and y_p in the plot call?
  • gnovice
    gnovice over 14 years
    @Zaid: Yes, I added a note to my answer explaining why they need to be flipped.
  • gnovice
    gnovice over 14 years
    How are you getting the values x and y? If you get them from the FIND command, you will likely have to flip them. You might not have to flip them if you found them by other means.
  • gnovice
    gnovice over 14 years
    The question is asking how to draw circles around the points, not change the value of the image at them.
  • Hanmyo
    Hanmyo over 13 years
    When you scale the image, the point will not scale with it - keep this in mind.