Login  Register

Re: Cyclic translation transform

Posted by jerry novakk on Jul 23, 2015; 8:15am
URL: http://imagej.273.s1.nabble.com/Cyclic-translation-transform-tp5013479p5013713.html

Here are three versions of macros for translations, wich I made from your propositions. The code is a little bit trashy and definitely not optimized, but it works fine for me. I had problems with translations with negative input so I had to combine makeRectangle.. and Specify.. to paste back out of bound parts of an image.

I thank you both again for your help and hope someone else finds this usefull.

--------------------------------------------------------------------------------------
//Periodic_Translation_TopLeft.txt

  getDimensions(W, H, dummy, dummy, dummy);

//Open your image and make an ROI of the area you want to translate to top left corner.

  waitForUser ("select area");
  getSelectionBounds(X, Y, dummy, dummy);

  Xc=W-X;
  Xm=-X;
  Yc=H-Y;
  Ym=-Y;

//translate horizontal

  run("Select All");
  run("Specify...", "Width=X Height=H x=0 y=0");
  run("Copy");
  run("Select None");
  run("Translate...", "x=Xm y=0 interpolation=None");
  run("Specify...", "Width=X Height=H x=Xc y=0");
  run("Paste");

//translate vertical

  run("Select All");
  run("Specify...", "Width=W Height=Y x=0 y=0");
  run("Copy");
  run("Select None");
  run("Translate...", "x=0 y=Ym interpolation=None");
  run("Specify...", "Width=W Height=H x=0 y=Yc");
  run("Paste");
  run("Select None");
-----------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------
//Periodic_Translation_Center.txt

  getDimensions(W, H, dummy, dummy, dummy);

//Open your image and make an ROI of the area you want to center translate.

  waitForUser ("select area to center");
  getSelectionBounds(X, Y, width, height);

  a=round((W-width)*0.5);
  b=round((H-height)*0.5);

//translate horizontal
 
  if (a<=X){
    dx=X-a;
    Xcopy= W-dx;
    run("Select All");
    run("Specify...", "Width="+dx+" Height="+H+" x=0 y=0");
    run("Copy");
    run("Select None");
    run("Translate...", "x=-"+dx+" y=0 interpolation=None");
//    makeRectangle(Xcopy, 0, dx, H);
    run("Specify...", "Width=dx Height=H x=Xcopy y=0");
    run("Paste");
  } else {
    dx=a-X;
    Xcopy= W-dx;
    run("Select All");
    run("Specify...", "Width="+dx+" Height="+H+" x="+Xcopy+" y=0");
    run("Copy");
    run("Select None");
    run("Translate...", "x="+dx+" y=0 interpolation=None");
    makeRectangle(0, 0, dx, H);
    run("Paste");
  }

//translate vertical

  if (b<=Y) {
    dy=Y-b;
    ycopy= H-dy;
    run("Select All");
    run("Specify...", "Width="+W+" Height="+dy+" x=0 y=0");
    run("Copy");
    run("Select None");
    run("Translate...", "x=0 y=-"+dy+" interpolation=None");
    run("Specify...", "Width=W Height=dy x=0 y=ycopy");
    run("Paste");
  } else {
    dy=b-Y;
    ycopy= H-dy;
    run("Select All");
    run("Specify...", "Width="+W+" Height="+dy+" x=0 y="+ycopy+"");
    run("Copy");
    run("Select None");
    run("Translate...", "x=0 y="+dy+" interpolation=None");
    makeRectangle(0, 0, W, dy);
    run("Paste");
  }

    run("Select None");

-----------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------
//Periodic_Translation_Input.txt

  getDimensions(W, H, dummy, dummy, dummy);

//translate horizontal

  Dialog.create("Translate horizontal");
  Dialog.addNumber("X", 0);
  Dialog.show();
  X= Dialog.getNumber();
 
  if (X<0) {
    Xa=W+X;
    run("Select All");
    run("Specify...", "Width=abs(X) Height=H x=0 y=0");
    run("Copy");
    run("Select None");
    run("Translate...", "x=X y=0 interpolation=None");
    run("Specify...", "Width=abs(X) Height=H x=Xa y=0");
    run("Paste");
  }
  else {
    Xb=W-X;
    run("Select All");
    run("Specify...", "Width=abs(X) Height=H x=Xb y=0");
    run("Copy");
    run("Select None");
    run("Translate...", "x=X y=0 interpolation=None");
    makeRectangle(0, 0, abs(X), H);
    run("Paste");
  }


//translate vertical

  Dialog.create("Translate vertical");
  Dialog.addNumber("Y", 0);
  Dialog.show();
  Y= Dialog.getNumber();

  if (Y<0) {
    Ya=H+Y;
    run("Select All");
    run("Specify...", "Width=W Height=abs(Y) x=0 y=0");
    run("Copy");
    run("Select None");
    run("Translate...", "x=0 y=Y interpolation=None");
    run("Specify...", "Width=W Height=abs(Y) x=0 y=Ya");
    run("Paste");
  }
  else {
    Yb=H-Y;
    run("Select All");
    run("Specify...", "Width=W Height=abs(Y) x=0 y=Yb");
    run("Copy");
    run("Select None");
    run("Translate...", "x=0 y=Y interpolation=None");
    makeRectangle(0, 0, W, abs(Y));
    run("Paste");
  }
 
  run("Select None");
----------------------------------------------------------------------------------