[Insight-users] Finding the closest pixel with a specific criteria

David Doria daviddoria at gmail.com
Wed Aug 31 16:21:07 EDT 2011


On Wed, Aug 31, 2011 at 4:00 PM, David Doria <daviddoria at gmail.com> wrote:
> I would like to find the closest pixel to a query pixel that passes
> some kind of test. For example, I would like to find the closest pixel
> to (10,10) that is non-zero. Is there an easy way to do this? I wrote
> a function that uses a NeighborhoodIterator with a successively larger
> radius until a suitable pixel is found. However, this is horribly
> slow.
>
> Any suggestions?

Well there was a serious error in my function - I was looping over the
whole image with the neighborhood iterator instead of just the
neighborhood of the query pixel (see revised below). My question still
holds though - is there a built in way to do something like this? And
#2 - is there a better way to loop over a pixels neighborhood than
setting up a single pixel region as I've done?

Thanks,

David

bool FindClosestNonZeroPixel(MaskImageType::Pointer image,
itk::Index<2> queryPixel, unsigned int radiusValue, itk::Index<2>&
returnPixel)
{
  itk::Index<2> zeroIndex;
  zeroIndex.Fill(0);

  ImageType::SizeType radius;
  radius.Fill(radiusValue);

  itk::Size<2> size;
  size.Fill(1);
  itk::ImageRegion<2> region(queryPixel,size);

  itk::ConstNeighborhoodIterator<MaskImageType> iterator(radius, image, region);

  unsigned int length = (radiusValue*2)+1;
  while(!iterator.IsAtEnd())
    {
    for(unsigned int i = 0; i < length*length; i++)
      {
      bool inBounds;
      ImageType::PixelType pixel = iterator.GetPixel(i, inBounds);
      if(pixel != 0)
	{
	returnPixel = iterator.GetIndex();
	return true;
	}

      }
    ++iterator;
    }

  return false;
}


More information about the Insight-users mailing list