PCA在OpenCV中使用新的C ++接口 [英] PCA in OpenCV using the new C++ interface

查看:713
本文介绍了PCA在OpenCV中使用新的C ++接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

As an aside:如果我使用OpenCV问题淹没SO:p

As an aside: Apologies if I'm flooding SO with OpenCV questions :p

我目前正在尝试移植旧的C代码以使用新的C ++接口,我已经到了重新构建我的Eigenfaces面部识别类的点。

I'm currently trying to port over my old C code to use the new C++ interface and I've got to the point where I'm rebuilidng my Eigenfaces face recogniser 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

我试图创建PCA空间,然后看看它是否工作通过显示计算的平均图像。有什么其他步骤吗?

I'm trying to create the PCA space, and then see if it's working by displaying the computed average image. Are there any other steps to this?

也许我需要将图像投影到PCA子空间上?

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

推荐答案

您的错误可能在这里:

Your error is probably here:

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

PCA期望一个数据向量为行的矩阵。但是,你永远不会将图像缩放为相同的大小,以使它们具有相同的像素数(因此尺寸是相同的),还 data.create(2,1,...) code> - 1 需要是矢量的维度,即像素的数量。然后将像素从裁剪复制到矩阵。

PCA expects a matrix with the data vectors as rows. However, you never scale the images to the same size so that they have the same number of pixels (so the dimension is the same), also data.create(2,1,...) - the 1 needs to be the dimension of your vector, i.e. the number of your pixels. Then copy the pixels from the crop to your matrix.

这篇关于PCA在OpenCV中使用新的C ++接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆