Login  Register

Re: Padding in ImageJ

Posted by Rasband, Wayne (NIH/NIMH) [E] on Nov 01, 2012; 11:29am
URL: http://imagej.273.s1.nabble.com/Padding-in-ImageJ-tp5000622p5000626.html

On Nov 1, 2012, at 5:48 AM, bateman wrote:

> Hi,
>
> I wish to pad an image with zeros so that I may perform calculations on an
> image i.e. distances to other pixels etc.
>
> I've seen the API has IJ.pad() function but I don't think this would work.
>
> Does anyone have any ideas of an easy way/ suggestions on how to do this?

You can pad an image with zeros using the Image>Adjust>Canvas Size command.

Here is an example macro that creates a 512x512 image and pads it on all sides by 10 pixels:

  newImage("Untitled", "8-bit white", 512, 512, 1);
  run("Canvas Size...", "width=532 height=532 position=Center zero");

Here is a version of the macro that uses variables for the image size and the padding:

  size = 512;
  pad = 10;
  newImage("Untitled", "8-bit white", size, size, 1);
  size2 = size + pad*2;
  run("Canvas Size...", "width=&size2 height=&size2 position=Center zero");

Here is a JavaScript version that opens a 512x512 image and pads by 10:

  imp = IJ.createImage("Untitled", "8-bit white", 512, 512, 1);
  IJ.run(imp, "Canvas Size...", "width=532 height=532 position=Center zero");
  imp.show();

And here is a version of the script that uses variables:

  size = 512;
  pad = 10;
  imp = IJ.createImage("Untitled", "8-bit white", size, size, 1);
  size2 = size + pad*2;
  IJ.run(imp, "Canvas Size...", "width="+size2+" height="+size2+" position=Center zero");
  imp.show();

-wayne

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html