ImagePlus and TiffEncoder
Posted by Dong Liu on Aug 19, 2009; 3:59pm
URL: http://imagej.273.s1.nabble.com/ImagePlus-and-TiffEncoder-tp3691449.html
Hi,
I am modifying the Averaging_Reducer by Wayne to a utility that does
not rely on the ImageJ GUI and can directly output to a stream as a
certain encoding. The problem I found is that the output is just a
blank image. Not sure what I should do for the new ImagePlus object
binned in order to get a correct Image/FileInfo. No exceptions in my
log.
Thanks,
Dong
The code i have is the following
binning.java:
public class Binning {
private int xshrink=4, yshrink=4; // the default value set to be 4
double product;
int[] pixel = new int[3];
int[] sum = new int[3];
int samples;
public int getXshrink() {
return xshrink;
}
public void setXshrink(int xshrink) {
this.xshrink = xshrink;
}
public int getYshrink() {
return yshrink;
}
public void setYshrink(int yshrink) {
this.yshrink = yshrink;
}
public ImagePlus shrink(ImagePlus imp) {
ImageStack stack1 = imp.getStack();
ImageStack stack2 = new
ImageStack(imp.getWidth()/xshrink,imp.getHeight()/yshrink);
int n = stack1.getSize();
for (int i=1; i<=n; i++) {
// IJ.showStatus(i+"/"+n);
// IJ.showProgress(i, n);
ImageProcessor ip2 = shrink(stack1.getProcessor(i));
stack2.addSlice(null, ip2);
}
ImagePlus imp2 = new ImagePlus("Reduced "+imp.getShortTitle(), stack2);
imp2.setCalibration(imp.getCalibration());
Calibration cal2 = imp2.getCalibration();
cal2.pixelWidth *= xshrink;
cal2.pixelHeight *= yshrink;
return imp2;
}
public ImageProcessor shrink(ImageProcessor ip) {
if (ip instanceof FloatProcessor)
return shrinkFloat(ip);
samples = ip instanceof ColorProcessor?3:1;
int w = ip.getWidth()/xshrink;
int h = ip.getHeight()/yshrink;
ImageProcessor ip2 = ip.createProcessor(w, h);
for (int y=0; y<h; y++)
for (int x=0; x<w; x++)
ip2.putPixel(x, y, getAverage(ip, x, y));
return ip2;
}
int[] getAverage(ImageProcessor ip, int x, int y) {
for (int i=0; i<samples; i++)
sum[i] = 0;
for (int y2=0; y2<yshrink; y2++) {
for (int x2=0; x2<xshrink; x2++) {
pixel = ip.getPixel(x*xshrink+x2, y*yshrink+y2, pixel);
for (int i=0; i<samples; i++)
sum[i] += pixel[i];
}
}
for (int i=0; i<samples; i++)
sum[i] = (int)(sum[i]/product+0.5);
return sum;
}
/*boolean showDialog() {
GenericDialog gd = new GenericDialog("Image Shrink");
gd.addNumericField("X Shrink Factor:", xshrink, 0);
gd.addNumericField("Y Shrink Factor:", yshrink, 0);
gd.showDialog();
if (gd.wasCanceled())
return false;
xshrink = (int) gd.getNextNumber();
yshrink = (int) gd.getNextNumber();
product = xshrink*yshrink;
return true;
}*/
ImageProcessor shrinkFloat(ImageProcessor ip) {
int w = ip.getWidth()/xshrink;
int h = ip.getHeight()/yshrink;
ImageProcessor ip2 = ip.createProcessor(w, h);
for (int y=0; y<h; y++)
for (int x=0; x<w; x++)
ip2.putPixelValue(x, y, getFloatAverage(ip, x, y));
return ip2;
}
float getFloatAverage(ImageProcessor ip, int x, int y) {
double sum = 0.0;
for (int y2=0; y2<yshrink; y2++)
for (int x2=0; x2<xshrink; x2++)
sum += ip.getPixelValue(x*xshrink+x2, y*yshrink+y2);
return (float)(sum/product);
}
}
it is used as
....
image = opener.openImage(source)
Binning binner = new Binning();
binner.setXshrink(shrink);
binner.setYshrink(shrink);
image2 = binner.shrink(image);
FileInfo fileInfo = image2.getFileInfo();
TiffEncoder tiffencoder = new TiffEncoder(fileInfo);
tiffencoder.write(out);
....