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
|
package ij.plugin;
import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.io.FileInfo;
import java.awt.Color;
/**
* This plugin implements the Image/Stacks/Tools/Make Substack command.
* What it does is extracts selected images from a stack to make a new substack.
* It takes three types of inputs: a range of images (e.g. 2-14), a range of images
* with an increment (e.g. 2-14-3), or a list of images (e.g. 7,9,25,27,34,132).
* It then copies those images from the active stack to a new stack in the order
* of listing or range.
*
* @author Anthony Padua
* @author Daniel Barboriak, MD
* @author Neuroradiology
* @author Duke University Medical Center
*
* @author Ved P. Sharma, Ph.D.
* @author Anatomy and Structural Biology
* @author Albert Einstein College of Medicine
*
*/
public class SubstackMaker implements PlugIn {
private static boolean delete = false;
public void run(String arg) {
ImagePlus imp = IJ.getImage();
if (imp.isHyperStack() || imp.isComposite()) {
(new SubHyperstackMaker()).run("");
return;
}
String userInput = showDialog();
if (userInput==null)
return;
ImagePlus imp2 = makeSubstack(imp, userInput);
if (imp2!=null)
imp2.show();
}
public ImagePlus makeSubstack(ImagePlus imp, String userInput) {
String stackTitle = "Substack ("+userInput+")";
if (stackTitle.length()>25) {
int idxA = stackTitle.indexOf(",",18);
int idxB = stackTitle.lastIndexOf(",");
if(idxA>=1 && idxB>=1){
String strA = stackTitle.substring(0,idxA);
String strB = stackTitle.substring(idxB+1);
stackTitle = strA + ", ... " + strB;
}
}
ImagePlus imp2 = null;
try {
int idx1 = userInput.indexOf("-");
if (idx1>=1) { // input displayed in range
String rngStart = userInput.substring(0, idx1);
String rngEnd = userInput.substring(idx1+1);
Integer obj = new Integer(rngStart);
int first = obj.intValue();
int inc = 1;
int idx2 = rngEnd.indexOf("-");
if (idx2>=1) {
String rngEndAndInc = rngEnd;
rngEnd = rngEndAndInc.substring(0, idx2);
String rngInc = rngEndAndInc.substring(idx2+1);
obj = new Integer(rngInc);
inc = obj.intValue();
}
obj = new Integer(rngEnd);
int last = obj.intValue();
imp2 = stackRange(imp, first, last, inc, stackTitle);
} else {
int count = 1; // count # of slices to extract
for (int j=0; j<userInput.length(); j++) {
char ch = Character.toLowerCase(userInput.charAt(j));
if (ch==',') {count += 1;}
}
int[] numList = new int[count];
for(int i=0; i<count; i++) {
int idx2 = userInput.indexOf(",");
if(idx2>0) {
String num = userInput.substring(0,idx2);
Integer obj = new Integer(num);
numList[i] = obj.intValue();
userInput = userInput.substring(idx2+1);
}
else{
String num = userInput;
Integer obj = new Integer(num);
numList[i] = obj.intValue();
}
}
imp2 = stackList(imp, count, numList, stackTitle);
}
} catch (Exception e) {
IJ.error("Substack Maker", "Invalid input string: \n \n \""+userInput+"\"");
}
return imp2;
}
String showDialog() {
String options = Macro.getOptions();
if (options!=null && !options.contains("slices=")) {
Macro.setOptions(options.replace("channels=", "slices="));
Macro.setOptions(options.replace("frames=", "slices="));
}
GenericDialog gd = new GenericDialog("Substack Maker");
gd.setInsets(10,45,0);
gd.addMessage("Enter a range (e.g. 2-14), a range with increment\n(e.g. 1-100-2) or a list (e.g. 7,9,25,27)", null, Color.darkGray);
gd.addStringField("Slices:", "", 40);
gd.addCheckbox("Delete slices from original stack", delete);
gd.showDialog();
if (gd.wasCanceled())
return null;
else {
delete = gd.getNextBoolean();
return gd.getNextString();
}
}
// extract specific slices
ImagePlus stackList(ImagePlus imp, int count, int[] numList, String stackTitle) throws Exception {
ImageStack stack = imp.getStack();
ImageStack stack2 = null;
boolean virtualStack = stack.isVirtual();
double min = imp.getDisplayRangeMin();
double max = imp.getDisplayRangeMax();
Roi roi = imp.getRoi();
for (int i=0, j=0; i<count; i++) {
int currSlice = numList[i]-j;
ImageProcessor ip2 = stack.getProcessor(currSlice);
ip2.setRoi(roi);
ip2 = ip2.crop();
if (stack2==null)
stack2 = new ImageStack(ip2.getWidth(), ip2.getHeight());
stack2.addSlice(stack.getSliceLabel(currSlice), ip2);
if (delete) {
stack.deleteSlice(currSlice);
j++;
}
}
if (delete) {
imp.setStack(stack);
// next three lines for updating the scroll bar
ImageWindow win = imp.getWindow();
StackWindow swin = (StackWindow) win;
if (swin!=null)
swin.updateSliceSelector();
}
ImagePlus impSubstack = imp.createImagePlus();
impSubstack.setStack(stackTitle, stack2);
if (virtualStack)
impSubstack.setDisplayRange(min, max);
return impSubstack;
}
// extract range of slices
ImagePlus stackRange(ImagePlus imp, int first, int last, int inc, String title) throws Exception {
ImageStack stack = imp.getStack();
ImageStack stack2 = null;
boolean virtualStack = stack.isVirtual();
double min = imp.getDisplayRangeMin();
double max = imp.getDisplayRangeMax();
Roi roi = imp.getRoi();
boolean showProgress = stack.getSize()>400 || stack.isVirtual();
for (int i= first, j=0; i<= last; i+=inc) {
if (showProgress) IJ.showProgress(i,last);
int currSlice = i-j;
ImageProcessor ip2 = stack.getProcessor(currSlice);
ip2.setRoi(roi);
ip2 = ip2.crop();
if (stack2==null)
stack2 = new ImageStack(ip2.getWidth(), ip2.getHeight());
stack2.addSlice(stack.getSliceLabel(currSlice), ip2);
if (delete) {
stack.deleteSlice(currSlice);
j++;
}
}
if (delete) {
imp.setStack(stack);
// next three lines for updating the scroll bar
ImageWindow win = imp.getWindow();
StackWindow swin = (StackWindow) win;
if (swin!=null)
swin.updateSliceSelector();
}
ImagePlus substack = imp.createImagePlus();
substack.setStack(title, stack2);
substack.setCalibration(imp.getCalibration());
if (virtualStack)
substack.setDisplayRange(min, max);
return substack;
}
}
|