Les nouveaux Forums de Radio Ici&Maintenant !
package image.analysis;import java.util.*;public class GaborCalculator {int s = 32; // filter matrix size - s x tint t = 32;int max_m = 5; // 5 scalesint max_n = 4; // 4 orientationsdouble U_l = 0.05; // lower frequencydouble U_h = 0.8; // higher frequencyint a = 2; // (U_h/U_l)^(1/4), scale values 1,2,4,8,16public GaborCalculator() {}/*** This function returns filter matrix for given scale and orientation.* There will be as many such matrices, as there are scales and orientation* combinations (5x4=20). The size of the filter matrix was chosen to be 32x32.* @param m - scale = [0..4]* @param n - orientation = [0..3]* @return*/public ComplexNumber[][] getFilterMatrix(int m, int n) { // m - scale, n - orientationComplexNumber[][] filterMatrix = new ComplexNumber[s][t];for (int curr_s=0; curr_s<s; curr_s++) {for (int curr_t=0; curr_t<t; curr_t++) {filterMatrix[curr_s][curr_t] = calculatePhi(curr_s, curr_t,m,n);//filterMatrix[curr_s][curr_t] = calculatePhi(curr_s - s/2, curr_t -t/2,m,n);}}return filterMatrix;}/*** This functions calculates Phi(x,y). Result is a complex number.* @param x - Phi argument* @param y - Phi argument* @param m - given scale* @param n - given orientation* @return*/public ComplexNumber calculatePhi(int x, int y, int m, int n) {int a_pow_m = 1;for (int i=0; i<m; i++)a_pow_m = a_pow_m * a;double angel = n*Math.PI/max_n;double _x = ( x*Math.cos(angel) + y*Math.sin(angel))/a_pow_m;double _y = (-x*Math.sin(angel) + y*Math.cos(angel))/a_pow_m;//double sigma_x=(a+1)*Math.sqrt(2*Math.log(2))/(2*Math.PI*a_pow_m*(a-1)*U_l); old versiondouble sigma_x = (a+1)*Math.sqrt(2*Math.log(2))/(2*Math.PI *(a-1)*U_h);double sigma_y = 2*Math.PI*Math.tan(Math.PI/2/max_n)*Math.sqrt(U_h*U_h/(2*Math.log(2)) - 1/(4*Math.PI*sigma_x*Math.PI*sigma_x));sigma_y = 1/sigma_y;double multiplier = 2*Math.PI*sigma_x*sigma_y*Math.exp((_x*_x/sigma_x/sigma_x + _y*_y/sigma_y/sigma_y)/2);multiplier = 1/multiplier/a_pow_m; // Phi m,n = a^-m * Phi(x',y')//angel=2*Math.PI*a_pow_m*U_l*_x; old versionangel = 2*Math.PI* U_h*_x;return new ComplexNumber( multiplier*Math.cos(angel), -multiplier*Math.sin(angel)); // conjugated complex number}/*** The main function, which calculates the whole features vector if* Gabor filters. The size of the features vector is the number of* combinations between scales and orientations: 5x4=20.* Below goes explanation of what has to be done further* (not implemented yet).* Each element of the vector will be applied (convoluted) to each image pixel.* After that, mean and deviation will be calculated for each feature* for the whole image. So, after convoluting the whole image with* this feature vector, there will be another vector of the same size 20,* each element of which will be [mean, deviation] pair. This final* vector of [mean,deviation] will get into storage during system* training as a 'real', if convoluted image was real, or as a CGI,* if convoluted image was a CGI. After such training on around 1K* of CGIs and 1K of Real images, the system will be able to give an* answer whether the image is real or CGI with a high probability.* To do that the system will calculate the same feature vector of size* 20 for the tested image, and will look for the closest feature vector* in the trained storage, using the K-NN algorithm. If the closest* vector is from Real image, then the questioned image is real, if* it is from CGI image, then the questioned image is CGI.* @return*/public List<ComplexNumber[][]> getFiltersForFeatures() {List<ComplexNumber[][]> result = new ArrayList<ComplexNumber[][]>();for (int curr_m=0; curr_m<max_m; curr_m++)for (int curr_n=0; curr_n<max_n; curr_n++) {System.out.println("Got filter " + curr_m + "" + curr_n);result.add(getFilterMatrix(curr_m,curr_n));}return result;}
package image.analysis;import java.text.DecimalFormat;public class ComplexNumber {private double realis;private double imaginalis;// output number formatprivate static DecimalFormat dformat = new DecimalFormat("0.0000");public ComplexNumber(double real, double img) {realis = real;imaginalis = img;}public double getImaginalis() {return imaginalis;}public void setImaginalis(double imaginalis) {this.imaginalis = imaginalis;}public double getRealis() {return realis;}public void setRealis(double realis) {this.realis = realis;}public double getModule() {return Math.sqrt(getRealis()*getRealis() + getImaginalis()*getImaginalis());}public String getModuleStr() {return dformat.format(this.getModule());}public String getRealisStr() {return dformat.format(this.getRealis());}public String getImaginalisStr() {return dformat.format(this.getImaginalis());}public String toString() {return "Realis = " + dformat.format(this.getRealis()) + ", Imaginalis = " + dformat.format(this.getImaginalis());}}
public void testPrintFilter() {GaborCalculator gabor = new GaborCalculator();int m=3, n=2;ComplexNumber[][] filter = gabor.getFilterMatrix(m,n);for (int i=0; i<filter.length; i++) {System.out.print(i+1+": ");for (int j=0; j<filter[i].length; j++)//System.out.print(filter[i][j].getRealisStr() + " ");System.out.print(filter[i][j].getModuleStr() + " ");System.out.println();}}
S'il s'agit (peut être) de matériel militaire...