⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 algorithmlda2.java

📁 完整的模式识别库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	    
	    // save the transformation matrix 
	    //
	    LDA3 = W;
	}
	
	size = set4_d.size();
	
	xsize4 += size;
	ysize4 += size;
	
	for (int i = 0; i < size; i++)
	{    
	    MyPoint p = (MyPoint)set4_d.elementAt(i);
	    xval4 += p.x;
	    yval4 += p.y;
	}
	
	// compute the transformation matrix for the fourth data set
	//
	if (size > 0)
	{
	    
	    // declare matrix objects
	    //
	    Matrix T = new Matrix();
	    Matrix M = new Matrix();
	    Matrix W = new Matrix();
	    
	    // allocate memory for the matrix elements
	    //
	    T.Elem = new double[2][2];
	    M.Elem = new double[2][2];
	    W.Elem = new double[2][2];
	    
	    // initialize the transformation matrix dimensions
	    //
	    W.row = 2;
	    W.col = 2;
	    
	    // reset the matrices
	    //
	    W.resetMatrix();
	    
	    // initialize the matrix needed to compute the eigenvalues
	    //
	    T.initMatrix(S4.Elem, 2, 2);
	    
	    // make a copy of the original matrix
	    //
	    M.copyMatrix(T);
	    
	    // compute the eigen values
	    //
	    eigVal4 = Eigen.compEigenVal(T);
	    
	    // compute the eigen vectors
	    //
	    for (int i = 0; i < 2; i++)
	    {
		Eigen.calcEigVec(M, eigVal4[i], eigVec);
		for (int j = 0; j < 2; j++)
		{
		    W.Elem[j][i] = eigVec[j];
		}
	    }
	    
	    // save the transformation matrix 
	    //
	    LDA4 = W;
	}
	
    }

    /**  
     * Determines the within class scatter matrix
     *
     * @param   M Matrix storing within class scatter matrix
     * @see     Matrix
     */
    public void withinClass(Matrix M)
    {
	// declare local variables
	//
	int size = 0;
	double x[] = null;
	double y[] = null;
	
	DisplayScale scale = output_panel_d.disp_area_d.getDisplayScale();
	
	// declare the covariance object
	//
	Covariance cov = new Covariance();
	
	// compute the propabilities of each data set
	//
	double maxsamples = set1_d.size() + set2_d.size() 
	                  + set3_d.size() + set4_d.size();
	
	double p1 = set1_d.size() / maxsamples;
	double p2 = set2_d.size() / maxsamples;
	double p3 = set3_d.size() / maxsamples;
	double p4 = set4_d.size() / maxsamples;
	
	// get the first data set size
	//
	size = set1_d.size();
	
	// initialize arrays to store the samples
	//
	x = new double[size];
	y = new double[size];
	
	// set up the initial random vectors i.e., the vectors of
	// X and Y coordinate points form the display
	//
	for (int i = 0; i < size; i++)
	{
	    MyPoint p = (MyPoint)set1_d.elementAt(i);
	    x[i] = p.x;
	    y[i] = p.y;
	}
	
	// compute the covariance matrix of the first data set
	//
	CLDA.row = CLDA1.col = 2;
	CLDA1.Elem = new double[2][2];
	CLDA1.resetMatrix();
	
	if (size > 0)
	{
	    CLDA1.Elem = cov.computeCovariance(x, y);
	}
	
	// get the second data set size
	//
	size = set2_d.size();
	
	// initialize arrays to store the samples
	//
	x = new double[size];
	y = new double[size];
	
	// set up the initial random vectors i.e., the vectors of
	// X and Y coordinate points form the display
	//
	for (int i = 0; i < size; i++)
        {
	    MyPoint p = (MyPoint)set2_d.elementAt(i);
	    x[i] = p.x;
	    y[i] = p.y;
	}
	
	// compute the covariance matrix of the second data set
	//
	CLDA2.row = CLDA2.col = 2;
	CLDA2.Elem = new double[2][2];
	CLDA2.resetMatrix();
	
	if (size > 0)
	{
	    CLDA2.Elem = cov.computeCovariance(x, y);
	}
	
	// get the third data set size
	//
	size = set3_d.size();
	
	// initialize arrays to store the samples
	//
	x = new double[size];
	y = new double[size];
	
	// set up the initial random vectors i.e., the vectors of
	// X and Y coordinate points form the display
	//
	for (int i = 0; i < size; i++)
	{
	    MyPoint p = (MyPoint)set3_d.elementAt(i);
	    x[i] = p.x;
	    y[i] = p.y;
	}
	
	// compute the covariance matrix of the third data set
	//
	CLDA3.row = CLDA3.col = 2;
	CLDA3.Elem = new double[2][2];
	CLDA3.resetMatrix();
	
	if (size > 0)
	{
	    CLDA3.Elem = cov.computeCovariance(x, y);
	}
	
	// get the fourth data set size
	//
	size = set4_d.size();
	
	// initialize arrays to store the samples
	//
	x = new double[size];
	y = new double[size];
	
	// set up the initial random vectors i.e., the vectors of
	// X and Y coordinate points form the display
	//
	for (int i = 0; i < size; i++)
	{
	    MyPoint p = (MyPoint)set4_d.elementAt(i);
	    x[i] = p.x;
	    y[i] = p.y;
	}
	
	// compute the covariance matrix of the fourth data set
	//
	CLDA4.row = CLDA4.col = 2;
	CLDA4.Elem = new double[2][2];
	CLDA4.resetMatrix();
	
	if (size > 0)
        {
	    CLDA4.Elem = cov.computeCovariance(x, y);
	}
	
	// compute the within class scatter matrix
	//
	M.row = M.col = 2;
	M.Elem = new double[2][2];
	M.resetMatrix();
	M.addMatrix(CLDA1);
	M.addMatrix(CLDA2);
	M.addMatrix(CLDA3);
	M.addMatrix(CLDA4);
	CLDA = M;
    }
    
	//  // method dispalyMatrices
	//  //
	//  // arguments:
	//  //    Data d: input data point
	//  //
	//  // return   : none
	//  //
	//  // display two matrices
	//  //
	//  public void displayMatrices()
	//  {
	//
	//    // declare local variables
	//    //
	//    double a11 = 0.0;
	//    double a12 = 0.0;
	//    double a21 = 0.0;
	//    double a22 = 0.0;
	//
	//    String text;
	//
	//    a11 = MathUtil.setDecimal(cov_matrix_d.Elem[0][0], 2);
	//    a12 = MathUtil.setDecimal(cov_matrix_d.Elem[0][1], 2);
	//    a21 = MathUtil.setDecimal(cov_matrix_d.Elem[1][0], 2);
	//    a22 = MathUtil.setDecimal(cov_matrix_d.Elem[1][1], 2);
	//
	//    text =
	//      new String(
	//        "      Covariance matrix:\n"
	//          + "         "
	//          + a11
	//          + "    "
	//          + a12
	//          + "\n"
	//          + "         "
	//          + a21
	//          + "    "
	//          + a22);
	//
	//    // append message to process box
	//    //
	//    pro_box_d.appendMessage(text);
	//
	//    a11 = MathUtil.setDecimal(trans_matrix_d.Elem[0][0], 2);
	//    a12 = MathUtil.setDecimal(trans_matrix_d.Elem[0][1], 2);
	//    a21 = MathUtil.setDecimal(trans_matrix_d.Elem[1][0], 2);
	//    a22 = MathUtil.setDecimal(trans_matrix_d.Elem[1][1], 2);
	//
	//    text =
	//      new String(
	//        "      Transformation matrix:\n"
	//          + "         "
	//          + a11
	//          + "    "
	//          + a12
	//          + "\n"
	//          + "         "
	//          + a21
	//          + "    "
	//          + a22);
	//
	//    // append message to process box
	//    //
	//    pro_box_d.appendMessage(text);
	//
	//  }
    
    /**
     * method computes the line of discrimination for class dependent LDA 
     */
    public void computeDecisionRegions()
    {
	// Debug
	//
	// System.out.println(algo_id + ": computeDecisionRegions()");
	
	// Setup Local Variables
	//
	MyPoint pt = null;
	double dist = 0.0;
	int associated = 0;
	double smallestSoFar = Double.MAX_VALUE;
	
	int target = 0;
	boolean set1flag = true;
	boolean set2flag = true;
	boolean set3flag = true;
	int counter = 0;
	
	DisplayScale scale = output_panel_d.disp_area_d.getDisplayScale();
	
	double currentX = scale.xmin;
	double currentY = scale.ymin;
	
	// set precision
	//
	int outputWidth = output_panel_d.disp_area_d.getXPrecision();
	int outputHeight = output_panel_d.disp_area_d.getYPrecision();
	
	double incrementY = (scale.ymax - scale.ymin) / outputHeight;
	double incrementX = (scale.xmax - scale.xmin) / outputWidth;
	
	// declare a 2D array to store the class associations
	//
	output_canvas_d = new int[outputWidth][outputHeight];
	
	// loop through each and every point on the pixmap and
	// determine which class each pixel is associated with
	pro_box_d.setProgressMin(0);
	pro_box_d.setProgressMax(outputWidth);
	
	for (int i = 0; i < outputWidth; i++)
	{
	    currentX += incrementX;
	    currentY = scale.ymin;
	    
	    //set current status
	    pro_box_d.setProgressCurr(i);
	    
	    for (int j = 0; j < outputHeight; counter++, j++)
	    {
		
		currentY += incrementY;
		
		// declare the current pixel point
		//
		MyPoint pix = new MyPoint(currentX, currentY);
		
		// convert the pixel to the time domain
		//
		double X[][] = new double[1][2];
		X[0][0] = pix.x;
		X[0][1] = pix.y;
		
		smallestSoFar = Double.MAX_VALUE;
		
		// reset the boolean flags
		//
		set1flag = true;
		set2flag = true;
		set3flag = true;
		
		// find the closest point from the first class
		//
		for (int k = 0; k < point_means_d.size(); k++)
	        {
		    // get the first mean point
		    //
		    pt = (MyPoint)point_means_d.elementAt(k);
		    
		    // convert the mean point to the time domain
		    //
		    double Y[][] = new double[1][2];
		    Y[0][0] = pt.x;
		    Y[0][1] = pt.y;
		    
		    // represent the pixel as a matrix
		    //
		    Matrix A = new Matrix();
		    A.initMatrix(X, 1, 2);
		    
		    // represent the mean point as a matrix
		    //
		    Matrix B = new Matrix();
		    B.initMatrix(Y, 1, 2);
		    
		    // transform the pixel and mean point to
		    // the feature space
		    //
		    Matrix C = new Matrix();
		    Matrix D = new Matrix();
		    
		    // use the transformation matrix of the
		    // first data set
		    //
		    if (set1_d.size() > 0 && set1flag)
		    {
			set1flag = false;
			target = 0;
			A.multMatrix(LDA1, C);
			B.multMatrix(LDA1, D);
		    }
		    
		    // use the transformation matrix of the
		    // second data set
		    //
		    else if (set2_d.size() > 0 && set2flag)
		    {
			set2flag = false;
			target = 1;
			A.multMatrix(LDA2, C);
			B.multMatrix(LDA2, D);
		    }
		    
		    // use the transformation matrix of the
		    // third data set
		    //
		    else if (set3_d.size() > 0 && set3flag)
		    {
			set3flag = false;
			target = 2;
			A.multMatrix(LDA3, C);
			B.multMatrix(LDA3, D);
		    }
		    
		    // use the transformation matrix of the
		    // forth data set
		    //
		    else
		    {
			target = 3;
			A.multMatrix(LDA4, C);
			B.multMatrix(LDA4, D);
		    }
		    
		    // find the distance between the pixel and
		    // mean point
		    //
		    dist = MathUtil.distance(C.Elem[0][0], C.Elem[0][1], 
					     D.Elem[0][0], D.Elem[0][1]);
		    
		    if (dist < smallestSoFar)
		    {
			associated = target;
			smallestSoFar = dist;
		    }
		}
		
		// put and entry in the output canvas array to 
		// indicate which class the current pixel is closest to
		//
		output_canvas_d[i][j] = associated;
		
		// add a point to the vector of decision
		// region points if the class that the current
		// point is associated with is different for
		// the class what the previous point was
		// associated with i.e., a transition point
		//
		if (j > 0 && i > 0)
		{
		    if (associated != output_canvas_d[i][j - 1] || 
			associated != output_canvas_d[i - 1][j])
		    {
			decision_regions_d.add(pix);
		    }
		}
	    }
	}
    }
    
    /**
     * Determines the between class scatter matrix for
     * the class dependent linear discrimination algorithm
     * 
     * @param   M1 between class scatter matrix for the first class
     * @param   M2 between class scatter matrix for the second class
     * @param   M3 between class scatter matrix for the third class
     * @param   M4 between class scatter matrix for the fourth class
     */
    public void betweenClass1(Matrix M1, Matrix M2, Matrix M3, Matrix M4)
    {
	
	// declare local variables
	//
	int size = 0;
	
	double xmean1 = 0.0;
	double ymean1 = 0.0;
	double xmean2 = 0.0;
	double ymean2 = 0.0;
	double xmean3 = 0.0;
	double ymean3 = 0.0;
	double xmean4 = 0.0;
	double ymean4 = 0.0;
	double xout1 = 0.0;
	double yout1 = 0.0;
	double xout2 = 0.0;
	double yout2 = 0.0;
	double xout3 = 0.0;
	double yout3 = 0.0;
	double xout4 = 0.0;
	double yout4 = 0.0;
	
	// declare the covariance object
	//
	Covariance cov = new Covariance();
	
	// declare the initial random variables
	//
	double transpose[][] = new double[2][1];
	double mean[][] = new double[1][2];
	
	// compute the propabilities of each data set
	//
	double maxsamples = set1_d.size() + set2_d.size() 
	                  + set3_d.size() + set4_d.size();
	
	double pr1 = set1_d.size() / maxsamples;
	double pr2 = set2_d.size() / maxsamples;
	double pr3 = set3_d.size() / maxsamples;
	double pr4 = set4_d.size() / maxsamples;
	
	// initialize the between class matrices
	//
	if (set1_d.size() > 0)
       	{
	    M1.row = M1.col = 2;
	    M1.Elem = new double[2][2];
	    M1.resetMatrix();
	}
       	if (set2_d.size() > 0)
       	{
	    M2.row = M2.col = 2;
	    M2.Elem = new double[2][2];
	    M2.resetMatrix();
	}
	if (set3_d.size() > 0)
       	{
	    M3.row = M3.col = 2;
	    M3.Elem = new double[2][2];
	    M3.resetMatrix();
	}
	if (set4_d.size() > 0)
       	{
	    M4.row = M4.col = 2;
	    M4.Elem = new double[2][2];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -