import java.awt.*; import java.awt.image.BufferedImage; import java.awt.event.*; import java.util.Calendar; import java.text.*; import java.util.Random; import ij.*; import ij.gui.*; import ij.process.*; import ij.plugin.filter.PlugInFilter; import ij.text.*; /* * Phase Ratio analyzer performs following tasks * 1, get 250 (or a user set number) random points from a picture, * 2, each time, display a pop up window with magnified (x4, 16x16 pixels) picture around the selected point and ask if the user think the center of the picture is low or high * 3, remember low or high selection from the user * 4, give the ratio between low and high after all points */ public class Phase_Ratio_Analyzer implements PlugInFilter { ImagePlus imp; static int num = 250; static int cropSize = 16; String name = "Phase Ratio Analyzer"; public int setup(String args, ImagePlus imp) { if(args.equals("about")) { IJ.showMessage(name); return DONE; } if (IJ.versionLessThan("1.41l")) return DONE; if(imp != null) this.imp = imp; GenericDialog param = new GenericDialog(name, IJ.getInstance()); param.addNumericField("Number of Points:", num, 0); param.addNumericField("Crop size (less than 50):", cropSize, 0 ); param.showDialog(); if(!param.wasCanceled()) { num = (int)param.getNextNumber(); cropSize = (int)param.getNextNumber(); if( cropSize > 50 ){ IJ.showMessage("Crop size is too big,\nreset to 50pixel."); cropSize = 50; } } return DOES_ALL; } public void run(ImageProcessor ip) { RatioAnalyzerWindow raWindow = new RatioAnalyzerWindow( imp, num, cropSize); raWindow.makeChoice(); } } class RatioAnalyzerWindow extends Frame implements ActionListener{ BufferedImage oi; Button buttonA; Button buttonB; Button buttonStop; Panel southContainer; String fileName; static TextWindow rWindow = new TextWindow("Phase","", 400, 300); static TextPanel tp = rWindow.getTextPanel(); static{ tp.setColumnHeadings("Name\tBright cliks\tDark clicks\tTotal clicks\tBright Percentage\tDark Percentge"); } double A = 0f, B = 0f; int n = 0; int nsteps = 0; int cropSize = 0; Random gen = new Random( Calendar.getInstance().getTimeInMillis() % 100000l ); public RatioAnalyzerWindow(ImagePlus _ip, int _nsteps, int _cropSize){ super( "Analyer Window"); oi = _ip.getBufferedImage(); nsteps = _nsteps; cropSize = _cropSize; fileName = _ip.getOriginalFileInfo().fileName; buttonA = new Button( "Bright" ); buttonB = new Button( "Dark" ); buttonStop = new Button( "Stop" ); buttonA.addActionListener( this); buttonB.addActionListener( this ); buttonStop.addActionListener( this ); this.setSize( 300, 300 ); southContainer = new Panel(); southContainer.setLayout( new FlowLayout() ); southContainer.add( buttonA ); southContainer.add( buttonB ); southContainer.add( buttonStop ); this.setLayout( new BorderLayout() ); this.add("South", southContainer ); } public void paint( Graphics g ){ int x = gen.nextInt( oi.getWidth() - cropSize - 1 ); int y = gen.nextInt( oi.getHeight() - cropSize - 1 ); int posA = 150 - ( cropSize << 1 ); int crop4 = cropSize << 2; BufferedImage clip = oi.getSubimage( x, y, cropSize, cropSize ); g.drawImage( clip, posA, posA, crop4, crop4, this ); g.setColor( Color.red ); g.drawLine( 10, 150, 148, 150 ); g.drawLine( 152, 150, 290, 150 ); g.drawLine( 150, 10, 150, 148 ); g.drawLine( 150, 152, 150, 250 ); } public void makeChoice(){ ij.gui.GUI.center( this ); this.setVisible( true ); } public void choiceEnd(){ this.setVisible( false ); tp.appendLine( fileName+"\t" + A + "\t" + B + "\t" + ( A + B )+"\t" + (100d * A / ( A + B ) ) + "\t" + ( 100d * B / ( A + B ) ) ); } public void actionPerformed(ActionEvent evt) { if( evt.getSource() == buttonA ){ A += 1f; repaint(); }else if( evt.getSource() == buttonB ){ B += 1f; repaint(); }else if( evt.getSource() == buttonStop ){ this.choiceEnd(); } n ++; if( n == nsteps ){ this.choiceEnd(); } } }