File: ImagesToStack.java

package info (click to toggle)
imagej 1.52j-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,604 kB
  • sloc: java: 120,017; sh: 279; xml: 161; makefile: 6
file content (283 lines) | stat: -rw-r--r-- 8,487 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package ij.plugin;
import ij.plugin.frame.Recorder;
import ij.*;
import ij.gui.*;
import ij.process.*;
import ij.measure.Calibration;
import ij.macro.Interpreter;
import ij.io.FileInfo;


/** Implements the Image/Stacks/Images to Stack" command. */
public class ImagesToStack implements PlugIn {
	private static final int rgb = 33;
	private static final int COPY_CENTER=0, COPY_TOP_LEFT=1, SCALE_SMALL=2, SCALE_LARGE=3;
	private static final String[] methods = {"Copy (center)", "Copy (top-left)", "Scale (smallest)", "Scale (largest)"};
	private static int staticMethod = COPY_CENTER;
	private static boolean staticBicubic;
	private static boolean staticKeep;
	private static boolean staticTitlesAsLabels = true;
	private int method = COPY_CENTER;
	private boolean bicubic;
	private boolean keep;
	private boolean titlesAsLabels = true;
	private String filter;
	private int width, height;
	private int maxWidth, maxHeight;
	private int minWidth, minHeight;
	private int minSize, maxSize;
	private boolean allInvertedLuts;
	private Calibration cal2;
	private int stackType;
	private ImagePlus[] images;
	private String name = "Stack";
	
	/** Converts the images in 'images' to a stack, using the 
		default settings ("copy center" and "titles as labels"). */
	public static ImagePlus run(ImagePlus[] images) {
		ImagesToStack itos = new ImagesToStack();
		int count = itos.findMinMaxSize(images, images.length);
		return itos.convert(images, count);
	}

	public void run(String arg) {
		convertImagesToStack();
	}

	public void convertImagesToStack() {
		boolean scale = false;
		int[] wList = WindowManager.getIDList();
		if (wList==null) {
			IJ.error("No images are open.");
			return;
		}

		int count = 0;
		int stackCount = 0;
		images = new ImagePlus[wList.length];
		for (int i=0; i<wList.length; i++) {
			ImagePlus imp = WindowManager.getImage(wList[i]);
			if (imp.getStackSize()==1)
				images[count++] = imp;
			else
				stackCount++;
		}		
		if (count<2) {
			String msg = "";
			if (stackCount>1)
				msg = "\n \nUse the Image>Stacks>Tools>Concatenate\ncommand to combine stacks.";
			IJ.error("Images to Stack", "There must be at least two open 2D images."+msg);
			return;
		}

		filter = null;
		count = findMinMaxSize(images, count);
		boolean sizesDiffer = width!=minWidth||height!=minHeight;
		boolean showDialog = true;
		String macroOptions = Macro.getOptions();
		if (IJ.macroRunning() && macroOptions==null) {
			if (sizesDiffer) {
				IJ.error("Images are not all the same size");
				return;
			} 
			showDialog = false;
		}
		if (showDialog) {
			GenericDialog gd = new GenericDialog("Images to Stack");
			if (sizesDiffer) {
				String msg = "The "+count+" images differ in size (smallest="+minWidth+"x"+minHeight
				+",\nlargest="+maxWidth+"x"+maxHeight+"). They will be converted\nto a stack using the specified method.";
				gd.setInsets(0,0,5);
				gd.addMessage(msg);
				gd.addChoice("Method:", methods, methods[staticMethod]);
			}
			gd.addStringField("Name:", name, 12);
			gd.addStringField("Title Contains:", "", 12);
			if (sizesDiffer)
				gd.addCheckbox("Bicubic Interpolation", staticBicubic);
			gd.addCheckbox("Use Titles as Labels", staticTitlesAsLabels);
			gd.addCheckbox("Keep Source Images", staticKeep);
			gd.showDialog();
			if (gd.wasCanceled()) return;
			if (sizesDiffer)
				method = gd.getNextChoiceIndex();
			name = gd.getNextString();
			filter = gd.getNextString();
			if (sizesDiffer)
				bicubic = gd.getNextBoolean();
			titlesAsLabels = gd.getNextBoolean();
			keep = gd.getNextBoolean();
			if (filter!=null && (filter.equals("") || filter.equals("*")))
				filter = null;
			if (filter!=null) {
				count = findMinMaxSize(images, count);
				if (count==0) {
					IJ.error("Images to Stack", "None of the images have a title containing \""+filter+"\"");
				}
			}
			if (!IJ.isMacro()) {
				staticMethod = method;
				staticBicubic = bicubic;
				staticKeep = keep;
				staticTitlesAsLabels = titlesAsLabels;
			}
			if (Recorder.record)
   				Recorder.recordCall("imp = ImagesToStack.run(arrayOfImages);");
		} else
			keep = false;
		if (method==SCALE_SMALL) {
			width = minWidth;
			height = minHeight;
		} else if (method==SCALE_LARGE) {
			width = maxWidth;
			height = maxHeight;
		}
		ImagePlus stack = convert(images, count);
		if (stack!=null)
			stack.show();
	}
	
	private ImagePlus convert(ImagePlus[] images, int count) {		
		double min = Double.MAX_VALUE;
		double max = -Double.MAX_VALUE;
		ImageStack stack = new ImageStack(width, height);
		FileInfo fi = images[0].getOriginalFileInfo();
		if (fi!=null && fi.directory==null) fi = null;
		Overlay overlay = new Overlay();
		for (int i=0; i<count; i++) {
			ImageProcessor ip = images[i].getProcessor();
			boolean invertedLut = ip.isInvertedLut();
			if (ip.getMin()<min) min = ip.getMin();
			if (ip.getMax()>max) max = ip.getMax();
			String label = titlesAsLabels?images[i].getTitle():null;
			if (label!=null) {
				String info = (String)images[i].getProperty("Info");
				if (info!=null) label += "\n" + info;
			}
			if (fi!=null) {
				FileInfo fi2 = images[i].getOriginalFileInfo();
				if (fi2!=null && !fi.directory.equals(fi2.directory))
					fi = null;
			}
			switch (stackType) {
				case 16: ip = ip.convertToShort(false); break;
				case 32: ip = ip.convertToFloat(); break;
				case rgb: ip = ip.convertToRGB(); break;
				default: break;
			}
			if (invertedLut && !allInvertedLuts) {
				if (keep)
					ip = ip.duplicate();
				ip.invert();
			}
			if (ip.getWidth()!=width||ip.getHeight()!=height) {
				switch (method) {
					case COPY_TOP_LEFT: case COPY_CENTER:
						ImageProcessor ip2 = null;
						switch (stackType) {
							case 8: ip2 = new ByteProcessor(width, height); break;
							case 16: ip2 = new ShortProcessor(width, height); break;
							case 32: ip2 = new FloatProcessor(width, height); break;
							case rgb: ip2 = new ColorProcessor(width, height); break;
						}
						int xoff=0, yoff=0;
						if (method==COPY_CENTER) {
							xoff = (width-ip.getWidth())/2;
							yoff = (height-ip.getHeight())/2;
						}
						ip2.insert(ip, xoff, yoff);
						ip = ip2;
						break;
					case SCALE_SMALL: case SCALE_LARGE:
						ip.setInterpolationMethod((bicubic?ImageProcessor.BICUBIC:ImageProcessor.BILINEAR));
						ip.resetRoi();
						ip = ip.resize(width, height);
						break;
				}
			} else {
				if (keep)
					ip = ip.duplicate();
				Overlay overlay2 = images[i].getOverlay();
				if (overlay2!=null) {
					for (int j=0; j<overlay2.size(); j++) {
						Roi roi = overlay2.get(j);
						roi.setPosition(i+1);
						overlay.add((Roi)roi.clone());
					}
				}
			}
			stack.addSlice(label, ip);
			if (i==0 && invertedLut && !allInvertedLuts)
				stack.setColorModel(null);
			if (!keep) {
				images[i].changes = false;
				images[i].close();
			}
		}
		if (stack.getSize()==0)
			return null;
		ImagePlus imp = new ImagePlus(name, stack);
		if (stackType==16 || stackType==32)
			imp.getProcessor().setMinAndMax(min, max);
		if (cal2!=null)
			imp.setCalibration(cal2);
		if (fi!=null) {
			fi.fileName = "";
			fi.nImages = imp.getStackSize();
			imp.setFileInfo(fi);
		}
		if (overlay.size()>0)
			imp.setOverlay(overlay);
		return imp;
	}
	
	private int findMinMaxSize(ImagePlus[] images, int count) {
		int index = 0;
		stackType = 8;
		width = 0;
		height = 0;
		cal2 = images[0].getCalibration();
		maxWidth = 0;
		maxHeight = 0;
		minWidth = Integer.MAX_VALUE;
		minHeight = Integer.MAX_VALUE;
		minSize = Integer.MAX_VALUE;
		allInvertedLuts = true;
		maxSize = 0;
		for (int i=0; i<count; i++) {
			if (exclude(images[i].getTitle())) continue;
			if (images[i].getType()==ImagePlus.COLOR_256)
				stackType = rgb;
			if (!images[i].getProcessor().isInvertedLut())
				allInvertedLuts = false;
			int type = images[i].getBitDepth();
			if (type==24) type = rgb;
			if (type>stackType) stackType = type;
			int w=images[i].getWidth(), h=images[i].getHeight();
			if (w>width) width = w;
			if (h>height) height = h;
			int size = w*h;
			if (size<minSize) {
				minSize = size;
				minWidth = w;
				minHeight = h;
			}
			if (size>maxSize) {
				maxSize = size;
				maxWidth = w;
				maxHeight = h;
			}
			Calibration cal = images[i].getCalibration();
			if (!images[i].getCalibration().equals(cal2))
				cal2 = null;
			images[index++] = images[i];
		}
		return index;
	}

	final boolean exclude(String title) {
		return filter!=null && title!=null && title.indexOf(filter)==-1;
	}
	
}