Freehand red circles Over on http://meta.stackoverflow.com, we have a few memes of our own. One of them is Freehand Red Circles.
See this post:

So, the challenge is,
can you draw freehand red circles... with code?
Additional restrictions:

You will take an image as input, and you must output the image with freehand red circle added.
Must be predictable, i.e. same image input must result in same output. You can use randomness, but results have to be consistent for same input.
Output must be exactly the same image as input, except with circle (no other alterations).
The freehand red circle must look freehand (no perfect circles!), be red (obviously), and look generally like a circle (no random squiggly lines).


This is a popularity-contest, so the answer with the most upvotes at the beginning of March 2014 will win. There is no specific goal, other than &quot;freehand red circles,&quot; so be as creative as possible so you get the most upvotes! (To be as unbiased as possible, I will upvote any answer that follows the rules.)
C + GD library
Instead of just drawing circles any old where, I thought it would be fun to find something red in the picture and draw a circle around that.
Here are some examples of the results obtained with a few photos from Wikimedia Commons:

And here's the code. It's a bit messy, but not too difficult to follow, I hope:
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;math.h&gt;
#include &lt;gd.h&gt;

#define min(a,b) ((a)&lt;(b)?(a):(b))
#define max(a,b) ((a)&gt;(b)?(a):(b))

/* Used for image segmentation */
int floodfill(int *tmp, int i, int w, int id) {
  int np=1;
  tmp[i]=id;
  if (tmp[i-w-1]&lt;0) np+=floodfill(tmp,i-w-1,w,id);
  if (tmp[i-w]&lt;0) np+=floodfill(tmp,i-w,w,id);
  if (tmp[i-w+1]&lt;0) np+=floodfill(tmp,i-w+1,w,id);
  if (tmp[i-1]&lt;0) np+=floodfill(tmp,i-1,w,id);
  if (tmp[i+1]&lt;0) np+=floodfill(tmp,i+1,w,id);
  if (tmp[i+w-1]&lt;0) np+=floodfill(tmp,i+w-1,w,id);
  if (tmp[i+w]&lt;0) np+=floodfill(tmp,i+w,w,id);
  if (tmp[i+w+1]&lt;0) np+=floodfill(tmp,i+w+1,w,id);
  return np;
}

int main(int argv, char *argc[]) {
  FILE          *infile,*outfile;
  gdImagePtr    img;
  int           *t, *tmp;
  int           w,h,x,y,r,g,b;
  int           c,redness,rgb;
  int           i,np,max,min,thresh;
  int           xt,yt,n;
  int           areaID,size,maxID;
  double        xmin,ymin,xmax,ymax,rad,r0,th;
  gdPoint       v[33];

  
  /* Check command line and open source JPEG file */
  if (argv!=3) return printf(&quot;Usage: %s &lt;in.jpg&gt; &lt;out.jpg&gt;\n&quot;,argc[0]);
  if (!(infile=fopen(argc[1],&quot;r&quot;))) return printf(&quot;Can't open &lt;%s&gt;\n&quot;,argc[1]);
  if (!(img=gdImageCreateFromJpeg(infile))) return printf(&quot;Bad JPEG: &lt;%s&gt;\n&quot;,argc[1]);
  fclose(infile);
  
  /* Extract red pixels and auto-threshold */
  w=img-&gt;sx;
  h=img-&gt;sy;
  np=w*h;
  t=tmp=calloc(np,sizeof(int));
  for (max=0,min=255,y=1;y&lt;h-1;y++) {
    for (x=1;x&lt;w-1;x++) {
      rgb=gdImageGetTrueColorPixel(img,x,y);
      r = (rgb&amp;0xff0000)&gt;&gt;16;
      g = (rgb&amp;0xff00)&gt;&gt;8;
      b = rgb&amp;0xff;
      redness = max(0,r-(max(g,b)+abs(g-b)));
      if (redness&gt;max) max=redness;
      if (redness&lt;min) min=redness;
      *t++ = redness;
    }
    t += 2;
  }
  thresh = (max+min)/2;
  for (t=tmp,i=0;i&lt;np;i++,t++) *t=((*t&gt;thresh)?-1:0);
  
  /* Label each area detected */
  areaID=1;
  maxID=0;
  max=-1;
  for (t=tmp,i=0;i&lt;np;i++,t++) {
    if (*t&lt;0) {
      size=floodfill(tmp,i,w,areaID);
      if (size&gt;max) {
        max = size;
        maxID = areaID;
      }
      areaID++;
    }
  }
  
  /* Calculate centre coordinates and area */
  if (max&gt;0) {
    xt=yt=n=xmax=ymax=0;
    xmin=w; ymin=h;
    for (t=tmp,y=0;y&lt;h;y++) {
      for (x=0;x&lt;w;x++) {
        if (*t++==maxID) {
          xt+=x;
          yt+=y;
          n++;
          if (x&lt;xmin) xmin=x;
          if (y&lt;ymin) ymin=y;
          if (x&gt;xmax) xmax=x;
          if (y&gt;ymax) ymax=y;
        }
      }
    }
    x = xt/(2*n) + (xmax+xmin)/4;
    y = yt/(2*n) + (ymax+ymin)/4;

    r0 = max(20,min(min(w,h),max(xmax-xmin,ymax-ymin))/2);
  }
  /* Default circle if nothing found */
  else {
    x=w/2; y=h/2; r0=min(w,h)/3;
  }
  
  /* Draw a red circle */
  for (th=4.0,i=0;i&lt;33;i++) {
    rad = r0 * (1.2 + (&quot; ,&lt;MYZVSB&gt;@EJIOSWZfgb^bbfgeZTOI@2&quot;[i]-87)/160.0);
    v[i].x = x + rad * sin(th);
    v[i].y = y + rad * cos(th);
    th += 0.22;
  }
  gdImageSetThickness(img,7);
  c = gdImageColorAllocate(img,255,0,0);
  gdImageOpenPolygon(img,v,33,c);
  
  /* Output results to file */
  printf(&quot;Saving...\n&quot;);
  if (!(outfile=fopen(argc[2],&quot;w&quot;))) {
    return printf(&quot;Can't open &lt;%s&gt; for writing\n&quot;,argc[2]);
  }
  gdImageJpeg(img,outfile,85);
  fclose(outfile);
  gdImageDestroy(img);
  printf(&quot;Finished\n&quot;);
  return 0;
}


Note: Markdown messed up my link in the comments, so I'll just point out that the code uses segmentation to identify all the areas of red in the picture, and then draws a circle around the largest of these. For example, this image:

produces the following output: