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

C++ 使用PCA使用OpenCV查找视频的特征值和特征向量,c++,opencv,pca,C++,Opencv,Pca,我必须在OpenCV(c++)中使用PCA算法来找到特征值和特征向量。我只是在学习opencv,所以我不知道如何在我的程序中使用PCA类。我想知道我应该在哪里添加PCA方法来找出视频的特征值和特征向量 #include <opencv2/objdetect/objdetect.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp>

我必须在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;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
int main(int argc,常量字符**argv)
{
//创建用于人脸检测的级联分类器对象
层叠式分级机;
//使用haarcascade_frontalface_alt.xml库
face_cascade.load(“haarcascade_frontalface_alt.xml”);
//设置视频捕获设备并将其链接到第一个捕获设备
视频捕获设备;
captureDevice.open(0);
//设置捕获过程中使用的图像文件
网框;
matgrayscaleframe;
Mat试验;
内因;
//创建一个窗口以显示结果
namedWindow(“输出捕获”,1);
//创建循环以捕获和查找面
while(true)
{
//捕获新的图像帧
captureDevice>>captureFrame;
//将捕获的图像转换为灰度并进行均衡
CVT颜色(captureFrame、GrayscaleName、CV_BGR2GRAY);
均衡器历史(GrayScaleName,GrayScaleName);
//创建向量数组以存储找到的面
向量面;
//找到面并将其存储在向量数组中
face_cascade.detectMultiScale(灰度图像,faces,1.1,3,CV_HAAR_FIND_最大对象| CV_HAAR_SCALE_图像,大小(30,30));
//为原始图像上向量数组中找到的所有面绘制一个矩形
对于(int i=0;i对我来说,单张图像的主成分分析没有任何意义。你想用它来实现什么?(而且,你从不初始化nEigens)为什么会有人脸检测?对不起,我是opencv和pca的新手。我只需要特征值和特征向量,这样我就可以计算欧几里德距离。欧几里德距离是什么?基本上我想用pca提取情感。步骤基本上我想用pca提取情感。步骤是视频图像归一化,然后灰度ale转换并使用pca获得特征值和特征向量。然后找到特征向量之间的欧几里得距离,我们将决定情绪。