Skip to content

Commit

Permalink
Adding PCA
Browse files Browse the repository at this point in the history
  • Loading branch information
Romaisaa committed May 11, 2023
1 parent 8ed76f8 commit 3345e94
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
20 changes: 11 additions & 9 deletions CV-Toolbox.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport

CONFIG += c++11
CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
CV/face_recognition.cpp \
CV/faces_detection.cpp \
CV/harris.cpp \
CV/histogram.cpp \
Expand Down Expand Up @@ -39,6 +40,7 @@ SOURCES += \
pages/page7.cpp

HEADERS += \
CV/face_recognition.h \
CV/faces_detection.h \
CV/harris.h \
CV/histogram.h \
Expand Down Expand Up @@ -83,15 +85,15 @@ FORMS += \



INCLUDEPATH += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\build\include
INCLUDEPATH += C:\opencv\build\include

LIBS += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\release\bin\libopencv_core470.dll
LIBS += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\release\bin\libopencv_highgui470.dll
LIBS += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\release\bin\libopencv_imgcodecs470.dll
LIBS += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\release\bin\libopencv_imgproc470.dll
LIBS += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\release\bin\libopencv_features2d470.dll
LIBS += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\release\bin\libopencv_calib3d470.dll
LIBS += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\release\bin\libopencv_objdetect470.dll
LIBS += C:\opencv\release\bin\libopencv_core470.dll
LIBS += C:\opencv\release\bin\libopencv_highgui470.dll
LIBS += C:\opencv\release\bin\libopencv_imgcodecs470.dll
LIBS += C:\opencv\release\bin\libopencv_imgproc470.dll
LIBS += C:\opencv\release\bin\libopencv_features2d470.dll
LIBS += C:\opencv\release\bin\libopencv_calib3d470.dll
LIBS += C:\opencv\release\bin\libopencv_objdetect470.dll


# Default rules for deployment.
Expand Down
26 changes: 26 additions & 0 deletions CV/face_recognition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "face_recognition.h"

void face_recognition:: performPCA(cv::Mat& dataPoints, cv::Mat& eigenvalues, cv::Mat& eigenvectors, cv:: Mat& convertedData,int numComponents)
{
cv::Mat mean;
cv::reduce(dataPoints, mean, 0, cv::REDUCE_AVG);
for (int i = 0; i < dataPoints.rows; i++)
{
dataPoints.row(i) = dataPoints.row(i) - mean;
}
cv::Mat covariance;
cv::mulTransposed(dataPoints, covariance, true);
covariance = covariance / (dataPoints.rows - 1);
cv::eigen(covariance, eigenvalues, eigenvectors);
eigenvalues=eigenvalues.rowRange(0, numComponents);
eigenvectors = eigenvectors.rowRange(0, numComponents);
convertedData = dataPoints * eigenvectors.t();
}

void face_recognition:: multiplyEigen(cv::Mat& eigenvectors, cv::Mat& image, cv::Mat& result)
{
cv::Mat reshapedImage = image.reshape(1, 1);
cv::Mat floatImage;
reshapedImage.convertTo(floatImage, CV_32F);
result = floatImage * eigenvectors.t();
}
20 changes: 20 additions & 0 deletions CV/face_recognition.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef FACE_RECOGNITION_H
#define FACE_RECOGNITION_H
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/imgproc/types_c.h>


class face_recognition
{
public:
face_recognition();
private:
void performPCA(cv::Mat& dataPoints, cv::Mat& eigenvalues, cv::Mat& eigenvectors, cv:: Mat& convertedData,int numComponents);
void multiplyEigen(cv::Mat& eigenvectors, cv::Mat& image, cv::Mat& result);


};

#endif // FACE_RECOGNITION_H

0 comments on commit 3345e94

Please sign in to comment.