KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > instruments > PTTemplate


1 package JSci.instruments;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7 import javax.swing.event.*;
8 import javax.swing.border.*;
9 import java.text.*;
10
11 /** Find a given template in a two dimensional image. Usage:
12  * press <strong>Add</strong> button; the image stops; select a template with the inner
13  * rectangle; press <strong>Done</strong>. The tracking starts immediately. If you want to
14  * track another object, simply repeat the procedure. Select an object
15  * number before <strong>Remove</strong>.
16  */

17 public class PTTemplate implements ImageFilter,ParticleTracker {
18
19     private static long startTime = System.currentTimeMillis();;
20     private TwoROI theROI = null;
21     private Image stillImage=null;
22     private boolean catchImage=false;
23     private JComboBox crossCombo = new JComboBox();
24
25     /** @param r a TwoROI is needed. The inner rectangle represents the
26      * template to be searched for; the outer represents the region in which the template is searched.
27      */

28     public PTTemplate(TwoROI r) {
29     theROI = r;
30     }
31
32     private ImageSink sink;
33     public void setSink(ImageSink fs) {
34         if (sink!=fs) {
35             sink=fs;
36             sink.setSource(this);
37         }
38     }
39
40     private ImageSource source = null;
41     public void setSource(ImageSource fs) {
42         if (source!=fs) {
43             source=fs;
44             source.setSink(this);
45         }
46     }
47
48     public int getWidth() { return source.getWidth(); }
49     public int getHeight() { return source.getHeight(); }
50     public Dimension getSize() { return source.getSize(); }
51
52
53     /** defines the name of the filter */
54     public String JavaDoc getName() { return "PT Template"; }
55
56     private ParticleTrackerListener ptl = null;
57     public void setListener(ParticleTrackerListener ptl) {
58     this.ptl=ptl;
59     }
60
61     public String JavaDoc toString() {
62     return "Particle Tracking - Template; startTime="+startTime;
63     }
64
65     /** find the bead */
66     public void receive(Image f) {
67     ListModel l = crossCombo.getModel();
68     if (l.getSize()>=0) {
69         int[] n=new int[l.getSize()];
70         double[] x=new double[l.getSize()];
71         double[] y=new double[l.getSize()];
72         for (int j=0;j<l.getSize();j++) {
73         PTTemplateCross cross = (PTTemplateCross)l.getElementAt(j);
74         cross.find(f);
75         n[j]=cross.getN();
76         x[j]=cross.getX();
77         y[j]=cross.getY();
78         }
79         if (ptl!=null) ptl.receivePosition(f.getTimeStamp()-startTime,n,x,y,null);
80     }
81     if (catchImage && stillImage==null) { stillImage=f; }
82     if (sink!=null) {
83         if (stillImage!=null) sink.receive(stillImage);
84         else sink.receive(f);
85     }
86     }
87     
88     JComponent comp;
89     /** defines the Component that controls this filter */
90     public Component getControlComponent() {
91     if (comp!=null) return comp;
92     JPanel comp = new JPanel();
93     // comp.setLayout(new BorderLayout());
94
// add buttons
95
final JButton addButton = new JButton("Add");
96     comp.add(addButton);
97     addButton.addActionListener(new ActionListener() {
98         public void actionPerformed(ActionEvent e) {
99             if (addButton.getText().equals("Add")) {
100             stillImage=null;
101             catchImage=true;
102             addButton.setText("Done");
103             }
104             else {
105             catchImage=false;
106             addButton.setText("Add");
107             if (stillImage!=null) {
108                 PTTemplateCross cross = new PTTemplateCross((Rectangle)theROI.getShape(),theROI.getOuterRectangle(),stillImage.getSubImage((Rectangle)theROI.getShape()));
109                 crossCombo.addItem(cross);
110             }
111             stillImage=null;
112             }
113         }
114         });
115     
116     // remove button
117
JButton removeButton = new JButton("Remove");
118     comp.add(removeButton);
119     removeButton.addActionListener(new ActionListener() {
120         public void actionPerformed(ActionEvent e) {
121             crossCombo.removeItem(crossCombo.getSelectedItem());
122         }
123         });
124     // combo box
125
crossCombo.setEditable(false);
126     comp.add(crossCombo);
127     // TwoROI component
128
comp.add(theROI.getControlComponent());
129     // return
130
Border etched = BorderFactory.createEtchedBorder();
131     Border titled = BorderFactory.createTitledBorder(etched,getName());
132     comp.setBorder(titled);
133     return comp;
134     }
135
136
137
138     public static void main(String JavaDoc[] args) {
139     TwoROI r = new TwoROI();
140     PTTemplate f = new PTTemplate(r);
141     Player p = new Player();
142     ImageSource fs = new SimulatedBarycentreSource();
143     fs.setSink(f);
144     f.setSink(p);
145     p.addROI(r);
146     p.start();
147
148     final NumberFormat formatter = NumberFormat.getNumberInstance();
149     { formatter.setMaximumFractionDigits(3);
150     formatter.setMinimumFractionDigits(1);
151     formatter.setMinimumIntegerDigits(1);
152     formatter.setMaximumIntegerDigits(4);
153     }
154     
155     ParticleTrackerListener ptl=new ParticleTrackerListener() {
156         public void receivePosition(long time,int[] n,double[] x,double[] y,double[] z) {
157             System.out.print(time);
158             for (int j=0;j<n.length;j++)
159             System.out.print(" "+n[j]+" "+formatter.format(x[j])+" "+formatter.format(y[j]));
160             System.out.println();
161         }
162         };
163     
164     f.setListener(ptl);
165
166     }
167
168
169
170
171
172
173
174
175
176
177
178 /////////////////////////////////////////////////////////////////////////////
179
// Simulated - for testing
180

181 static class SimulatedBarycentreSource implements ImageSource,Runnable JavaDoc {
182
183     Dimension dim = new Dimension(130,97);
184
185     public SimulatedBarycentreSource() {
186     Thread JavaDoc t = new Thread JavaDoc(this);
187     t.setDaemon(true);
188     t.start();
189     }
190
191     private ImageSink sink;
192     /** set the object that must read the frames through receive() method.
193      * @param fs the object that reads the frames
194      */

195     public void setSink(ImageSink fs) {
196     if (sink!=fs) {
197         sink=fs;
198         sink.setSource(this);
199     }
200     }
201
202
203     private static final double SIGMA = 4.0;
204     private static final double DELTAX = 4.0;
205     private static final double DELTAY = 2.0;
206     private double f(double x,double y) {
207     return
208         Math.exp(-(x*x+y*y)/(2.0*SIGMA*SIGMA))-
209         Math.exp(-((x-DELTAX)*(x-DELTAX)+(y-DELTAY)*(y-DELTAY))/(2.0*SIGMA*SIGMA));
210     }
211
212     public void run() {
213     int num = 0;
214     while (true) {
215         final int n = num++;
216         final long t = System.currentTimeMillis();
217         final double x0 = Math.random()*4+40.0;
218         final double y0 = Math.random()*4+50.0;
219         final byte[] im = new byte[getWidth()*getHeight()];
220         final Dimension dim = new Dimension(getWidth(),getHeight());
221         Image f = new Image() {
222             public byte[] getData() { return im; }
223             public Dimension getSize() { return dim; }
224             public long getTimeStamp() { return t; }
225         };
226
227         for (int j=0;j<f.getWidth();j++)
228         for (int k=0;k<f.getHeight();k++)
229             f.getData()[j+k*f.getWidth()] = (byte) (128.0+128.0*SimulatedBarycentreSource.this.f(j-x0,k-y0));
230
231         if (sink!=null) sink.receive(f);
232         try { Thread.sleep(100); }
233         catch (InterruptedException JavaDoc e) {}
234     }
235     }
236
237     /** @return the width of the image */
238     public int getWidth() { return dim.width; }
239     
240     /** @return the height of the image */
241     public int getHeight() { return dim.height; }
242     
243     /** @return the dimension of the image */
244     public Dimension getSize() { return dim; }
245
246     /** @return the component that controls the object that implements
247     this interface */

248     public Component getControlComponent() {
249     JPanel p = new JPanel();
250     p.add(new JLabel("Test"));
251     Border etched = BorderFactory.createEtchedBorder();
252     Border titled = BorderFactory.createTitledBorder(etched,"source");
253     p.setBorder(titled);
254     return p;
255     }
256
257 }
258 }
259
Popular Tags