[SciPy-user] circumference in raster image

Robert Cimrman cimrman3@ntc.zcu...
Wed Jul 8 07:43:14 CDT 2009

Hi Zach,

Zachary Pincus wrote:
> Hi Robert,
> Basically, assuming the object is in a binarized array, you could use  
> ndimage to do one iteration of erosion, giving you the same object but  
> one pixel smaller. Then xor the eroded and original binary images to  
> give an image where the single-pixel border around the object is 1 and  
> the rest is zero; from here you can just sum the pixels to give a  
> (very rough) perimeter value. (Note of course that this doesn't  
> account for the spacing between pixels being different on the diagonal  
> than horizontal or vertical... for that you'd need some chain code  
> things, which I think ndimage doesn't provide.)

Just for the record how I did it (before knowing about your code): I 
have used np.gradient to obtain the edges (yes, the image is binary) and 
counted the circumference like this:

# pixel_sizes = (width, height) of a pixel in some lenght units
grad0, grad1 = np.gradient(mask)
val0 = len(grad0[np.where(grad0)]) * pixel_sizes[0]
val1 = len(grad1[np.where(grad1)]) * pixel_sizes[1]
circumference = 0.5 * (val0 + val1)

A poor man's approach, but worked too.


More information about the SciPy-user mailing list