import java.awt.*; import java.io.*; import ij.*; import ij.gui.*; import ij.process.*; import ij.text.*; import ij.plugin.PlugIn; //This plugin correlates two 8-bit images or stacks. The resultant correlation plot is a //stack with the same number of slices as the stack with the fewer number. //Many correlation plots need to be recontrasted. Pressing Apple/Shift/C will //bring up the Brightness/Contrast menu. public class Image_Correlator implements PlugIn { private static int index1; private static int index2; private static boolean displayCounts, collate; private ImageStack img1, img2; private int smallest; private int z1, z2, count; public void run(String arg) { if (showDialog()) correlate(img1, img2); } public boolean showDialog() { int[] wList = WindowManager.getIDList(); if (wList==null) { IJ.noImage(); return false; } String[] titles = new String[wList.length]; for (int i=0; i=titles.length)index1 = 0; if (index2>=titles.length)index2 = 0; GenericDialog gd = new GenericDialog("Image Correlator"); gd.addChoice("Image1: ", titles, titles[index1]); gd.addChoice("Image2: ", titles, titles[index2]); gd.addCheckbox("Display Counts: ", displayCounts); gd.addCheckbox("Collate Z-Data (Stacks Only):", collate); gd.showDialog(); if (gd.wasCanceled()) return false; index1 = gd.getNextChoiceIndex(); index2 = gd.getNextChoiceIndex(); displayCounts = gd.getNextBoolean(); collate = gd.getNextBoolean(); String title1 = titles[index1]; String title2 = titles[index2]; //Test to find out if both images are 8-bit grayscale. ImagePlus sliceimg1 = WindowManager.getImage(wList[index1]); ImagePlus sliceimg2 = WindowManager.getImage(wList[index2]); if (sliceimg1.getType()!=sliceimg1.GRAY8 || sliceimg2.getType()!=sliceimg1.GRAY8) { IJ.showMessage("Image Correlator", "Both stacks must be 8-bit grayscale."); return false; } img1 = sliceimg1.getStack(); img2 = sliceimg2.getStack(); return true; } public Object[] getCorrelation(ImagePlus im1, ImagePlus im2){ int width = im1.getWidth(); int height = im1.getHeight(); float[] v1, v2; ImageProcessor ip1 = im1.getProcessor(); ImageProcessor ip2 = im2.getProcessor(); v1 = new float[width*height]; v2 = new float[width*height]; ImageProcessor plot= new FloatProcessor(256, 256); int index = 0; for (int y=0; y1) && (i2.getSize()>1)) return true; else return false; } void displayCounts(ImageStack plot) { StringBuffer sb = new StringBuffer(); int count; int slices = plot.getSize(); if (slices==1) { ImageProcessor ip = plot.getProcessor(1); for (int x=0; x<256; x++) for (int y=255; y>=0; y--) { count = (int)ip.getPixelValue(x,y); if (count>0) sb.append(x+"\t"+(255-y)+"\t"+count+"\n"); } new TextWindow("Non-zero Counts" , "X\tY\tCount", sb.toString(), 300, 400); } else { for (int slice=1; slice<=slices; slice++) { ImageProcessor ip = plot.getProcessor(slice); for (int x=0; x<256; x++) for (int y=255; y>=0; y--) { count = (int)ip.getPixelValue(x,y); if (count>0) sb.append(x+"\t"+(255-y)+"\t"+(slice-1)+"\t"+count+"\n"); } } new TextWindow("Non-zero Counts" , "X\tY\tZ\tCount", sb.toString(), 300, 400); } } }