/*Diana Negoescu '09 * program 'reads' a road sign using shape context * execution: java Method2 picture_name.jpg */ import java.awt.Color; public class Method2{ //function that computes the luminance of a pixel public static double lum(Color color){ int r=color.getRed(); int g=color.getGreen(); int b=color.getBlue(); return .299*r+.587*g+.114*b; } //function that transforms a picture into a black and white one of same //dimension public static Picture blackwhite(String filename){ Picture source=new Picture(filename); int w=source.width(); int h=source.height(); Picture result=new Picture(w,h); Color black=new Color(0,0,0); Color white=new Color(255, 255, 255); double max=0.0; double min=255.0; double av=0; for (int i=0; iav+50) result.set(j,i,white); else result.set(j,i,black); } } result.show(); return result; } //label each pixel according to which object it belongs to public static void floodfillstack(Picture pic, int[][] a, Stack s, Color c, int k){ while (!s.isEmpty()){ Stack.Node x=s.pop(); if ((x.i>=pic.height())||(x.j>=pic.width())||(x.i<0)||(x.j<0)){ continue; } Color col=pic.get(x.j,x.i); if (col.equals(Color.BLACK)) continue; if (a[x.i][x.j]!=0) continue; s.push(x.i+1,x.j); s.push(x.i,x.j-1); s.push(x.i-1,x.j); s.push(x.i,x.j+1); a[x.i][x.j]=k; } } static boolean less(double x, double y) { return (x < y); } static void exch(Letter[] a,int[] mmin, int i, int j) { Letter swapl = a[i]; a[i] = a[j]; a[j] = swapl; int swap=mmin[i]; mmin[i]=mmin[j]; mmin[j]=swap; } static int partition(Letter[] a, int[] mmin, int left, int right) { int i = left - 1; int j = right; while(true) { while (less(mmin[++i], mmin[right])) // left pointer if (i == right) break; while (less(mmin[right], mmin[--j])) // right pointer if (j == left) break; if (i >= j) break; // pointers cross? exch(a, mmin, i, j); // swap left and right } exch(a, mmin, i, right); // swap pivot return i; } public static void quicksortgen(Letter[] a, int b, int i, int[] mmin) { quicksort(a, mmin, b, i); } private static void quicksort(Letter[] a,int[] mmin, int left,int right){ if (right <= left) return; int i = partition(a, mmin, left, right); quicksort(a, mmin, left, i-1); quicksort(a, mmin, i+1, right); } //function that extracts the letters from a 'flood' matrix public static Letter[] extract(int[][] a, int h, int w, int k){ //create an array of letters Letter[] letters=new Letter[k-1]; int[] mmin=new int[k-1]; int[] jmin=new int[k-1]; int[] jmax=new int[k-1]; for (int i=1; ijmax[i-1])) jmax[i-1]=j; if ((a[j][m]==i)&&(mmmax)) mmax=m; } } //create a smaller matrix just fitting the letter int[][] x=new int[jmax[i-1]-jmin[i-1]+1][mmax-mmin[i-1]+1]; //put 1's where the letter's pixels were for (int j=0; j=x.length+7)||(m<7)||(m>=x[0].length+7)) matbig[j][m]=0; else matbig[j][m]=x[j-7][m-7]; } } letters[i-1]=new Letter(matbig); } int b=0; //sort the letters for (int i=0; i=0)&&(ii=0)&&(jjmax) max=gradmagn[i][j]; if (gradmagn[i][j]Math.PI/2) graddir[i][j]-=Math.PI; //'kill' the pixels that are not edges if ((graddir[i][j]<-3*Math.PI/8.0) || (3*Math.PI/8<=graddir[i][j])) { if ((gradmagn[i][j]max) max=rough_edge[i][j]; if (rough_edge[i][j]thigh*(max-min))|| (rough_edge[i][j-1]-min>thigh*(max-min))|| (rough_edge[i+1][j]-min>thigh*(max-min))|| (rough_edge[i-1][j]-min>thigh*(max-min))|| (rough_edge[i+1][j+1]-min>thigh*(max-min))|| (rough_edge[i-1][j-1]-min>thigh*(max-min))|| (rough_edge[i-1][j+1]-min>thigh*(max-min))|| (rough_edge[i+1][j-1]-min>thigh*(max-min)))){ rough_edge[i][j]=0; } } } } for (int i=0; i=0)&&(ii=0)&&(jjm[0].length; j++){ s=s+m[i][j]; } } return s; } public static void main(String[] args){ String filename="database.txt"; In in=new In(filename); //read the database file int N=in.readInt(); int h=in.readInt(); int w=in.readInt(); Letter[] reference=new Letter[N]; for (int i=0; i=m.length+7)||(k<7)||(k>=m[0].length+7)) matbig[j][k]=0; else matbig[j][k]=m[j-7][k-7]; } } reference[i]=new Letter(matbig, c.charAt(0)); } Letterpic[] referencepic=new Letterpic[N]; for (int i=0; i