import ij.*;
import ij.gui.GenericDialog;
import ij.gui.NewImage;
import ij.plugin.FolderOpener;
import ij.plugin.filter.PlugInFilter;
import static ij.plugin.filter.PlugInFilter.DOES_8G;
import static ij.plugin.filter.PlugInFilter.STACK_REQUIRED;
import ij.process.*;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
* Calculates the average values of pixels located at the same
* position in each slice of the stack and adds a slice showing the average values to the end of the stack.
public class Recon_2d implements PlugInFilter {
protected ImagePlus imp;
int Bin_number, Bin_size;
int[] average ;
public int setup(String arg, ImagePlus imp) {
this.imp = imp;
return DOES_8G+STACK_REQUIRED;
}
public void run(ImageProcessor ip) {
GenericDialog gd0 = new GenericDialog("Parameters");
gd0.addNumericField("Bin_Number", Bin_number,0);
gd0.addNumericField("Bin_Size", Bin_size,0);
gd0.showDialog();
if (gd0.wasCanceled()) return ;
Bin_number = (int)gd0.getNextNumber();
Bin_size = (int)gd0.getNextNumber();
ImageStack stack = imp.getStack();
int[] sum;
int[] [] recon = null;
// takes pixels of one slice
byte[] pixels;
int dimension = ip.getWidth()*ip.getHeight()*Bin_number;
sum = new int[dimension];
int width=ip.getWidth();
int height=stack.getSize();
int l=0;
for (int i=1;i<=stack.getSize();i++) {
pixels = (byte[]) stack.getPixels(i);
// add the value of each pixel and the corresponding position of the sum array
for (int j=0;j<stack.getWidth();j++) {
for (int k=0;k<Bin_number;k++){
for( l=0;l<Bin_size;l++){
sum[l]+=0xff & pixels[l];
}
}
}
}
average =new int[dimension];
// divide each entry by the number of pixels in each bin
for ( l=0;l<dimension;l++) {
average[l] = (byte) ((sum[l]/Bin_size) & 0xff);
}
try{
int [][] PixelArray=new int[width][height];
BufferedImage bufferImage2=new BufferedImage(width, height,BufferedImage.TYPE_BYTE_GRAY);
for(int b=0;b<Bin_number;b++){
for(int y=0;y<height;y++){
for(int x=0;x<width;x++){
int Pixel=PixelArray[x][y]<<16 | PixelArray[x][y] << 8 | PixelArray[x][y];
bufferImage2.setRGB(x, y, PixelArray[x][y]);
}
}
File outputfile = new File("D:\\Output\\new"+b+".jpg");
ImageIO.write(bufferImage2, "jpg", outputfile);
}
}
catch(Exception ee){
ee.printStackTrace();
}
}
}
我想创建Bin_number
个大小为width*height
灰度的2D图像。但是空白图像正在创建中。最初,我将有height
个大小为width*l
的图片。我必须将这个大小为width*l*height
的堆栈重新定义为大小为width*height*bin_number
的2D图像堆栈,其中bin_number
是用户输入。 Bin_size
也是用户输入。例如,如果width = 1024,l = 1024,高度或输入堆栈大小,换句话说输入图像是100的大小1024*1024
,则可以获得8个bin_size 128,在这种情况下输出的数量2D图像将为8.因此2D输出图像将是128个大小1024*100
。