OpenCV Principal Component Analysis(PCA)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <cv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
 
void drawAxis(Mat &img, Point p, Point q, Scalar colour, const float scale = 0.2)
{
    double angle;
    double hypotenuse;
    angle = atan2((double)p.y - q.y, (double)p.x - q.x);
    hypotenuse = sqrt((double)(p.y - q.y) * (p.y - q.y) + (p.x - q.x) * (p.x - q.x));
 
    // Here we lengthen the arrow by a factor of scale
    q.x = (int)(p.x - scale * hypotenuse * cos(angle));
    q.y = (int)(p.y - scale * hypotenuse * sin(angle));
    line(img, p, q, colour, 1, CV_AA);
 
    // create the arrow hooks
    p.x = (int)(q.x + 9 * cos(angle + CV_PI / 4));
    p.y = (int)(q.y + 9 * sin(angle + CV_PI / 4));
    line(img, p, q, colour, 1, CV_AA);
    
    p.x = (int)(q.x + 9 * cos(angle - CV_PI / 4));
    p.y = (int)(q.y + 9 * sin(angle - CV_PI / 4));
    line(img, p, q, colour, 1, CV_AA);
}
 
double getOrientation(const vector<Point> &pts, Mat &img)
{
    // construct a buffer used by the pca analysis
    int sz = static_cast<int>(pts.size());
    Mat data_pts = Mat(sz, 2, CV_64FC1);
 
    for (int i = 0; i < data_pts.rows; ++i)
    {
        data_pts.at<double>(i, 0= pts[i].x;
        data_pts.at<double>(i, 1= pts[i].y;
    }
 
    //Perform PCA analysis
    PCA pca_analysis(data_pts, Mat(), CV_PCA_DATA_AS_ROW);
 
    //Store the center of the object
    Point cntr = Point(static_cast<int>(pca_analysis.mean.at<double>(00)), 
        static_cast<int>(pca_analysis.mean.at<double>(01)));
 
    //Store and eigenvalues and eigenvectors
    vector<Point2d> eigen_vecs(2);
    vector<double> eigen_val(2);
 
    for (int i = 0; i < 2++i)
    {
        eigen_vecs[i] = Point2d(pca_analysis.eigenvectors.at<double>(i, 0),
            pca_analysis.eigenvectors.at<double>(i, 1));
 
        eigen_val[i] = pca_analysis.eigenvalues.at<double>(i, 0);
    }
 
    //Draw the principal components
    circle(img, cntr, 3, Scalar(2550255), 2);
    Point p1 = cntr + 0.02 * Point(static_cast<int>(eigen_vecs[0].x * eigen_val[0]), 
        static_cast<int>(eigen_vecs[0].y * eigen_val[0]));
    Point p2 = cntr - 0.02 * Point(static_cast<int>(eigen_vecs[1].x * eigen_val[1]), 
        static_cast<int>(eigen_vecs[1].y * eigen_val[1]));
    
    drawAxis(img, cntr, p1, Scalar(02550), 1);
    drawAxis(img, cntr, p2, Scalar(2552550), 5);
    
    // orientation in radins
    double angle = atan2(eigen_vecs[0].y, eigen_vecs[0].x);
 
    return angle;
}
 
int main()
{
    Mat src = imread("img/pca_test1.jpg");
 
    if (!src.data || src.empty())
    {
        cout << "can't not load image" << endl;
        return EXIT_FAILURE;
    }
 
    imshow("Src", src);
 
    //Convert Image to grayScale
    Mat gray;
    cvtColor(src, gray, COLOR_BGR2GRAY);
 
    // Convert image to Binary
    Mat bw;
    threshold(gray, bw, 50.255., CV_THRESH_BINARY || CV_THRESH_OTSU);
    
    // Find all the contours in the thresholded image
    vector<Vec4i> hierarchy;
    vector<vector<Point>> contours;
    findContours(bw, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
 
    for (size_t i = 0; i < contours.size(); ++i)
    {
        // Calculate the area of each contour
        double area = contourArea(contours[i]);
 
        if (area < 1e2 || 1e5 < area)
            continue;
 
        drawContours(src, contours, static_cast<int>(i), Scalar(00255), 28, hierarchy, 0);
        getOrientation(contours[i], src);
    }
 
    imshow("output", src);
    
    waitKey(0);
    return 0;
}
cs