View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Circle pixel coordinates using mid-point algorithm

4.8 | 5 ratings Rate this file 10 Downloads (last 30 days) File Size: 2.67 KB File ID: #33844 Version: 1.1
image thumbnail

Circle pixel coordinates using mid-point algorithm



20 Nov 2011 (Updated )

Return the optimal pixel coordinates of a circle, given its center and radius.

| Watch this File

File Information

GETMIDPOINTCIRCLE return the x,y pixel coordinates of a circle
  [x y] = getmidpointcircle(x0, y0, radius) returns the pixel coordinates
  of the circle centered at pixel position [x0 y0] and of the given integer
  radius. The mid-point circle algorithm is used for computation
  This function is aimed at image processing applications, where the
  integer pixel coordinates matter, and for which one pixel cannot be
  missed or duplicated. In that view, using rounded trigonometric
  coordinates generated using cosine calls are inadequate. The mid-point
  circle algorithm is the answer.
  Accent is made on performance. We compute in advance the number of point
  that will be generated by the algorithm, to pre-allocate the coordinates
  arrays. I have tried to do this using a MATLAB class implementing the
  iterator pattern, to avoid computing the number of points in advance and
  still be able to iterate over circle points. However, it turned out that
  repeated function calls is extremely expansive, and the class version of
  this function is approximately 1000 times slower. With this function, you
  can get the pixel coordinates of a circle of radius 1000 in 0.16 ms, and
  this time will scale linearly with increasing radius (e.g. it takes
  0.16 s for a radius of 1 million).
  Also, this functions ensure that sorted coordinates are returned. The
  mid-point algorithm normally generates a point for the 8 circles octants
  in one iteration. If they are put in an array in that order, the [x y]
  points will jump from one octant to another. Here, we ensure that they
  are returned in order, starting from the top point, and going clockwise.
  n_circles = 20;
  color_length = 100;
  image_size = 128;
  max_radius = 20;
  I = zeros(image_size, image_size, 3, 'uint8');
  colors = hsv(color_length);
  for i = 1 : n_circles
      x0 = round( image_size * rand);
      y0 = round( image_size * rand);
      radius = round( max_radius * rand );
      [x y] = getmidpointcircle(x0, y0, radius);
      index = 1 ;
      for j = 1 : numel(x)
          xp = x(j);
          yp = y(j);
          if ( xp < 1 || yp < 1 || xp > image_size || yp > image_size )
          I(xp, yp, :) = round( 255 * colors(index, :) );
          index = index + 1;
          if index > color_length
              index = 1;
  imshow(I, []);

MATLAB release MATLAB 7.12 (R2011a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
22 Apr 2016 Xavier Berthelon

@Konstantinos Stamatopoulos

No need to get the [x, y] in your case I think. A simple circle plot using the function 'hold on' will display both your image A and on top the circle with specified radius and center.
Something like this:

th = 0:pi/10:2*pi;
xunit = radius* cos(th) + x_circle;
yunit = radius * sin(th) + y_circle;
hold on
h = plot(xunit, yunit);
hold off

Comment only
22 Apr 2016 Xavier Berthelon

08 Mar 2016 Konstantinos Stamatopoulos

How can you change only the pixel values with specified coordinates generated with this function in an existed image?

I mean: I have an tiff image (A) 100x780...I specify the x0, y0, radius of the circle (x0 & y0 are pixel coordinates based on image A)....and then I obtain the [x y] from the function. Then, I want to change only the values of those pixels in the image A that I know the coordinates and finally display the whole image A with the changed pixels...So at the end I want to be able to display the original image with the cirle

Comment only
23 Sep 2015 mohammad hajjar

15 Jan 2015 Zohar Bar-Yehuda

18 Feb 2014 Jun

Jun (view profile)

nice work

09 Apr 2013 Juan

Juan (view profile)

It works well, thanks for uploading

21 Feb 2013 1.1

Adds an analytical expression for the predicted number of data points, needed to pre-allocate arrays correctly. Gives a minor performance boost.

Contact us