PCA in OpenCV using the new C ++ interface

Aside: Sorry if I flood SO with OpenCV questions: p

I'm currently trying to port my old code to C in order to use the new C ++ interface, and I got to the point where I was releasing the Eigenfaces face recognition class.

 Mat img = imread("1.jpg");
 Mat img2 = imread("2.jpg");
 FaceDetector* detect = new HaarDetector("haarcascade_frontalface_alt2.xml");

 // convert to grey scale
 Mat g_img, g_img2;
 cvtColor(img, g_img, CV_BGR2GRAY);
 cvtColor(img2, g_img2, CV_BGR2GRAY);

 // find the faces in the images
 Rect r = detect->getFace(g_img);
 Mat img_roi = g_img(r);

 r = detect->getFace(g_img2);
 Mat img2_roi = g_img2(r);

 // create the data matrix for PCA
 Mat data;
 data.create(2,1, img2_roi.type());
 data.row(0) = img_roi;
 data.row(1) = img2_roi;

 // perform PCA
 Mat averageFace;
 PCA pca(data, averageFace, CV_PCA_DATA_AS_ROW, 2);

 //namedWindow("avg",1); imshow("avg", averageFace); - causes segfault
 //namedWindow("avg",1); imshow("avg", Mat(pca.mean)); - doesn't work

I am trying to create a PCA space and then see if it works by showing the calculated average image. Are there any other steps to this?

Perhaps I need to project images onto the PCA subspace first?

+2
source share
1 answer

Perhaps your mistake:

Mat data;
data.create(2,1, img2_roi.type());
data.row(0) = img_roi;
data.row(1) = img2_roi;

PCA . , , ( ), data.create(2,1,...) - 1 , . .

+2

Source: https://habr.com/ru/post/1791599/