/****************************************************/ /* Basic Intensity Crrection for 3D Stack of Images */ /******************* ChloƩ Murtin *******************/ /************* The University of Tokyo **************/ /*********************** 2014 ***********************/ /****************************************************/ import ij.*; import ij.plugin.filter.*; import ij.process.*; import ij.gui.*; import java.awt.*; import java.awt.event.*; import ij.plugin.PlugIn; import java.util.*; import java.awt.TextField.*; import java.lang.*; public class Progressive_Intensity_and_Gamma_Correction implements PlugInFilter, ActionListener, DialogListener, ItemListener {//, TextComponent { //ItemListener { public void itemStateChanged(ItemEvent e) { //Write your method here } public void actionPerformed(ActionEvent e) { IJ.run("Histogram", "stack"); //code that reacts to the action... } public void adjustmentValueChanged(AdjustmentEvent e){ //double offset = e.getValue(); //Object target = e.getSource(); } public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) { Vector v = gd.getNumericFields(); IJ.log("test " + v.get(0).getClass() ); //IJ.log("test " + v.get(0).getText() + " " + v.get(1).toString()); //IJ.log("test " + v); /*text = gd.getNextString(); MultiLineLabel label = (MultiLineLabel)gd.getMessage(); label.setText(divider+"\n"+text+"\n"+divider);*/ return true; } private ImagePlus imp; private double myEnhanc = 100.0; private int myEnhanc_max = 4095; private double ge = 3; private double gg = 3; private String[] interpolationMethodList; private String[] interpolationMethodListG; private String interpolationMethodI; private String interpolationMethodG; private boolean norm_bool = false; private double gamma = 1.0; private int threshold = 0; public int setup(String arg, ImagePlus imp){ IJ.register (Progressive_Intensity_and_Gamma_Correction.class); if (IJ.versionLessThan("1.32c")) return DONE; return DOES_ALL+SUPPORTS_MASKING; } public void run(ImageProcessor ip){ interpolationMethodList = new String[ 3 ]; interpolationMethodList[0] = "Constant Enhancement"; interpolationMethodList[1] = "Linear Enhancement"; interpolationMethodList[2] = "Exponential Enhancement"; interpolationMethodListG = new String[ 3 ]; interpolationMethodListG[0] = "Constant Correction"; interpolationMethodListG[1] = "Linear Correction"; interpolationMethodListG[2] = "Exponential Correction"; /** Image Choice Dialog Box */ final int[] ids = WindowManager.getIDList(); if ( ids == null || ids.length < 1 ) { IJ.showMessage( "You should have at least one image open." ); return; } final String[] titles = new String[ ids.length ]; for ( int i = 0; i < ids.length; ++i ) { titles[ i ] = ( WindowManager.getImage( ids[ i ] ) ).getTitle(); } Font myfont = new Font("SansSerif", Font.BOLD, 12); final String current = WindowManager.getCurrentImage().getTitle(); final GenericDialog gd = new GenericDialog( "Progressive Intensity and Gamma Correction" ); /** Image Choice */ gd.addMessage( "* Image Selection *", myfont ); gd.addChoice( "Image_Stack", titles, current ); gd.addMessage( "* Intensity Correction *", myfont ); gd.addNumericField( "Intensity_Enhancement", myEnhanc, 1, 6, "%" ); //gd.addNumericField( "Maximum Value", myEnhanc_max, 1, 6, "(0-255)" ); gd.addChoice( "Interpolation_Method", interpolationMethodList, current ); gd.addNumericField( "Exponential Growth Factor", ge, 1, 6, "" ); gd.addMessage( "* Gamma Correction *", myfont ); gd.addNumericField( "Gamma_Correction", gamma, 1, 6, "" ); gd.addChoice( "Gamma_Interpolation_Method", interpolationMethodListG, current ); gd.addNumericField( "Exponential Growth Factor", gg, 1, 6, "" ); gd.addMessage( "* Other Corrections *", myfont ); gd.addNumericField( "Threshold", threshold, 0, 0, "" ); gd.addCheckbox( "Normalization", norm_bool ); gd.showDialog(); if (gd.wasCanceled()) return; final GenericDialog gd2 = new GenericDialog( "Warning" ); gd2.addMessage( "No undo possible. Continue?" ); gd2.showDialog(); if (gd2.wasCanceled()) return; imp = WindowManager.getImage( ids[ gd.getNextChoiceIndex() ] ); myEnhanc = gd.getNextNumber(); interpolationMethodI = gd.getNextChoice(); ge = gd.getNextNumber(); gamma = gd.getNextNumber(); interpolationMethodG = gd.getNextChoice(); gg = gd.getNextNumber(); threshold = (int) gd.getNextNumber(); norm_bool = gd.getNextBoolean(); ip = imp.getProcessor(); int Zmax = imp.getImageStackSize(); //if ( gamma == 0 || gamma > 5 ) if ( gamma == 0 ) { gamma = 1/gamma; IJ.showMessage( "Incorrect Value of Gamma" ); return; } gamma = 1/gamma; /** Intensity Correction */ if ( myEnhanc != 100){ double a; double b; if (interpolationMethodI == "Linear Enhancement"){ a = (100 - myEnhanc)/(1 - Zmax); b = 100 - a; for (int z = 1; z <= Zmax; z++){ imp.setSlice(z); ip.multiply((a*z+b)/100); } } if (interpolationMethodI == "Exponential Enhancement"){ //a = (myEnhanc - 100)/(Math.exp(Zmax) - Math.E); //b = 100 - a*Math.E; a = (myEnhanc - 100)/(Math.pow(Zmax,ge) - 1); b = 100 - a; for (int z = 1; z <= Zmax; z++){ imp.setSlice(z); //ip.multiply((a*Math.exp(z)+b)/100); ip.multiply((a*Math.pow(z,ge) + b)/100); } } if (interpolationMethodI == "Constant Enhancement"){ for (int z = 1; z <= Zmax; z++){ imp.setSlice(z); ip.multiply(myEnhanc/100); } } } /** Normalization */ double multValue; double max; double min; int bd = imp.getBitDepth(); double range = Math.pow(2,bd)-1; if (norm_bool){ for (int z = 1; z <= Zmax; z++){ imp.setSlice(z); max = ip.getStatistics().max; min = ip.getStatistics().min; multValue = range/(max-min); ip.subtract(min); ip.multiply(multValue); } } /** Gamma Correction */ if ( gamma != 1){ double A; double B; if (interpolationMethodG == "Linear Correction"){ A = (gamma - 1)/(Zmax - 1); B = 1 - A; for (int z = 1; z <= Zmax; z++){ imp.setSlice(z); ip.gamma(A*z+B); } } if (interpolationMethodG == "Exponential Correction"){ //A = (gamma - 1)/(Math.exp(Zmax) - Math.E); //B = 1 - A*Math.E; A = (gamma - 1)/(Math.pow(Zmax,gg) - 1); B = 1 - A; for (int z = 1; z <= Zmax; z++){ imp.setSlice(z); //ip.gamma(A*Math.exp(z)+B); ip.gamma((A*Math.pow(z,gg) + B)); //////////////////// } } if (interpolationMethodG == "Constant Correction"){ for (int z = 1; z <= Zmax; z++){ imp.setSlice(z); ip.gamma(gamma); } } } /** Thresholding */ if ( threshold != 0 ) { // do thresholding for (int z = 1; z <= Zmax; z++){ imp.setSlice(z); ImageProcessor mask = ip.duplicate(); mask.threshold(threshold); mask.invert(); ip.fill(mask); } } imp.updateAndRepaintWindow(); return; } }