Showing posts with label Imageplus. Show all posts
Showing posts with label Imageplus. Show all posts

Tuesday, 1 October 2013

ImageJ: Create ImageStack for dicom 2d slice

Hello friends following code for make ImagePlus by given path and also modify your code in your own way...enjoy..!!!!
package imagejexample;
import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.io.Opener;
import ij.process.ImageProcessor;
import java.awt.image.ColorModel;
import java.io.File;
/**
*
* @author Jay.Thakkar
*/
public class TestInput {
public static void main(String[] args) {
TestInput testInput = new TestInput();
testInput.run();
}
private static boolean grayscale;
private static boolean halfSize;
public void run() {
String dir = "D:\\testImaes\\skull\\";
String[] list = new File(dir).list();
System.out.println("### Size()::" + list.length);
int n = list.length;
int width = 0, height = 0, type = 0;
ImageStack stack = null;
double min = Double.MAX_VALUE;
double max = -Double.MAX_VALUE;
int k = 0;
try {
for (int i = 0; i < n; i++) {
String path = (dir + list[i]);
if (path == null) {
break;
}
if (list[i].endsWith(".txt")) {
continue;
}
//System.out.println("Path:"+path);
ImagePlus imp = new Opener().openImage(path);
if (imp != null && stack == null) {
width = imp.getWidth();
height = imp.getHeight();
type = imp.getType();
ColorModel cm = imp.getProcessor().getColorModel();
if (halfSize) {
stack = new ImageStack(width / 2, height / 2, cm);
} else {
stack = new ImageStack(width, height, cm);
}
}
if (stack != null) {
k = stack.getSize() + 1;
}
if (imp == null) {
IJ.log(list[i] + ": unable to open");
} else if (imp.getWidth() != width || imp.getHeight() != height) {
IJ.log(list[i] + ": wrong dimensions");
} else if (imp.getType() != type) {
IJ.log(list[i] + ": wrong type");
} else {
ImageProcessor ip = imp.getProcessor();
if (grayscale) {
ip = ip.convertToByte(true);
}
if (halfSize) {
ip = ip.resize(width / 2, height / 2);
}
if (ip.getMin() < min) {
min = ip.getMin();
}
if (ip.getMax() > max) {
max = ip.getMax();
}
String label = imp.getTitle();
String info = (String) imp.getProperty("Info");
if (info != null) {
label += "\n" + info;
}
stack.addSlice(label, ip);
}
System.gc();
}
} catch (OutOfMemoryError e) {
IJ.outOfMemory("FolderOpener");
stack.trim();
}
if (stack != null && stack.getSize() > 0) {
ImagePlus imp2 = new ImagePlus("Stack", stack);
if (imp2.getType() == ImagePlus.GRAY16 || imp2.getType() == ImagePlus.GRAY32) {
imp2.getProcessor().setMinAndMax(min, max);
}
imp2.show();
}
}
}
view raw CustomImagePlus hosted with ❤ by GitHub