[Insight-developers] To draw a cross or a circle on image

Paul Koshevoy koshevoy at sci.utah.edu
Tue May 23 12:42:07 EDT 2006


Alan wrote:
> Hi folks !
>
> Is there a way to draw a cross or a circle on an image ?
> Are there any primitives to do it ?
> Thanks for any hints.
>
> Alan
> _______________________________________________
> Insight-developers mailing list
> Insight-developers at itk.org
> http://www.itk.org/mailman/listinfo/insight-developers
>   
Here is some sample code to draw crosses:


//----------------------------------------------------------------
// p2x1_t
//
typedef itk::Point<double, 2> p2x1_t;

//----------------------------------------------------------------
// v2x1_t
//
typedef itk::Vector<double, 2> v2x1_t;

//----------------------------------------------------------------
// mark
//
template <class image_t>
void
mark(typename image_t::Pointer & image,
     const typename image_t::IndexType & index,
     const typename image_t::PixelType & mark_value,
     const int arm_length = 2,
     const char symbol = '+')
{
  typedef typename image_t::SpacingType spacing_t;
  typedef typename image_t::RegionType::SizeType image_size_t;
  typedef typename image_t::IndexType index_t;
 
  // calculate image bounding box:
  spacing_t sp = image->GetSpacing();
  image_size_t sz = image->GetLargestPossibleRegion().GetSize();
  p2x1_t min = image->GetOrigin();
  p2x1_t max = min + v2x1(sp[0] * double(sz[0] - 1),
              sp[1] * double(sz[1] - 1));
 
  index_t xy;
  for (int j = -arm_length; j <= arm_length; j++)
  {
    int x = index[0] + j;
    int y = index[1] + j;
   
    if (symbol == '+')
    {
      // draw a cross:
      xy[0] = x;
      xy[1] = index[1];
      if (xy[0] >= 0 && xy[0] < sz[0] &&
      xy[1] >= 0 && xy[1] < sz[1])
      {
    image->SetPixel(xy, mark_value);
      }
     
      xy[0] = index[0];
      xy[1] = y;
      if (xy[0] >= 0 && xy[0] < sz[0] &&
      xy[1] >= 0 && xy[1] < sz[1])
      {
    image->SetPixel(xy, mark_value);
      }
    }
    else
    {
      // draw a diagonal cross:
      xy[0] = x;
      xy[1] = y;
      if (xy[0] >= 0 && xy[0] < sz[0] &&
      xy[1] >= 0 && xy[1] < sz[1])
      {
    image->SetPixel(xy, mark_value);
      }
     
      xy[1] = index[1] - j;
      if (xy[0] >= 0 && xy[0] < sz[0] &&
      xy[1] >= 0 && xy[1] < sz[1])
      {
    image->SetPixel(xy, mark_value);
      }
    }
  }
}

//----------------------------------------------------------------
// mark
//
template <class image_t>
void
mark(typename image_t::Pointer & image,
     const p2x1_t & mark_coords,
     const typename image_t::PixelType & mark_value,
     const int arm_length = 2,
     const char symbol = '+')
{
  typedef typename image_t::IndexType index_t;
 
  index_t index;
  if (!image->TransformPhysicalPointToIndex(mark_coords, index))
  {
    // the mark lays outside of the image:
    return;
  }
 
  mark<image_t>(image, index, mark_value, arm_length, symbol);
}



More information about the Insight-developers mailing list