File: StackMaker.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 (80 lines) | stat: -rw-r--r-- 2,529 bytes parent folder | download | duplicates (4)
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
package ij.plugin;
import ij.*;
import ij.gui.*;
import ij.process.*;
import ij.plugin.*;
import ij.util.Tools;

/** The plugin implements the Image/Stacks/Tools/Montage to Stack command.
	It creates a w*h image stack from an wxh image montage.
	This is the opposite of what the "Make Montage" command does.
	2010.04.20,TF: Final stack can be cropped to remove border around frames.
*/
public class StackMaker implements PlugIn {
	private int rows;
	private int columns;
	private int border;

	public void run(String arg) {
		ImagePlus imp = WindowManager.getCurrentImage();
		if (imp==null) {
			IJ.noImage();
			return;
		}
		if (imp.getStackSize()>1) {
			IJ.error("This command requires a montage");
			return;
		}		
		String options = Macro.getOptions();
		if (options!=null) {
			options = options.replace("images_per_row=", "columns=");
			options = options.replace("images_per_column=", "rows=");
		}
		columns = info("xMontage", imp, 2);
		rows = info("yMontage", imp, 2);
		String montageHeight = (String)imp.getProperty("yMontage");
		if (montageHeight!=null)
			rows = Integer.parseInt(montageHeight);
		GenericDialog gd = new GenericDialog("Stack Maker");
		gd.addNumericField("Columns: ", columns, 0);
		gd.addNumericField("Rows: ", rows, 0);
		gd.addNumericField("Border width: ", border, 0);
		gd.showDialog();
		if (gd.wasCanceled())
			return;
		columns = (int)gd.getNextNumber();
		rows = (int)gd.getNextNumber();
		border = (int)gd.getNextNumber();
		if (rows==0 || columns==0)
			return;
		ImageStack stack = makeStack(imp.getProcessor(), rows, columns, border);
		new ImagePlus("Stack", stack).show();
	}
	
	private int info(String key, ImagePlus imp, int value) {
		String svalue = imp.getStringProperty(key);
		if (svalue!=null)
			value = Integer.parseInt(svalue);
		return value;
	}
	
	public ImageStack makeStack(ImageProcessor ip, int rows, int columns, int border) {
		int stackSize = rows*columns;
		int width = ip.getWidth()/columns;
		int height = ip.getHeight()/rows;
		//IJ.log("makeStack: "+rows+" "+columns+" "+border+" "+width+" "+height);
		ImageStack stack = new ImageStack(width, height);
		for (int y=0; y<rows; y++)
			for (int x=0; x<columns; x++) {
				ip.setRoi(x*width, y*height, width, height);
				stack.addSlice(null, ip.crop());
			}
		if (border>0) { 
			int cropwidth = width-border-border/2;
			int cropheight = height-border-border/2;
			StackProcessor sp = new StackProcessor(stack,ip); 
			stack = sp.crop(border, border, cropwidth, cropheight);
		}
		return stack;
	}	 
}