Guest User

Untitled

a guest
Mar 18th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.Rectangle;
  4. import java.awt.geom.AffineTransform;
  5. import java.awt.geom.Area;
  6. import java.awt.image.BufferedImage;
  7. import java.awt.image.DataBufferByte;
  8. import java.io.File;
  9. import java.util.ArrayList;
  10. import java.util.Collections;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.TreeMap;
  15. import java.util.TreeSet;
  16. import java.util.concurrent.ExecutorService;
  17. import java.util.concurrent.Future;
  18.  
  19. import amira.AmiraMeshEncoder;
  20. import ij.IJ;
  21. import ij.ImageJ;
  22. import ij.ImagePlus;
  23. import ij.ImageStack;
  24. import ij.gui.Roi;
  25. import ij.io.FileSaver;
  26. import ij.measure.Calibration;
  27. import ij.process.ByteProcessor;
  28. import ij.process.FloatProcessor;
  29. import ij.process.ImageProcessor;
  30. import ij.process.ShortProcessor;
  31. import ini.trakem2.Project;
  32. import ini.trakem2.display.*;
  33. import ini.trakem2.display.paint.USHORTPaint;
  34. import ini.trakem2.tree.ProjectThing;
  35. import ini.trakem2.utils.Utils;
  36.  
  37.  
  38.  
  39. destdir="/home/john/tmp/tem2/";
  40.  
  41. public static void writeArealists( String destdir, ProjectThing root, String prefix,
  42. LayerSet layer_set,
  43. Roi roi, float scale )
  44. {
  45. // put a check here to be sure root is okay
  46. if( root == null )
  47. return;
  48.  
  49. // add this object's title to the file name
  50. String thisprefix = prefix + "_" + root.getTitle();
  51.  
  52. // get this project's object and see if it's an AreaList
  53. Object obj = root.getObject();
  54. if( obj instanceof AreaList )
  55. {
  56. // if so, write a tif
  57. ImagePlus imp = exportAsMask( (AreaList)obj, layer_set, roi, scale );
  58. System.out.println( "the imp mask : " + imp );
  59. System.out.println( "writing: " + thisprefix );
  60. IJ.save( imp, destdir + thisprefix + ".tif" );
  61. }
  62.  
  63. // recurse
  64. children = root.getChildren();
  65. if( children != null )
  66. {
  67. for( int i = 0; i < children.size(); i++ )
  68. {
  69. ProjectThing c = children.get( i );
  70. writeArealists( destdir, c, thisprefix, layer_set, roi, scale );
  71. }
  72. }
  73. }
  74.  
  75. static public ImagePlus exportAsMask( AreaList al, LayerSet layer_set, ij.gui.Roi roi, float scale )
  76. {
  77. //layer_ids = al.getLayerIds();
  78. //int first_layer = al.
  79. //int last_layer
  80.  
  81. list = new ArrayList();
  82. list.add( al );
  83.  
  84. // Compute highest label value, which affects of course the stack image type
  85. label_values = new TreeSet();
  86. for (Displayable d : list) {
  87. String label = d.getProperty("label");
  88. if (null != label) label_values.add(Integer.parseInt(label));
  89. }
  90. int lowest=0, highest=0;
  91. if (label_values.size() > 0) {
  92. lowest = label_values.first();
  93. highest = label_values.last();
  94. }
  95. int n_non_labeled = list.size() - label_values.size();
  96. int max_label_value = highest + n_non_labeled;
  97.  
  98. int type_ = ImagePlus.GRAY8;
  99. if (max_label_value > 255) {
  100. type_ = ImagePlus.GRAY16;
  101. if (max_label_value > 65535) {
  102. type_ = ImagePlus.GRAY32;
  103. }
  104. }
  105. int type = type_;
  106.  
  107. int width,height;
  108. Rectangle broi;
  109. if (null == roi) {
  110. broi = null;
  111. width = (int)(layer_set.getLayerWidth() * scale);
  112. height = (int)(layer_set.getLayerHeight() * scale);
  113. } else {
  114. broi = roi.getBounds();
  115. width = (int)(broi.width * scale);
  116. height = (int)(broi.height * scale);
  117. }
  118.  
  119.  
  120. ImageStack stack = new ImageStack(width, height);
  121. Calibration cal = layer_set.getCalibration();
  122.  
  123. //float len = last_layer - first_layer + 1;
  124. //layers = layer_set.getLayers().subList(first_layer, last_layer+1);
  125. layers = al.getLayerRange();
  126. float len = (float)layers.size();
  127.  
  128. slices = Collections.synchronizedMap(new TreeMap());
  129.  
  130. for (int k = 0; k < layers.size(); k++) {
  131. Layer la = layers.get(k);
  132. int slice = k;
  133. Utils.showProgress(slice / len);
  134.  
  135. ImageProcessor ip;
  136.  
  137. if (ImagePlus.GRAY8 == type) {
  138. BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
  139. Graphics2D g = bi.createGraphics();
  140.  
  141. for (AreaList ali : list) {
  142. Area area = ali.getArea(la);
  143. if (null == area || area.isEmpty()) continue;
  144. // Transform: the scale and the roi
  145. AffineTransform aff = new AffineTransform();
  146. // reverse order of transformations:
  147. /* 3 - To scale: */ if (1 != scale) aff.scale(scale, scale);
  148. /* 2 - To roi coordinates: */ if (null != broi) aff.translate(-broi.x, -broi.y);
  149. /* 1 - To world coordinates: */ aff.concatenate(ali.getAffineTransform());
  150. g.setTransform(aff);
  151. int label = 255;
  152. g.setColor(new Color(label, label, label));
  153. g.fill(area);
  154. }
  155. g.dispose();
  156. ip = new ByteProcessor(bi);
  157. bi.flush();
  158.  
  159. } else if (ImagePlus.GRAY16 == type) {
  160. USHORTPaint paint = new USHORTPaint((short)0);
  161. BufferedImage bi = new BufferedImage(paint.getComponentColorModel(), paint.getComponentColorModel().createCompatibleWritableRaster(width, height), false, null);
  162. Graphics2D g = bi.createGraphics();
  163. //ColorSpace ugray = ColorSpace.getInstance(ColorSpace.CS_GRAY);
  164.  
  165. int painted = 0;
  166.  
  167. for (AreaList ali : list) {
  168. Area area = ali.getArea(la);
  169. if (null == area || area.isEmpty()) continue;
  170. // Transform: the scale and the roi
  171. AffineTransform aff = new AffineTransform();
  172. // reverse order of transformations:
  173. /* 3 - To scale: */ if (1 != scale) aff.scale(scale, scale);
  174. /* 2 - To roi coordinates: */ if (null != broi) aff.translate(-broi.x, -broi.y);
  175. /* 1 - To world coordinates: */ aff.concatenate(ali.at);
  176. // Fill
  177. g.setTransform(aff);
  178.  
  179. // The color doesn't work: paints in a stretched 8-bit mode
  180. //g.setColor(new Color(ugray, new float[]{((float)labels.get(d)) / range}, 1));
  181.  
  182. short ls = (short)255;
  183. paint.setValue(ls);
  184. g.setPaint(paint);
  185.  
  186. g.fill(area); //.createTransformedArea(aff));
  187.  
  188. painted += 1;
  189. }
  190. g.dispose();
  191. ip = new ShortProcessor(bi);
  192. bi.flush();
  193.  
  194. Utils.log2("painted: " + painted);
  195.  
  196. } else {
  197. // Option 1: could use the same as above, but shifted by 65536, so that 65537 is 1, 65538 is 2, etc.
  198. // and keep doing it until no more need to be shifted.
  199. // The PROBLEM: cannot keep the order without complicated gymnastics to remember
  200. // which label in which image has to be merged to the final image, which prevent
  201. // a simple one-pass blitter.
  202. //
  203. // Option 2: paint each arealist, extract the image, use it as a mask for filling:
  204.  
  205. FloatProcessor fp = new FloatProcessor(width, height);
  206. float[] fpix = (float[]) fp.getPixels();
  207. ip = fp;
  208.  
  209. BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
  210. Graphics2D gbi = bi.createGraphics();
  211.  
  212. for (AreaList ali : list) {
  213. Area area = ali.getArea(la);
  214. if (null == area || area.isEmpty()) {
  215. continue;
  216. }
  217. // Transform: the scale and the roi
  218. // reverse order of transformations:
  219. AffineTransform aff = new AffineTransform();
  220. /* 3 - To scale: */ if (1 != scale) aff.scale(scale, scale);
  221. /* 2 - To ROI coordinates: */ if (null != broi) aff.translate(-broi.x, -broi.y);
  222. /* 1 - To world coordinates: */ aff.concatenate(ali.at);
  223. Area s = area.createTransformedArea(aff);
  224. Rectangle sBounds = s.getBounds();
  225. // Need to paint at all?
  226. if (0 == sBounds.width || 0 == sBounds.height || !sBounds.intersects(0, 0, width, height)) continue;
  227. // Paint shape
  228. gbi.setColor(Color.white);
  229. gbi.fill(s);
  230. // Read out painted region
  231. int x0 = Math.max(0, sBounds.x);
  232. int y0 = Math.max(0, sBounds.y);
  233. int xN = Math.min(width, sBounds.x + sBounds.width);
  234. int yN = Math.min(height, sBounds.y + sBounds.height);
  235. // Get the array
  236. byte[] bpix = ((DataBufferByte)bi.getRaster().getDataBuffer()).getData();
  237. float value = 255f;
  238. // For every non-black pixel, set a 'value' pixel in the FloatProcessor
  239. for (int y = y0; y < yN; ++y) {
  240. for (int x = x0; x < xN; ++x) {
  241. int pos = y * width + x;
  242. if (0 == bpix[pos]) continue; // black
  243. fpix[pos] = value;
  244. }
  245. }
  246. // Clear image region
  247. gbi.setColor(Color.black);
  248. gbi.fill(s);
  249. }
  250. gbi.dispose();
  251. bi.flush();
  252. }
  253.  
  254. slices.put(slice, ip);
  255. }
  256.  
  257.  
  258. for ( e : slices.entrySet()) {
  259. Layer la = layers.get(e.getKey());
  260. stack.addSlice(la.getZ() * cal.pixelWidth + "", e.getValue());
  261. if (ImagePlus.GRAY8 != type) {
  262. e.getValue().setMinAndMax(lowest, highest);
  263. }
  264. }
  265.  
  266. Utils.showProgress(1);
  267. // Save via file dialog:
  268. ImagePlus imp = new ImagePlus("Labels", stack);
  269. imp.setCalibration(layer_set.getCalibrationCopy());
  270.  
  271. return imp;
  272. }
  273.  
  274.  
  275. // get the root ProjectThing
  276. project = Project.getProjects().get(0);
  277. root = project.getRootProjectThing();
  278. layer_set = project.getRootLayerSet();
  279.  
  280. //first = 0;
  281. //last = layer_set.size() - 1;
  282. scale = 1.0f;
  283.  
  284. prefix = "";
  285.  
  286. // call the function
  287. writeArealists( destdir, root, prefix,
  288. layer_set, null, scale );
Add Comment
Please, Sign In to add comment