ImageJ base code to get access to all the classes and their methods to test new Plugins. https://imagejdocu.tudor.lu/howto/plugins/the_imagej_eclipse_howto
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

448 lines
12 KiB

3 years ago
  1. //--version--1.6
  2. // 1.6 adds the '?' button that points to the wiki page
  3. // panel labels are now drawn on an overlay
  4. // added overlay commands and copy to system clipboard to the rightclick menu
  5. // Montage tools for easy montage manipulation
  6. // jerome.mutterer at ibmp.fr
  7. var str="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  8. var lcas=false;
  9. var antialiasedLabels = true;
  10. var n=0;
  11. var xoffset=0.05;
  12. var yoffset=0.05;
  13. var pos="Clicked quadrant";
  14. var commands = newArray("Copy", "Paste","-", "Scale Bar...",
  15. "Brightness/Contrast...", "-", "Extract Selected Panels","Crop Montage [F3]","-",
  16. "Add Panel to Manager [F1]","Selected panels to stack [F2]", "Montage to Stack", "-",
  17. "Fit Clipboard content into panel [F4]","Fill Panel with Clipboard content [F5]","-",
  18. "Set Montage Layout","Change Montage Layout","-",
  19. "Hide Overlay","Show Overlay", "Remove Overlay", "Flatten", "-",
  20. "Copy to System"));
  21. var toolCmds = newMenu("Magic Montage Menu Tool",commands);
  22. var pmCmds = newMenu("Popup Menu",commands);
  23. macro "Popup Menu" {
  24. runCommand();
  25. }
  26. macro "Auto Montage Action Tool - C66fF0077C6f6F9977Cf66F9077C888F0977" {
  27. setBatchMode(true);
  28. b=bitDepth;
  29. if ((b!=24)&&(nSlices==1)) { exit("Stack, Composite, or RGB image required.");}
  30. if ((b==24)&&(nSlices==1)) { run("Make Composite"); b=8;}
  31. Stack.getDimensions(width, height, channels, slices, frames);
  32. getVoxelSize(xp,yp,zp,unit);
  33. if (channels==1) { channels = channels* frames*slices; Stack.setDimensions(channels,1,1); }
  34. id=getImageID;
  35. t=getTitle;
  36. if (b!=24) {
  37. newImage("tempmont", "RGB", width, height,channels);
  38. id2=getImageID;
  39. for (i=1;i<=channels;i++) {
  40. setPasteMode("copy");
  41. selectImage(id);
  42. Stack.setChannel(i);
  43. getLut(r,g,b);
  44. run("Duplicate...", "title=temp"+i);
  45. setLut(r,g,b);
  46. run("RGB Color");
  47. run("Copy");
  48. selectImage(id2);
  49. setSlice(i);
  50. run("Paste");
  51. }
  52. }
  53. run("Make Montage...", "scale=1 border=0");
  54. rename(getTitle+" of "+t);
  55. setVoxelSize(xp,yp,zp,unit);
  56. setBatchMode(false);
  57. }
  58. macro "Select Panels Tool - Cf44R0077R9077C888R9977R0977"{
  59. run("Select None");
  60. setPasteMode("copy");
  61. w = getWidth;
  62. h = getHeight;
  63. getCursorLoc(x, y, z, flags);
  64. id=getImageID;
  65. t=getTitle;
  66. selectImage(id);
  67. xn = info("xMontage");
  68. yn = info("yMontage");
  69. if ((xn==0)||(yn==0))
  70. exit;
  71. xc = floor(x/(w/xn));
  72. yc = floor(y/(h/yn));
  73. makeRectangle(xc*(w/xn),yc*(h/yn),(w/xn),(h/yn));
  74. xstart = x; ystart = y;
  75. x2=x; y2=y;
  76. x2c=xc;y2c=yc;
  77. while (flags&16 !=0) {
  78. getCursorLoc(x, y, z, flags);
  79. if (x!=x2 || y!=y2) {
  80. x2c = floor(x/(w/xn));
  81. y2c = floor(y/(h/yn));
  82. makeRectangle(xc*(w/xn),yc*(h/yn),(w/xn)*(x2c-xc+1),(h/yn)*(y2c-yc+1));
  83. x2=x; y2=y;
  84. wait(10);
  85. }
  86. }
  87. setPasteMode("add");
  88. }
  89. macro "Extract Selected Panels"{
  90. t=getTitle;
  91. xn = info("xMontage");
  92. yn = info("yMontage");
  93. pw = getWidth/xn;
  94. ph = getHeight/yn;
  95. run("Duplicate...", "title=[Extract of "+t+"]");
  96. setMetadata("Info","xMontage="+getWidth/pw+"\nyMontage="+getHeight/ph+"\n");
  97. }
  98. macro "Montage Shuffler Tool - C888R0077R9977C44fR0977R9077"{
  99. id=getImageID;
  100. run("Select None");
  101. setPasteMode("copy");
  102. w = getWidth;
  103. h = getHeight;
  104. getCursorLoc(x, y, z, flags);
  105. xn = info("xMontage");
  106. yn = info("yMontage");
  107. if ((xn==0)||(yn==0))
  108. exit;
  109. xstart = x; ystart = y;
  110. x2=x; y2=y;
  111. while (flags&16 !=0) {
  112. getCursorLoc(x, y, z, flags);
  113. if (x!=x2 || y!=y2) spring(xstart, ystart, x, y);
  114. x2=x; y2=y;
  115. wait(10);
  116. }
  117. if (x!=xstart || y!=ystart) {
  118. xext=0;
  119. yext=0;
  120. if (x>w) xext=1;
  121. if (y>h) yext=1;
  122. if ((xext>0)||(yext>0)) {
  123. run("Canvas Size...", "width="+w+xext*(w/xn)+" height="+h+yext*(h/yn)+" position=Top-Left zero");
  124. setMetadata("Info","xMontage="+(parseInt(xn)+parseInt(xext))+"\nyMontage="+(parseInt(yn)+parseInt(yext))+"\n");
  125. exit;
  126. }
  127. sc = floor(xstart/(w/xn));
  128. tc = floor(x/(w/xn));
  129. sr = floor(ystart/(h/yn));
  130. tr = floor(y/(h/yn));
  131. swap(sc,sr,tc,tr);
  132. }
  133. }
  134. macro "Annotation Tool - C700 T2709A T8709B T1f09C T8f09D" {
  135. xn = info("xMontage");
  136. yn = info("yMontage");
  137. getCursorLoc(x, y, z, flags);
  138. iw = getWidth/xn;
  139. ih = getHeight/yn;
  140. co = floor(x/iw);
  141. li = floor(y/ih);
  142. fontsize = ih/10;
  143. if (fontsize<12) fontsize=12;
  144. marque = substring(str,n,n+1);
  145. if (lcas==1) marque= toLowerCase(marque);
  146. opt="";
  147. if (pos == "Clicked quadrant") {
  148. xoffset=0.05; yoffset=0.05;
  149. if (x>((co+0.5)*iw)) xoffset=0.90;
  150. if (y<((li+0.5)*ih)) yoffset=0.85;
  151. }
  152. if (antialiasedLabels==true) opt=opt+"antialiased";
  153. setFont("SanSerif",fontsize, opt);
  154. fg = getValue("rgb.foreground");
  155. makeText(marque ,co*iw+xoffset*iw,(li+1)*ih-yoffset*ih-getValue("font.height"));
  156. Roi.setStrokeColor(fg&0xff0000>>16,fg&0x00ff00>>8,fg&0x0000ff);
  157. Overlay.addSelection("",0);
  158. run("Select None");
  159. n++; if (n>lengthOf(str)) n=0;
  160. }
  161. macro "Annotation Tool Options" {
  162. if (nImages>0) setupUndo;
  163. Dialog.create("Annotation - Options");
  164. Dialog.addString("Labels",str);
  165. Dialog.addCheckbox("Lowercase labels",lcas);
  166. Dialog.addCheckbox("Reset label counter",true);
  167. Dialog.addCheckbox("Antialiased",true);
  168. Dialog.addChoice("Position",newArray("Clicked quadrant","Lower left","Lower right","Upper right","Upper left"),pos);
  169. Dialog.show;
  170. str = Dialog.getString;
  171. lcas = Dialog.getCheckbox;
  172. resetCounter = Dialog.getCheckbox;
  173. if (resetCounter==true) n=0;
  174. antialiasedLabels = Dialog.getCheckbox;
  175. pos=Dialog.getChoice();
  176. if (pos=="Lower left") {xoffset=0.05; yoffset=0.05;}
  177. else if (pos=="Lower right") {xoffset=0.90; yoffset=0.05;}
  178. else if (pos=="Upper left") {xoffset=0.05; yoffset=0.85;}
  179. else if (pos=="Upper right") {xoffset=0.90; yoffset=0.85;}
  180. }
  181. macro "Montage Sync Tool - C800L07f7L707fG" {
  182. w=getWidth;
  183. h= getHeight;
  184. getCursorLoc(x,y,z,flags);
  185. xn = info("xMontage");
  186. yn = info("yMontage");
  187. if ((xn==0)||(yn==0)) {
  188. run("Set Montage Layout");
  189. exit;
  190. }
  191. xc = floor(x/(w/xn));
  192. yc = floor(y/(h/yn));
  193. x0 = x-xc*w/xn;
  194. y0 = y-yc*h/yn;
  195. np = 1*xn*yn;
  196. xp =newArray(np);
  197. yp =newArray(np);
  198. for (i=0;i<xn;i++) {
  199. for (j=0;j<yn;j++) {
  200. xp[j*xn+i] = x0+i*(w/xn);
  201. yp[j*xn+i] = y0+j*(h/yn);
  202. }
  203. }
  204. makeSelection("point yellow large hybred",xp,yp);
  205. }
  206. macro "Magic Montage Menu Tool - C800G027ff200" {
  207. runCommand();
  208. }
  209. function runCommand() {
  210. cmd = getArgument();
  211. if (cmd=="Scale Bar...")
  212. doCommand(cmd);
  213. else
  214. run(getArgument());
  215. }
  216. macro "Magic Montage Help Action Tool - C911T4e18?"{
  217. run("URL...", "url=http://wsr.imagej.net/macros/toolsets/MagicMontage.pdf");
  218. //run("URL...", "url=http://imagejdocu.tudor.lu/doku.php?id=howto:working:work_with_magic_montage");
  219. }
  220. // Returns numeric value associated with 'key' or 0 if not found.
  221. function info(key) {
  222. value = getInfo(key);
  223. if (value=="")
  224. return 0;
  225. else
  226. return parseInt(value);
  227. }
  228. function swap(a,b,c,d) {
  229. setupUndo;
  230. setBatchMode(true);
  231. makeRectangle(a*(w/xn),b*(h/yn),(w/xn),(h/yn));
  232. run("Duplicate...", "title=tmp");
  233. selectImage(id);
  234. makeRectangle(c*(w/xn),d*(h/yn),(w/xn),(h/yn));
  235. run("Copy");
  236. makeRectangle(a*(w/xn),b*(h/yn),(w/xn),(h/yn));
  237. run("Paste");
  238. selectWindow("tmp");
  239. run("Select All");
  240. run("Copy");
  241. selectImage(id);
  242. makeRectangle(c*(w/xn),d*(h/yn),(w/xn),(h/yn));
  243. run("Paste");
  244. run("Select None");
  245. setBatchMode(false);
  246. }
  247. function spring(x0,y0,x1,y1) {
  248. d = sqrt((y1-y0)*(y1-y0)+(x1-x0)*(x1-x0));
  249. step=3;
  250. r=15;
  251. xa = newArray(floor(d/step));
  252. ya = newArray(xa.length);
  253. for (i=0;i<xa.length;i++) {
  254. j=i*step;
  255. xa[i]=x0+j*(x1-x0)/d+sin(j/7)*r;
  256. ya[i]=y0+j*(y1-y0)/d+cos(j/7)*r;
  257. }
  258. if (xa.length>1) {
  259. xa[0]=x0;
  260. ya[0]=y0;
  261. xa[xa.length-1]=x1;
  262. ya[ya.length-1]=y1;
  263. }
  264. makeSelection("freeline",xa,ya);
  265. }
  266. macro "Add Panel to Manager [F1]" {
  267. roiManager("Add");
  268. setOption("Show All",true);
  269. }
  270. macro "Montage to Stack" {
  271. columns = info("xMontage");
  272. rows = info("yMontage");
  273. if (rows==0 || columns==0) {
  274. run("Set Montage Layout");
  275. columns = info("xMontage");
  276. rows = info("yMontage");
  277. }
  278. run("Montage to Stack...", "columns=&columns rows=&rows");
  279. }
  280. macro "Selected panels to stack [F2]" {
  281. id=getImageID;
  282. t=getTitle;
  283. selectImage(id);
  284. roiManager("select",0);
  285. getSelectionBounds(x,y,sw,sh);
  286. setBatchMode(true);
  287. newImage("Extracted Panels of "+t, "RGB", sw,sh,roiManager("count"));
  288. id2=getImageID;
  289. setPasteMode("copy");
  290. for (i=0;i<roiManager("count");i++) {
  291. selectImage(id);
  292. roiManager("select",i);
  293. run("Copy");
  294. selectImage(id2);
  295. setSlice(i+1);
  296. run("Paste");
  297. }
  298. setBatchMode(false);
  299. }
  300. macro "Crop Montage [F3]" {
  301. setBatchMode(true);
  302. setPasteMode("copy");
  303. w=getWidth;
  304. h= getHeight;
  305. b=bitDepth;
  306. getSelectionBounds(x,y,sw,sh);
  307. t=getTitle;
  308. id=getImageID;
  309. getVoxelSize(xp,yp,zp,unit);
  310. xn = info("xMontage");
  311. yn = info("yMontage");
  312. xc = floor(x/(w/xn));
  313. yc = floor(y/(h/yn));
  314. xpa = x-xc*(w/xn);
  315. ypa= y-yc*(h/yn);
  316. newImage("Crop of "+t,b+"RGB",sw,sh,(xn)*(yn));
  317. id2=getImageID;
  318. for (j=0;j<yn;j++) {
  319. for (i=0;i<xn;i++) {
  320. selectImage(id);
  321. makeRectangle(i*(w/xn)+xpa,j*(h/yn)+ypa,sw,sh);
  322. run("Copy");
  323. selectImage(id2);
  324. setSlice(j*(xn)+i+1);
  325. run("Paste");
  326. }
  327. }
  328. setVoxelSize(xp,yp,zp,unit);
  329. setBatchMode(false);
  330. setSlice(nSlices);
  331. setSlice(1);
  332. }
  333. macro "Fit Clipboard content into panel [F4]" {
  334. getSelectionBounds(x,y,sw,sh);
  335. id=getImageID;
  336. setBatchMode(true);
  337. ffp=sw/sh;
  338. run("Internal Clipboard");
  339. run("RGB Color");
  340. ffc=getWidth/getHeight;
  341. if (ffc>ffp) {
  342. run("Size...", "width="+sw+" height="+sw/ffc+" constrain interpolate");
  343. run("Canvas Size...", "width="+sw+" height="+sh+" position=Center zero");
  344. } else {
  345. run("Size...", "width="+sh*ffc+" height="+sh+" constrain interpolate");
  346. run("Canvas Size...", "width="+sw+" height="+sh+" position=Center zero");
  347. }
  348. run("Copy");
  349. close;
  350. selectImage(id);
  351. setBatchMode(false);
  352. setPasteMode("Copy");
  353. run("Paste");
  354. }
  355. macro "Fill Panel with Clipboard content [F5]" {
  356. getSelectionBounds(x,y,sw,sh);
  357. id=getImageID;
  358. setBatchMode(true);
  359. ffp=sw/sh;
  360. run("Internal Clipboard");
  361. run("RGB Color");
  362. ffc=getWidth/getHeight;
  363. if (ffc>ffp) {
  364. run("Size...", "width="+sw*ffc+" height="+sh+" constrain interpolate");
  365. run("Canvas Size...", "width="+sw+" height="+sh+" position=Center zero");
  366. } else {
  367. run("Size...", "width="+sw+" height="+sh/ffc+" constrain interpolate");
  368. run("Canvas Size...", "width="+sw+" height="+sh+" position=Center zero");
  369. }
  370. run("Copy");
  371. close;
  372. selectImage(id);
  373. setBatchMode(false);
  374. setPasteMode("Copy");
  375. run("Paste");
  376. }
  377. macro "Set Montage Layout" {
  378. columns = info("xMontage");
  379. rows = info("yMontage");
  380. if (columns>0 && rows>0)
  381. exit("Layout ("+columns+"x"+rows+") is already set");
  382. Dialog.create("Set Montage Layout");
  383. Dialog.addNumber("Width:", 2);
  384. Dialog.addNumber("Height:", 2);
  385. Dialog.show;
  386. mw = Dialog.getNumber;
  387. mh = Dialog.getNumber;
  388. setMetadata("Info","xMontage="+mw+"\nyMontage="+mh+"\n");
  389. }
  390. macro "Change Montage Layout" {
  391. columns = info("xMontage");
  392. rows = info("yMontage");
  393. if (rows==0 || columns==0) {
  394. run("Set Montage Layout");
  395. columns = info("xMontage");
  396. rows = info("yMontage");
  397. }
  398. id1 = getImageID;
  399. title = getTitle;
  400. getVoxelSize(xp,yp,zp,unit);
  401. Dialog.create("Change Montage Layout");
  402. Dialog.addNumber("Columns:", columns);
  403. Dialog.addNumber("Rows:", rows);
  404. Dialog.show;
  405. columns2 = Dialog.getNumber;
  406. rows2 = Dialog.getNumber;
  407. run("Montage to Stack...", "columns=&columns rows=&rows");
  408. id2 = getImageID;
  409. run("Make Montage...", "columns=&columns2 rows=&rows2 scale=1.0");
  410. rename(title);
  411. setVoxelSize(xp,yp,zp,unit);
  412. selectImage(id1); close;
  413. selectImage(id2); close;
  414. }