Large pits in real operation of PCA

Source: Internet
Author: User
Tags pow sin square root
PCA real operation in the big pit really is not hurt ah .... Today, we are talking about a problem with a subconscious error. In my blog There are two other articles reproduced in the blog is a record of the idea of PCA, there is a need to see.
	Mat m (Ten, 2, cv_32f, Scalar (0));	
	Mat dt = cv::mat_<double> (m);
	Dt.at<double> (0,0) = 2.5;
	Dt.at<double> (0,1) = 2.4;
	Dt.at<double> (1,0) = 0.5;
	dt.at<double> () = 0.7;
	Dt.at<double> (2,0) = 2.2;
	Dt.at<double> (2,1) = 2.9;
	Dt.at<double> (3,0) = 1.9;
	Dt.at<double> (3,1) = 2.2;
	Dt.at<double> (4,0) = 3.1;
	Dt.at<double> (4,1) = 3.0;
	Dt.at<double> (5,0) = 2.3;
	Dt.at<double> (5,1) = 2.7;
	Dt.at<double> (6,0) = 2;
	Dt.at<double> (6,1) = 1.6;
	Dt.at<double> (7,0) = 1;
	Dt.at<double> (7,1) = 1.1;
	Dt.at<double> (8,0) = 1.5;
	Dt.at<double> (8,1) = 1.6;
	Dt.at<double> (9,0) = 1.1;
	Dt.at<double> (9,1) = 0.9

	//Perform PCA algorithm
	PCA PCA (DT, Mat (), cv_pca_data_as_row);//The third parameter means reading data in row mode, The fourth parameter indicates that the selected principal component is the ratio

	of mat eigenvalues = Pca.eigenvalues.clone ();//calculated principal component eigenvalues
	mat eigenvectors = pca.eigenvectors . Clone (); Calculation of the principal component characteristics

The principal component characteristics obtained are:

As can be seen from the above, two principal component eigenvectors are obtained: ( -0.735178656,0.677873399) and ( -0.67787339,-0.735178656). Next, the problem arises. I want to solve the angle between the feature vector and the horizontal straight line, so that the image tilt correction.
As shown in the figure above, we see the blue line as the vector we have obtained, and the angle between the SITA and the horizontal direction is calculated. Here comes the first question, I follow the size of the eigenvalues of the arrangement, I use the maximum eigenvalue of the corresponding feature vector to solve the angle is always wrong, and then holding the attitude of the attempt to use the second eigenvector to solve the angle, unexpectedly successful, the reason is still not too understanding, Why the first one doesn't work, the second one is OK.
The first problem arises, and then the second problem arises, where we need to abandon our traditional mode of thinking and see that the acquired eigenvector is considered (x, y) mode. In PCA, its eigenvector belongs to (y,x) mode, that is, the first value is the y-coordinate value, and the second value is the x-axis coordinate value.
So for the above image we should use the eigenvector: ( -0.735178656,0.677873399) its angle Sita = arctan ((-0.735178656)/0.677873399);
Here's a note, I'm using inverse trigonometric function in C + + to get the angle value. Here is a brief introduction: " Cos,sin,asin,acos These trigonometric functions in C + + are radians, not angles .
radians = angle *pi/180;


Example 1:
For example, the Bing side is a, b
Set the angle to X, then
X=atan (A/b);
where x is the radian system
If you want to convert to an angle value, the x*180/3.1415


Example 2:

    Calculate rotation angle {radians = angle *pi/180} {Two points distance between the formula square root (| Square +| of x1-x2|    y1-y2| Square)}  double angle_tanvalue=sqrt (POW (point.x-point.x,2) +pow (point.y-right_top_point.y,2))/
sqrt (POW (turn_point_l[1].x-point.x,2) +pow (turn_point_l[1].y-right_top_point.y,2));  //Find the Radian  of tan and Sin  Double Angle_atanvalue=atan (angle_tanvalue);  double Angle_sinvalue=sin (angle_atanvalue);  //radians converted to angle  Double angle=angle_atanvalue*180/3.1415;  //Take absolute value  Angle=fabs (angle);



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.