1 package JSci.instruments; 2 3 import java.awt.*; 4 import java.io.*; 5 6 9 class PTTemplateCross { 10 11 12 public static double REGION_SPEED = 0.2; 13 14 private int number; 15 private Rectangle position; 16 private Rectangle searchArea; 17 private Image templateImage; 18 private double templateBckgrnd; 19 private double[][] corr; 20 private static int nextN = 0; 21 22 private double x; 23 private double y; 24 private double ox; private double oy; 26 private double cx; private double cy; 28 29 private int fromImg(byte c) { if (c>=0) return (int)c; else return (int)(256+c); } 30 31 36 public PTTemplateCross(Rectangle p, Rectangle s, Image i) { 37 number = nextN++; 39 position = (Rectangle)p.clone(); 41 searchArea = (Rectangle)s.clone(); 42 ox=position.x-searchArea.x; 43 oy=position.y-searchArea.y; 44 cx=searchArea.x; 45 cy=searchArea.y; 46 templateImage = i; 48 int m,n; 50 templateBckgrnd = 0.0; 51 for (m=0;m<templateImage.getWidth();m++) 52 for (n=0;n<templateImage.getHeight();n++) 53 templateBckgrnd+=fromImg(templateImage.getData()[templateImage.getOffset()+m+n*templateImage.getScansize()]); 54 templateBckgrnd/=templateImage.getWidth()*templateImage.getHeight(); 55 corr = new double[searchArea.width-templateImage.getWidth()] 57 [searchArea.height-templateImage.getHeight()]; 58 } 59 60 64 public void find(Image img) { 65 66 double max=-100.0; 67 int maxx = 0; 68 int maxy = 0; 69 70 for (int j=0;j<corr.length;j++) for (int k=0;k<corr[0].length;k++) { 71 corr[j][k]=0.0; 72 for (int m=0;m<templateImage.getWidth();m++) for (int n=0;n<templateImage.getHeight();n++) 73 corr[j][k]+= 74 fromImg(img.getData()[img.getOffset()+(j+m+searchArea.x)+(k+n+searchArea.y)*img.getScansize()])* 75 (fromImg(templateImage.getData()[templateImage.getOffset()+m+n*templateImage.getScansize()])-templateBckgrnd); 76 if (corr[j][k]>max) { 77 maxx=j; 78 maxy=k; 79 max=corr[j][k]; 80 } 81 } 82 83 double Dx,Dy; 84 if (maxx==0 | maxy==0 | maxx==corr.length-1 | maxy==corr[0].length-1) {Dx=Dy=0;} 85 else { 86 double cxp=(corr[maxx+1][maxy]-corr[maxx-1][maxy])/2.0; 87 double cyp=(corr[maxx][maxy+1]-corr[maxx][maxy-1])/2.0; 88 double cxpyp=(corr[maxx+1][maxy+1]-corr[maxx+1][maxy-1]-corr[maxx-1][maxy+1]+corr[maxx-1][maxy-1])/4.0; 89 double cxpp=-2.0*corr[maxx][maxy]+corr[maxx+1][maxy]+corr[maxx-1][maxy]; 90 double cypp=-2.0*corr[maxx][maxy]+corr[maxx][maxy+1]+corr[maxx][maxy-1]; 91 double Det=cxpp*cypp-cxpyp*cxpyp; 92 Dx=-(cxp*cypp-cyp*cxpyp)/Det; 93 Dy=-(cxpp*cyp-cxpyp*cxp)/Det; 94 } 95 96 x=searchArea.getMinX()+maxx+Dx; 97 y=searchArea.getMinY()+maxy+Dy; 98 99 cx+=(x-searchArea.x-ox)*REGION_SPEED; 100 cy+=(y-searchArea.y-oy)*REGION_SPEED; 101 if (cx<0) cx=0; 102 if (cx+searchArea.width>=img.getWidth()) cx=img.getWidth()-1-searchArea.width; 103 if (cy<0) cy=0; 104 if (cy+searchArea.height>=img.getHeight()) cy=img.getHeight()-1-searchArea.height; 105 106 position.setLocation((int)Math.rint(x),(int)Math.rint(y)); 107 searchArea.setLocation((int)Math.rint(cx),(int)Math.rint(cy)); 108 109 img.addOverlay(new Overlay() { 110 public void paint(Graphics g) { 111 Graphics2D g2 = (Graphics2D)g; 112 g2.setColor(Color.RED); 113 g2.draw(position); 114 g2.setColor(Color.MAGENTA); 115 g2.draw(searchArea); 116 g2.drawString(PTTemplateCross.this.toString(),searchArea.x,searchArea.y+searchArea.height+12); 117 } 118 }); 119 120 } 121 122 125 public int getN() {return number;} 126 127 128 public String toString() { return ""+number; } 129 130 133 public double getX() {return x;} 134 135 138 public double getY() {return y;} 139 140 } 141 | Popular Tags |