使用PCA使用OpenCV查找视频的特征值和特征向量

时间:2015-01-29 20:04:48

标签: c++ opencv pca

我必须在OpenCV(c ++)中使用PCA算法找到特征值和特征向量。我刚刚学习opencv,所以我不知道如何在我的程序中使用PCA课程。我想知道在哪里可以添加PCA方法来找出视频的特征值和特征向量。

    #include <opencv2/objdetect/objdetect.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/core/core.hpp>
    #include <iostream>
    #include <stdio.h>

    using namespace std;
    using namespace cv;

    int main(int argc, const char** argv)
{
//create the cascade classifier object used for the face detection
CascadeClassifier face_cascade;
//use the haarcascade_frontalface_alt.xml library
face_cascade.load("haarcascade_frontalface_alt.xml");

//setup video capture device and link it to the first capture device
VideoCapture captureDevice;
captureDevice.open(0);

//setup image files used in the capture process
Mat captureFrame;
Mat grayscaleFrame;
Mat trial;
int nEigens;
//create a window to present the results
namedWindow("outputCapture", 1);

//create a loop to capture and find faces
while (true)
{
    //capture a new image frame
    captureDevice >> captureFrame;

    //convert captured image to gray scale and equalize
    cvtColor(captureFrame, grayscaleFrame, CV_BGR2GRAY);
    equalizeHist(grayscaleFrame, grayscaleFrame);

    //create a vector array to store the face found
    std::vector<Rect> faces;

    //find faces and store them in the vector array
    face_cascade.detectMultiScale(grayscaleFrame, faces, 1.1, 3, CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_SCALE_IMAGE, Size(30, 30));

    //draw a rectangle for all found faces in the vector array on the original image
    for (int i = 0; i < faces.size(); i++)
    {
        Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
        Point pt2(faces[i].x, faces[i].y);

        rectangle(captureFrame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
    }
    PCA pca(captureFrame,trial, CV_PCA_DATA_AS_ROW, nEigens);

    Mat data(captureFrame.rows, nEigens, CV_32FC1);
    cout << nEigens;
    //print the output
    imshow("outputCapture", captureFrame);

    //pause for 33ms
    imshow("grayscaleconversion", grayscaleFrame);
    waitKey(33);

}

return 0;
}

0 个答案:

没有答案