2015年10月25日

用imagej的Macro量測線段的垂直線

五年前一個研究生網友寫信來問這樣的問題,他想要用imagej針對某一線段畫垂直線,不過當初我沒有找到方便的方式,只能跟他說就自己找直角,畫一條垂直線。(用angle tool畫90度的線,然後用Edit/draw去畫線)

最近開始用Macro,就去翻以前的信件,看到網友的這個問題,於是想著想著就把code寫出來。不過我不太好意思跟那網友說,欸,可以這樣作囉。

畢竟都是那麼久了,五年前是研究生,如果現在還需要這個功能的話,那......。

老樣子,底下影片是操作方式,最底下有code,複製貼上就可以。




macro "DrawVertical" {

   requires("1.37rk");

   if(selectionType!=5) showMessage("Selection Must be staight Line");


   Dialog.create("Setting");
   Dialog.addNumber("line Width:", 10);
   Dialog.addChoice("Base Line Color:", newArray("cyan", "red", "green", "magenta", "blue", "yellow", "orange", "black", "white"),"yellow");
   Dialog.addChoice("Vertical Line Color:", newArray("cyan", "red", "green", "magenta", "blue", "yellow", "orange", "black", "white"),"red");
   Dialog.show();

   LineWid=Dialog.getNumber();
   Bcolor = Dialog.getChoice();
   Vcolor = Dialog.getChoice();

   getPixelSize(scale, pixelSize, trash);

   getSelectionCoordinates(x, y);

   setColor(Bcolor);
   setLineWidth(LineWid);
   drawLine(x[0],y[0], x[1], y[1]);


   leftButton=16;
   x2=-1;
   y2=-1;
   z2=-1;
   flags2=-1;
   logOpened = false;

   print("start");


   while (!logOpened || isOpen("Log")) {
       getCursorLoc(seleX, seleY, z, flags);

       if (x!=x2 || y!=y2 || z!=z2 || flags!=flags2) {
           s = " ";

           if (flags&leftButton!=0) {
               s = "<left>";



               m1=(y[1]-y[0])/(x[1]-x[0]);
               m2=-1/m1;
               interX=(m2*seleX-m1*x[1]+y[1]-seleY)/(m2-m1);
               interY=m2*(interX-seleX)+seleY;

               setColor(Vcolor);
               setLineWidth(LineWid);
               moveTo(seleX,seleY);
               lineTo(interX,interY);

               lineLeng=pow((pow((interX-seleX),2)+pow((interY-seleY),2)),1/2);
               lineLeng=lineLeng*pixelSize;
               print("Length:","\t", lineLeng,"\t",scale);


               logOpened = true;
               startTime = getTime();

           }
       }
       x2=x;
       y2=y;
       z2=z;
       flags2=flags;
       wait(10);
   }

}

你可能對這有興趣

Related Posts Plugin for WordPress, Blogger...