PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src-plugins/mij/MIJ.java

https://github.com/hgrecco/fiji
Java | 1075 lines | 797 code | 58 blank | 220 comment | 132 complexity | 359a0e394854a3a5a555c59c3fd2de5c MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1, GPL-2.0, GPL-3.0, CC-BY-SA-3.0
  1. import java.awt.Rectangle;
  2. import java.awt.Polygon;
  3. import java.util.StringTokenizer;
  4. import ij.IJ;
  5. import ij.ImageJ;
  6. import ij.WindowManager;
  7. import ij.ImagePlus;
  8. import ij.ImageStack;
  9. import ij.gui.Line;
  10. import ij.gui.PointRoi;
  11. import ij.gui.OvalRoi;
  12. import ij.gui.PolygonRoi;
  13. import ij.gui.Roi;
  14. import ij.measure.Calibration;
  15. import ij.measure.ResultsTable;
  16. import ij.process.ByteProcessor;
  17. import ij.process.ColorProcessor;
  18. import ij.process.FloatProcessor;
  19. import ij.process.ShortProcessor;
  20. import ij.plugin.filter.Analyzer;
  21. /**
  22. * Matlab to ImageJ interface.
  23. *
  24. * @version 1.3.2 (27 September 2009).
  25. * @author Daniel Sage, Biomedical Imageing Group (BIG), Ecole Polytechnique Federale de Lausanne (EPFL), Switzerland.
  26. * @author Dimiter Prodanov, Catholic University of Louvain, Belgium, http://www.neuromorf.com.
  27. *
  28. * <p>
  29. * <b>More information:</b> <a href="http://bigwww.epfl.ch/sage/soft/mij/">http://bigwww.epfl.ch/sage/soft/mij/</a>
  30. * </p>
  31. *
  32. * <p>
  33. * <b>Important note for the installation:</b><br>
  34. * This class was tested with the Matlab 7.9.0 (R2009b) on Windows XP.
  35. * To use this class, the java classpath of Matlab should be included ij.jar (ImageJ) and mij.jar (MIJ) by:<br>
  36. * javaaddpath 'C:\Program Files\MATLAB\R2009b\java\ij.jar'<br>
  37. * javaaddpath 'C:\Program Files\MATLAB\R2009b\java\mij.jar'<br>
  38. * </p>
  39. * <p>
  40. * <b>Conditions of use:</b><br>
  41. * You'll be free to use this software for research purposes, but you
  42. * should not redistribute it without our consent. In addition, we
  43. * expect you to include a citation or acknowledgement whenever
  44. * you present or publish results that are based on it.
  45. * </o>
  46. *
  47. *
  48. */
  49. public class MIJ {
  50. public static ImageJ imagej;
  51. private static final String version = "1.3.3";
  52. private static final int CAL = 1;
  53. private static final int NOCAL = 0;
  54. private static boolean verbose = true;
  55. /**
  56. * Class constructor.
  57. */
  58. public MIJ() {
  59. }
  60. /**
  61. * Get the version of the MIJ class.
  62. *
  63. * @return Returns the version number.
  64. */
  65. public static String version() {
  66. return version;
  67. }
  68. /**
  69. * Give a brief description of the methods.
  70. *
  71. * @return Returns a brief description of the methods.
  72. */
  73. public static String help() {
  74. String str = "Supported methods for image manipulation in version " + version + "\n"+
  75. "createColor - exports RGB image \n"+
  76. "createImage - exports other images \n"+
  77. "exit -exists MIJ \n"+
  78. "getColumn - returns a specifying column the current instance of ResultsTable \n" +
  79. "getCurrentImage - returns a 2D array representing the current image \n"+
  80. "getCurrentTitle - imports the title of the current image \n"+
  81. "getHistogram - imports the histogram of the current image \n"+
  82. "getImage - returns a 2D array representing the image specified by the title\n"+
  83. "getListColumns - returns the list of columns currently used in the ResultsTable \n"+
  84. "getListImages - returns the list of opened images \n"+
  85. "getResultsTable - imports the ResultsTable \n"+
  86. "getRoi - imports the current ROI \n"+
  87. "help - give a brief description of the MIJ methods \n"+
  88. "run - runs command or macro \n" +
  89. "setColumn - exports contents to a column in the ResultsTable\n"+
  90. "setRoi - exports the current ROI \n"+
  91. "setThreshold - sets the threshods of the image \n"+
  92. "start - starts MIJ \n"+
  93. "version - return the MIJ version\n";
  94. return str;
  95. }
  96. /**
  97. * Starts new instance of ImageJ from Matlab.
  98. */
  99. public static void start() {
  100. verbose = true;
  101. launch();
  102. }
  103. /**
  104. * Starts new instance of ImageJ from Matlab with or without verbose mode.
  105. *
  106. * @param v indicate the verbose mode
  107. */
  108. public static void start(boolean v) {
  109. verbose = v;
  110. launch();
  111. }
  112. /**
  113. * Starts new instance of ImageJ specifying the plugins directory.
  114. *
  115. * @param string Location of the plugins
  116. */
  117. public static void start(String string) {
  118. System.setProperty("plugins.dir", string);
  119. verbose = true;
  120. launch();
  121. }
  122. /**
  123. * Starts new instance of ImageJ specifying the plugins directory.
  124. *
  125. * @param string Location of the plugins
  126. * @param v indicate the verbose mode
  127. */
  128. public static void start(String string, boolean v) {
  129. System.setProperty("plugins.dir", string);
  130. verbose = v;
  131. launch();
  132. }
  133. /**
  134. * Starts new instance of ImageJ from Matlab.
  135. */
  136. private static void launch() {
  137. if (verbose) {
  138. System.out.println("--------------------------------------------------------------");
  139. System.out.println("MIJ " + version + ": Matlab to ImageJ Interface");
  140. System.out.println("--------------------------------------------------------------");
  141. System.out.println("More Info: http://bigwww.epfl.ch/sage/soft/mij/");
  142. System.out.println("Help: MIJ.help");
  143. Runtime runtime = Runtime.getRuntime();
  144. System.out.println("JVM> " + version);
  145. System.out.println("JVM> Version: " + System.getProperty("java.version"));
  146. System.out.println("JVM> Total amount of memory: " + Math.round(runtime.totalMemory()/1024) + " Kb");
  147. System.out.println("JVM> Amount of free memory: " + Math.round(runtime.freeMemory()/1024) + " Kb");
  148. }
  149. if (imagej instanceof ImageJ) {
  150. if (verbose) {
  151. System.out.println("--------------------------------------------------------------");
  152. System.out.println("Status> ImageJ is already started.");
  153. System.out.println("--------------------------------------------------------------");
  154. }
  155. return;
  156. }
  157. imagej = new ImageJ();
  158. if (imagej instanceof ImageJ) {
  159. if (verbose) {
  160. System.out.println("ImageJ> Version:" + IJ.getVersion());
  161. System.out.println("ImageJ> Memory:" + IJ.freeMemory() );
  162. System.out.println("ImageJ> Directory plugins: "+ (IJ.getDirectory("plugins")==null?"Not specified":IJ.getDirectory("plugins")));
  163. System.out.println("ImageJ> Directory macros: " + (IJ.getDirectory("macros")==null?"Not specified":IJ.getDirectory("macros")));
  164. System.out.println("--------------------------------------------------------------");
  165. System.out.println("Status> ImageJ is running.");
  166. System.out.println("--------------------------------------------------------------");
  167. }
  168. return;
  169. }
  170. if (verbose) {
  171. System.out.println("--------------------------------------------------------------");
  172. System.out.println("Status> ImageJ can not be started.");
  173. System.out.println("--------------------------------------------------------------");
  174. }
  175. }
  176. /**
  177. * Starts new instance of ImageJ.
  178. *
  179. * @param args Start up arguments for MIJ
  180. */
  181. public static void main(String [] args) {
  182. try {
  183. MIJ.start(args[0]);
  184. } catch (ArrayIndexOutOfBoundsException e) {
  185. IJ.log(MIJ.help());
  186. }
  187. }
  188. /**
  189. * Exits ImageJ.
  190. */
  191. public static void exit() {
  192. //System.exit(0);
  193. //this.imagej.quit();
  194. IJ.getInstance().quit();
  195. }
  196. /**
  197. * Gives the list of the open images in the ImageJ instance.
  198. *
  199. * @return List of open images
  200. */
  201. public static String[] getListImages() {
  202. int[] is = WindowManager.getIDList();
  203. String[] strings = new String[is.length];
  204. for (int i = 0; i < is.length; i++) {
  205. ImagePlus imageplus = WindowManager.getImage(is[i]);
  206. strings[i] = imageplus.getTitle();
  207. }
  208. return strings;
  209. }
  210. /**
  211. * Returns the title of the current image window.
  212. *
  213. * @return Title of the current image window
  214. */
  215. public static String getCurrentTitle() {
  216. ImagePlus imageplus = WindowManager.getCurrentImage();
  217. return imageplus.getTitle();
  218. }
  219. /**
  220. * Set a region of interest (ROI) in the current image.
  221. *
  222. * @param roiarray give coordinates or positions of the ROI depending of the ROI type
  223. * @param type supported types: Roi.LINE, Roi.RECTANGLE, Roi.POINT, Roi.OVAL, Roi.POLYLINE, Roi.POLYGON, Roi.ANGLE
  224. */
  225. public static void setRoi(double[][] roiarray, int type) {
  226. ImagePlus imageplus = WindowManager.getCurrentImage();
  227. switch (type) {
  228. case Roi.LINE:
  229. Line roi=new Line((int) roiarray[0][0], (int) roiarray[1][0],
  230. (int) roiarray[0][1], (int) roiarray[1][1]);
  231. imageplus.setRoi((Roi)roi);
  232. break;
  233. case Roi.RECTANGLE:
  234. int width= (int) roiarray[0][0]-(int) roiarray[0][1];
  235. int height= (int) roiarray[1][1]-(int) roiarray[1][2];
  236. IJ.log("w: "+width+ " h: "+height);
  237. Roi rect=new Roi((int) roiarray[0][0], (int) roiarray[1][0], Math.abs(width), Math.abs(height));
  238. imageplus.setRoi(rect);
  239. break;
  240. case Roi.POINT:
  241. PointRoi pnt=new PointRoi((int) roiarray[0][0], (int) roiarray[1][0], imageplus);
  242. imageplus.setRoi(pnt);
  243. break;
  244. case Roi.OVAL:
  245. width= (int) roiarray[0][0]-(int) roiarray[0][1];
  246. height= (int) roiarray[1][1]-(int) roiarray[1][2];
  247. int xc= (int) roiarray[0][0] + width/2;
  248. int yc= (int) roiarray[1][1] + height/2;
  249. IJ.log("w: "+width+ " h: "+height+ " xc: "+xc+" yc "+yc);
  250. OvalRoi oval=new OvalRoi( xc, yc, width, height);
  251. imageplus.setRoi(oval);
  252. break;
  253. case Roi.POLYLINE:
  254. int nPoints=roiarray[0].length;
  255. int[] xarr=new int[nPoints];
  256. int[] yarr=new int[nPoints];
  257. for (int i=0;i<nPoints;i++) {
  258. xarr[i]=(int)roiarray[0][i];
  259. yarr[i]=(int)roiarray[1][i];
  260. }
  261. PolygonRoi poly=new PolygonRoi(xarr, yarr, nPoints, Roi.POLYLINE);
  262. imageplus.setRoi(poly);
  263. break;
  264. case Roi.POLYGON:
  265. nPoints=roiarray[0].length;
  266. xarr=new int[nPoints];
  267. yarr=new int[nPoints];
  268. for (int i=0;i<nPoints;i++) {
  269. xarr[i]=(int)roiarray[0][i];
  270. yarr[i]=(int)roiarray[1][i];
  271. }
  272. poly=new PolygonRoi(xarr, yarr, nPoints, Roi.POLYGON);
  273. imageplus.setRoi(poly);
  274. break;
  275. case Roi.ANGLE:
  276. break;
  277. default:
  278. }
  279. }
  280. /**
  281. * Get a region of interest (ROI) of the current image with or without calibration.
  282. *
  283. * @param option CAL for using calibration or NOCAL for no calibration
  284. * @return Object
  285. */
  286. public static Object getRoi(int option) {
  287. ImagePlus imageplus = WindowManager.getCurrentImage();
  288. Roi roi=imageplus.getRoi();
  289. Calibration cal=imageplus.getCalibration();
  290. double fh=cal.pixelHeight;
  291. double fw=cal.pixelWidth;
  292. Object ret=null;
  293. if (roi.isLine()) {
  294. Rectangle rect=roi.getBounds();
  295. double x=rect.getX();
  296. double y=rect.getY();
  297. double w=rect.getWidth();
  298. double h=rect.getHeight();
  299. if (option==NOCAL){
  300. double [][] pnts= {{x,x+w,x+w,x},{y,y,y+h,y+h}};
  301. ret=(Object)pnts;
  302. }
  303. if (option==CAL){
  304. double [][] pnts= {{x*fw,(x+w)*fw,(x+w),x*fw},
  305. {y*fh,y*fh,(y+h)*fh,(y+h)*fh}};
  306. ret=(Object)pnts;
  307. }
  308. }
  309. else {
  310. Polygon polygon=roi.getPolygon();
  311. if (option==NOCAL){
  312. int[][] pnts=new int[2][polygon.npoints];
  313. pnts[0]=polygon.xpoints;
  314. pnts[1]=polygon.ypoints;
  315. ret=(Object)pnts;
  316. }
  317. if (option==CAL){
  318. double [][] pnts=new double[2][polygon.npoints];
  319. for (int i=0;i<polygon.npoints; i++){
  320. pnts[0][i]=polygon.xpoints[i]*fw;
  321. pnts[1][i]=polygon.ypoints[i]*fh;
  322. }
  323. ret=(Object)pnts;
  324. }
  325. }
  326. return ret;
  327. }
  328. /**
  329. * Returns the current (selected) image from ImageJ.
  330. *
  331. * @return Current image
  332. */
  333. public static Object getCurrentImage() {
  334. ImagePlus imageplus = WindowManager.getCurrentImage();
  335. if (imageplus == null)
  336. return null;
  337. return get(imageplus);
  338. }
  339. /**
  340. * Returns the specifying image from ImageJ.
  341. *
  342. * @param title title of image
  343. * @return Object
  344. */
  345. public static Object getImage(String title) {
  346. String[] strings = getListImages();
  347. int[] is = WindowManager.getIDList();
  348. for (int i = 0; i < is.length; i++) {
  349. if (strings[i].equals(title)) {
  350. ImagePlus imageplus = WindowManager.getImage(is[i]);
  351. return get(imageplus);
  352. }
  353. }
  354. System.out.println("MIJ Error message: the requested image (" + title + ") does not exist.");
  355. return null;
  356. }
  357. /**
  358. * Returns the histogram of the current image.
  359. *
  360. * @return histogram
  361. */
  362. public static Object getHistogram() {
  363. ImagePlus imageplus = WindowManager.getCurrentImage();
  364. if (imageplus == null)
  365. return null;
  366. return imageplus.getProcessor().getHistogram();
  367. }
  368. /**
  369. * Returns the list of columns currently used in the ResultsTable.
  370. *
  371. * @return list of columns
  372. */
  373. public static String[] getListColumns() {
  374. ResultsTable rt = Analyzer.getResultsTable();
  375. StringTokenizer st = new StringTokenizer(rt.getColumnHeadings());
  376. int n = st.countTokens();
  377. String[] strings = new String[n];
  378. for (int i = 0; i < n; i++) {
  379. strings[i] =st.nextToken();
  380. }
  381. return strings;
  382. }
  383. /**
  384. * Returns the instance of the ResultsTable.
  385. *
  386. * @return Instance of the ResultsTable
  387. */
  388. public static Object getResultsTable(){
  389. ResultsTable rt=Analyzer.getResultsTable();
  390. int col=0;
  391. int[] index=new int[ResultsTable.MAX_COLUMNS];
  392. for (int cnt=0;cnt<ResultsTable.MAX_COLUMNS; cnt++) {
  393. if (rt.columnExists(cnt)){
  394. index[col]=cnt;
  395. col++;
  396. }
  397. }
  398. int counter=rt.getCounter();
  399. float [][] results=new float[counter][col];
  400. for( int i=0;i<col;i++) {
  401. for( int j=0;j<counter;j++) {
  402. results[j][i]=rt.getValue(index[i],j);
  403. }
  404. }
  405. return results;
  406. }
  407. /**
  408. * Returns a specifying column the current instance of ResultsTable.
  409. *
  410. * @param heading heading of a column
  411. * @return column specified by its heading
  412. */
  413. public static Object getColumn(String heading){
  414. ResultsTable rt=Analyzer.getResultsTable();
  415. int col= rt.getColumnIndex(heading);
  416. int counter=rt.getCounter();
  417. float []results=new float[counter];
  418. results=rt.getColumn(col);
  419. return results;
  420. }
  421. /**
  422. * Set a specifying column into the current instance ResultsTable.
  423. *
  424. * @param heading heading of a column
  425. * @param object
  426. */
  427. public static void setColumn(String heading, Object object){
  428. ResultsTable rt=Analyzer.getResultsTable();
  429. int col= rt.getColumnIndex(heading);
  430. if (col==ResultsTable.COLUMN_NOT_FOUND)
  431. col=rt.getFreeColumn(heading);
  432. int cc=rt.getCounter();
  433. if (object instanceof double[]) {
  434. double[] values = (double[]) object;
  435. for (int i=0; i<values.length; i++){
  436. if (cc<=i) rt.incrementCounter();
  437. rt.setValue(col, i, values[i]);
  438. }
  439. }
  440. }
  441. /**
  442. * Get an image.
  443. *
  444. * @param imageplus image
  445. * @return an N x M array representing the input image
  446. */
  447. private static Object get(ImagePlus imageplus) {
  448. if (imageplus == null)
  449. return null;
  450. int width = imageplus.getWidth();
  451. int height = imageplus.getHeight();
  452. int stackSize = imageplus.getStackSize();
  453. int counter = 0;
  454. ImageStack imagestack = imageplus.getStack();
  455. switch (imageplus.getType()) {
  456. case ImagePlus.COLOR_256: {
  457. ;
  458. }
  459. case ImagePlus.GRAY8: {
  460. short[][][] is = new short[height][width][stackSize];
  461. for (int sz = 0; sz < stackSize; sz++) {
  462. ByteProcessor byteprocessor = (ByteProcessor) imagestack.getProcessor(sz + 1);
  463. byte[] pixels = (byte[]) byteprocessor.getPixels();
  464. counter = 0;
  465. int h = 0;
  466. while (h < height) {
  467. int w = 0;
  468. while (w < width) {
  469. is[h][w][sz] = (short)(pixels[counter]&0xff);
  470. w++;
  471. counter++;
  472. }
  473. counter = ++h * width;
  474. }
  475. }
  476. return is;
  477. }
  478. case ImagePlus.GRAY16: {
  479. int[][][] is = new int[height][width][stackSize];
  480. for (int sz = 0; sz < stackSize; sz++) {
  481. counter = 0;
  482. ShortProcessor shortprocessor = (ShortProcessor) imagestack.getProcessor(sz + 1);
  483. short[] spixels = (short[]) shortprocessor.getPixels();
  484. int h = 0;
  485. while (h < height) {
  486. int w = 0;
  487. while (w < width) {
  488. is[h][w][sz] = (int)(spixels[counter]&0xffff);
  489. w++;
  490. counter++;
  491. }
  492. counter = ++h * width;
  493. }
  494. }
  495. return is;
  496. }
  497. case ImagePlus.GRAY32: {
  498. double[][][] fs = new double[height][width][stackSize];
  499. for (int sz = 0; sz < stackSize; sz++) {
  500. FloatProcessor floatprocessor = (FloatProcessor) imagestack.getProcessor(sz + 1);
  501. float[] fpixels = (float[]) floatprocessor.getPixels();
  502. counter = 0;
  503. int i = 0;
  504. while (i < height) {
  505. int j = 0;
  506. while (j < width) {
  507. fs[i][j][sz] = (double) fpixels[counter];
  508. j++;
  509. counter++;
  510. }
  511. counter = ++i * width;
  512. }
  513. }
  514. return fs;
  515. }
  516. case ImagePlus.COLOR_RGB: {
  517. if (stackSize == 1) {
  518. short[][][] is = new short[height][width][3];
  519. ColorProcessor colorprocessor = (ColorProcessor) imagestack.getProcessor(1);
  520. byte[] red = new byte[width * height];
  521. byte[] green = new byte[width * height];
  522. byte[] blue = new byte[width * height];
  523. colorprocessor.getRGB(red, green, blue);
  524. counter = 0;
  525. int h = 0;
  526. while (h < height) {
  527. int w = 0;
  528. while (w < width) {
  529. is[h][w][0] = (short)(red[counter]&0xff);
  530. is[h][w][1] = (short)(green[counter]&0xff);
  531. is[h][w][2] = (short)(blue[counter]&0xff);
  532. w++;
  533. counter++;
  534. }
  535. counter = ++h * width;
  536. }
  537. return is;
  538. }
  539. short[][][][] is = new short[height][width][stackSize][3];
  540. for (int sz = 0; sz < stackSize; sz++) {
  541. ColorProcessor colorprocessor = (ColorProcessor) imagestack.getProcessor(sz + 1);
  542. byte[] red = new byte[width * height];
  543. byte[] green = new byte[width * height];
  544. byte[] blue = new byte[width * height];
  545. colorprocessor.getRGB(red, green, blue);
  546. counter = 0;
  547. int h = 0;
  548. while (h < height) {
  549. int w = 0;
  550. while (w < width) {
  551. is[h][w][sz][0] = (short)red[counter];
  552. is[h][w][sz][1] = (short)green[counter];
  553. is[h][w][sz][2] = (short)blue[counter];
  554. w++;
  555. counter++;
  556. }
  557. counter = ++h * width;
  558. }
  559. }
  560. return is;
  561. }
  562. default:
  563. System.out.println("MIJ Error message: Unknow type of volumes.");
  564. return null;
  565. }
  566. }
  567. /**
  568. * Create a new image in ImageJ from a Matlab variable.
  569. *
  570. * This method try to create a image (ImagePlus of ImageJ) from a Matlab's variable
  571. * which should be an 2D or 3D array
  572. * The recognize type are byte, short, int, float and double. The dimensionality of
  573. * the 2 (image) or 3 (stack of images)
  574. *
  575. * @param object Matlab variable
  576. */
  577. public static void createImage(Object object) {
  578. createImage("Import from Matlab", object);
  579. }
  580. /**
  581. * Create a new image in ImageJ from a Matlab variable with a specified title.
  582. *
  583. * This method try to create a image (ImagePlus of ImageJ) from a Matlab's variable
  584. * which should be an 2D or 3D array
  585. * The recognize type are byte, short, int, float and double. The dimensionality of
  586. * the 2 (image) or 3 (stack of images)
  587. *
  588. * @param title title of the new image
  589. * @param object Matlab variable
  590. */
  591. public static void createImage(String title, Object object) {
  592. createImage(title, object, true);
  593. }
  594. /**
  595. * Create a new image in ImageJ from a Matlab variable with a specified title.
  596. *
  597. * This method tries to create an image (ImagePlus of ImageJ) from a Matlab's variable
  598. * which should be an 2D or 3D array
  599. * The recognize type are byte, short, int, float and double. The dimensionality of
  600. * the 2 (image) or 3 (stack of images)
  601. *
  602. * @param title title of the new image
  603. * @param object Matlab variable
  604. * @param showImage whether to display the newly created image or not
  605. * @return the resulting ImagePlus instance
  606. */
  607. public static ImagePlus createImage(String title, Object object, boolean showImage) {
  608. ImagePlus imp = null;
  609. int i = 0;
  610. if (object instanceof byte[][]) {
  611. byte[][] is = (byte[][]) object;
  612. int height = is.length;
  613. int width = is[0].length;
  614. ByteProcessor byteprocessor = new ByteProcessor(width, height);
  615. byte[] bp = (byte[]) byteprocessor.getPixels();
  616. int h = 0;
  617. while (h < height) {
  618. int w = 0;
  619. while (w < width) {
  620. bp[i] = is[h][w];
  621. w++;
  622. i++;
  623. }
  624. i = ++h * width;
  625. }
  626. imp = new ImagePlus(title, byteprocessor);
  627. }
  628. else if (object instanceof short[][]) {
  629. short[][] is = (short[][]) object;
  630. int height = is.length;
  631. int width = is[0].length;
  632. ShortProcessor shortprocessor = new ShortProcessor(width, height);
  633. short[] sp = (short[]) shortprocessor.getPixels();
  634. int h = 0;
  635. while (h < height) {
  636. int w = 0;
  637. while (w < width) {
  638. sp[i] = is[h][w];
  639. w++;
  640. i++;
  641. }
  642. i = ++h * width;
  643. }
  644. imp = new ImagePlus(title, shortprocessor);
  645. }
  646. else if (object instanceof int[][]) {
  647. if (verbose)
  648. System.out.println("MIJ warning message: Loss of precision: convert int 32-bit to short 16-bit");
  649. int[][] is = (int[][]) object;
  650. int height = is.length;
  651. int width = is[0].length;
  652. ShortProcessor shortprocessor = new ShortProcessor(width, height);
  653. short[] sp = (short[]) shortprocessor.getPixels();
  654. int h = 0;
  655. while (h < height) {
  656. int w = 0;
  657. while (w < width) {
  658. sp[i] = (short)is[h][w];
  659. w++;
  660. i++;
  661. }
  662. i = ++h * width;
  663. }
  664. imp = new ImagePlus(title, shortprocessor);
  665. }
  666. else if (object instanceof float[][]) {
  667. float[][] fs = (float[][]) object;
  668. int height = fs.length;
  669. int width = fs[0].length;
  670. FloatProcessor floatprocessor = new FloatProcessor(width, height);
  671. float[] fp = (float[])floatprocessor.getPixels();
  672. int h = 0;
  673. while (h < height) {
  674. int w = 0;
  675. while (w < width) {
  676. fp[i] = fs[h][w];
  677. w++;
  678. i++;
  679. }
  680. i = ++h * width;
  681. }
  682. floatprocessor.resetMinAndMax();
  683. imp = new ImagePlus(title, floatprocessor);
  684. }
  685. else if (object instanceof double[][]) {
  686. if (verbose)
  687. System.out.println("MIJ warning message: Loss of precision: convert double 32-bit to float 32-bit");
  688. double[][] ds = (double[][]) object;
  689. int height = ds.length;
  690. int width = ds[0].length;
  691. FloatProcessor floatprocessor = new FloatProcessor(width, height);
  692. float[] fp = (float[]) floatprocessor.getPixels();
  693. int h = 0;
  694. while (h < height) {
  695. int w = 0;
  696. while (w < width) {
  697. fp[i] = (float) ds[h][w];
  698. w++;
  699. i++;
  700. }
  701. i = ++h * width;
  702. }
  703. floatprocessor.resetMinAndMax();
  704. imp = new ImagePlus(title, floatprocessor);
  705. }
  706. else if (object instanceof byte[][][]) {
  707. byte[][][] is = (byte[][][]) object;
  708. int height = is.length;
  709. int width = is[0].length;
  710. int stackSize = is[0][0].length;
  711. ImageStack imagestack = new ImageStack(width, height);
  712. for (int sz = 0; sz < stackSize; sz++) {
  713. ByteProcessor byteprocessor = new ByteProcessor(width, height);
  714. byte[] bp = (byte[]) byteprocessor.getPixels();
  715. i = 0;
  716. int h = 0;
  717. while (h < height) {
  718. int w = 0;
  719. while (w < width) {
  720. bp[i] = is[h][w][sz];
  721. w++;
  722. i++;
  723. }
  724. i = ++h * width;
  725. }
  726. imagestack.addSlice("", byteprocessor);
  727. }
  728. imp = new ImagePlus(title, imagestack);
  729. }
  730. else if (object instanceof short[][][]) {
  731. short[][][] is = (short[][][]) object;
  732. int height = is.length;
  733. int width = is[0].length;
  734. int stackSize = is[0][0].length;
  735. ImageStack imagestack = new ImageStack(width, height);
  736. for (int sz = 0; sz < stackSize; sz++) {
  737. ShortProcessor shortprocessor = new ShortProcessor(width, height);
  738. short[] sp = (short[]) shortprocessor.getPixels();
  739. i = 0;
  740. int h = 0;
  741. while (h < height) {
  742. int w = 0;
  743. while (w < width) {
  744. sp[i] = is[h][w][sz];
  745. w++;
  746. i++;
  747. }
  748. i = ++h * width;
  749. }
  750. imagestack.addSlice("", shortprocessor);
  751. }
  752. imp = new ImagePlus(title, imagestack);
  753. }
  754. else if (object instanceof int[][][]) {
  755. if (verbose)
  756. System.out.println("MIJ warning message: Loss of precision: convert int 32 bits to short 16 bits");
  757. int[][][] is = (int[][][]) object;
  758. int height = is.length;
  759. int width = is[0].length;
  760. int stackSize = is[0][0].length;
  761. ImageStack imagestack = new ImageStack(width, height);
  762. for (int sz = 0; sz < stackSize; sz++) {
  763. ShortProcessor shortprocessor = new ShortProcessor(width, height);
  764. short[] sp = (short[]) shortprocessor.getPixels();
  765. i = 0;
  766. int h = 0;
  767. while (h < height) {
  768. int w = 0;
  769. while (w < width) {
  770. sp[i] = (short) is[h][w][sz];
  771. w++;
  772. i++;
  773. }
  774. i = ++h * width;
  775. }
  776. if (sz == 0)
  777. shortprocessor.resetMinAndMax();
  778. imagestack.addSlice("", shortprocessor);
  779. }
  780. imp = new ImagePlus(title, imagestack);
  781. }
  782. else if (object instanceof float[][][]) {
  783. float[][][] fs = (float[][][]) object;
  784. int height = fs.length;
  785. int width = fs[0].length;
  786. int stackSize = fs[0][0].length;
  787. ImageStack imagestack = new ImageStack(width, height);
  788. for (int sz = 0; sz < stackSize; sz++) {
  789. FloatProcessor floatprocessor = new FloatProcessor(width, height);
  790. float[] fp = (float[]) floatprocessor.getPixels();
  791. i = 0;
  792. int h = 0;
  793. while (h < height) {
  794. int w = 0;
  795. while (w < width) {
  796. fp[i] = fs[h][w][sz];
  797. w++;
  798. i++;
  799. }
  800. i = ++h * width;
  801. }
  802. if (sz == 0)
  803. floatprocessor.resetMinAndMax();
  804. imagestack.addSlice("", floatprocessor);
  805. }
  806. imp=new ImagePlus(title, imagestack);
  807. }
  808. else if (object instanceof double[][][]) {
  809. if (verbose)
  810. System.out.println("MIJ warning message: Loss of precision: convert double 32-bit to float 32-bit");
  811. double[][][] ds = (double[][][]) object;
  812. int height = ds.length;
  813. int width = ds[0].length;
  814. int stackSize = ds[0][0].length;
  815. ImageStack imagestack = new ImageStack(width, height);
  816. for (int sz = 0; sz < stackSize; sz++) {
  817. FloatProcessor floatprocessor = new FloatProcessor(width, height);
  818. float[] fp = (float[]) floatprocessor.getPixels();
  819. i = 0;
  820. int h = 0;
  821. while (h < height) {
  822. int w = 0;
  823. while (w < width) {
  824. fp[i] = (float) ds[h][w][sz];
  825. w++;
  826. i++;
  827. }
  828. i = ++h * width;
  829. }
  830. if (sz == 0)
  831. floatprocessor.resetMinAndMax();
  832. imagestack.addSlice("", floatprocessor);
  833. }
  834. imp=new ImagePlus(title, imagestack);
  835. }
  836. else {
  837. System.out.println("MIJ Error message: Unknow type of images or volumes.");
  838. return null;
  839. }
  840. if (showImage) {
  841. imp.show();
  842. imp.updateAndDraw();
  843. }
  844. return imp;
  845. }
  846. /**
  847. * Create a new color image in ImageJ from a Matlab variable.
  848. *
  849. * The last index of the array is the color channel index (3 channels) in
  850. * the follwing order Red-Green-Blue.
  851. *
  852. * @param is Matlab variable
  853. */
  854. public static void createColor(byte[][][] is) {
  855. createColor("Imported from Matlab", is);
  856. }
  857. /**
  858. * Create a new color image in ImageJ from a Matlab variable with a specified title.
  859. *
  860. * @param title title of the new image
  861. * @param is Matlab variable
  862. */
  863. public static void createColor(String title, byte[][][] is) {
  864. int height = is.length;
  865. int width = is[0].length;
  866. int stackSize = is[0][0].length;
  867. ColorProcessor colorprocessor = new ColorProcessor(width, height);
  868. byte[] R_pixels = new byte[width * height];
  869. byte[] G_pixels = new byte[width * height];
  870. byte[] B_pixels = new byte[width * height];
  871. boolean bool = false;
  872. if (stackSize >= 3) {
  873. for (int h = 0; h < height; h++) {
  874. int index = h * width;
  875. int w = 0;
  876. while (w < width) {
  877. R_pixels[index] = is[h][w][0];
  878. G_pixels[index] = is[h][w][1];
  879. B_pixels[index] = is[h][w][2];
  880. w++;
  881. index++;
  882. }
  883. }
  884. }
  885. else if (stackSize >= 2) {
  886. for (int j = 0; j < height; j++) {
  887. int index = j * width;
  888. int i = 0;
  889. while (i < width) {
  890. R_pixels[index] = is[j][i][0];
  891. G_pixels[index] = is[j][i][1];
  892. i++;
  893. index++;
  894. }
  895. }
  896. }
  897. else if (stackSize >= 1) {
  898. for (int j = 0; j < height; j++) {
  899. int index = j * width;
  900. int i = 0;
  901. while (i < width) {
  902. R_pixels[index] = is[j][i][0];
  903. i++;
  904. index++;
  905. }
  906. }
  907. }
  908. colorprocessor.setRGB(R_pixels, G_pixels, B_pixels);
  909. new ImagePlus(title, colorprocessor).show();
  910. }
  911. /**
  912. * Create a new 3D color image in ImageJ from a Matlab variable.
  913. *
  914. * @param is Matlab variable
  915. */
  916. public static void createColor(byte[][][][] is) {
  917. createColor("Import from Matlab", is);
  918. }
  919. /**
  920. * Create a new 3D color image in ImageJ from a Matlab variable with a specified title.
  921. *
  922. * @param title title of the new image
  923. * @param is Matlab variable
  924. */
  925. public static void createColor(String title, byte[][][][] is) {
  926. int height = is.length;
  927. int width = is[0].length;
  928. int stackSize = is[0][0].length;
  929. int dim = is[0][0][0].length;
  930. ImageStack imagestack = new ImageStack(width, height);
  931. ColorProcessor colorprocessor = new ColorProcessor(width, height);
  932. byte[] red = new byte[width * height];
  933. byte[] green = new byte[width * height];
  934. byte[] blue = new byte[width * height];
  935. boolean bool = false;
  936. if (dim >= 3) {
  937. for (int k = 0; k < stackSize; k++) {
  938. for (int j = 0; j < height; j++) {
  939. int index = j * width;
  940. int i = 0;
  941. while (i < width) {
  942. red[index] = is[j][i][k][0];
  943. green[index] = is[j][i][k][1];
  944. blue[index] = is[j][i][k][2];
  945. i++;
  946. index++;
  947. }
  948. }
  949. colorprocessor.setRGB(red, green, blue);
  950. imagestack.addSlice("", colorprocessor);
  951. }
  952. }
  953. else if (dim >= 2) {
  954. for (int k = 0; k < stackSize; k++) {
  955. for (int j = 0; j < height; j++) {
  956. int index = j * width;
  957. int i = 0;
  958. while (i < width) {
  959. red[index] = is[j][i][k][0];
  960. green[index] = is[j][i][k][1];
  961. i++;
  962. index++;
  963. }
  964. }
  965. colorprocessor.setRGB(red, green, blue);
  966. imagestack.addSlice("", colorprocessor);
  967. }
  968. }
  969. else if (dim >= 1) {
  970. for (int k = 0; k < stackSize; k++) {
  971. for (int j = 0; j < height; j++) {
  972. int index = j * width;
  973. int i = 0;
  974. while (i < width) {
  975. red[index] = is[j][i][k][0];
  976. i++;
  977. index++;
  978. }
  979. }
  980. colorprocessor.setRGB(red, green, blue);
  981. imagestack.addSlice("", colorprocessor);
  982. }
  983. }
  984. new ImagePlus(title, imagestack).show();
  985. }
  986. /**
  987. * Run a ImageJ command without arguments.
  988. *
  989. * This method call the run method of ImageJ without any options.
  990. *
  991. * @param command command to run
  992. */
  993. public static void run(String command) {
  994. IJ.run(command);
  995. }
  996. /**
  997. * Run a ImageJ command with specified arguments.
  998. *
  999. * This method call the run method of ImageJ with specified options.
  1000. *
  1001. * @param command command in ImageJ
  1002. * @param options options for the command
  1003. */
  1004. public static void run(String command, String options) {
  1005. IJ.run(command, options);
  1006. }
  1007. /**
  1008. * Set the threshold values of the current image.
  1009. *
  1010. * @param lowerThreshold
  1011. * @param upperThresold
  1012. */
  1013. public static void setTreshold(double lowerThreshold, double upperThresold){
  1014. IJ.setThreshold( lowerThreshold, upperThresold);
  1015. }
  1016. }