【发布时间】:2015-08-07 14:13:20
【问题描述】:
我在 C++ 中使用 OpenCV。我有两张面孔图像,我正在尝试将面孔彼此对齐。我有表示眼睛、鼻子和面部其他兴趣点的特征。我使用 Arun 的方法来提取旋转、平移和缩放,将一个图像中的特征集的质心转换到另一个图像。然后,它旋转并缩放它。我正在尝试使用这种转换来扭曲一个图像,以便图像中的人脸与另一张图像中的人脸对齐。至少这是想法,但我遇到了问题。似乎旋转和平移的执行顺序与我在 warpAffine 中的预期不同。行/列排序与 x/y 坐标似乎也存在问题。此外,正如标题所示,我认为 warpAffine 正在对图像中的 0,0 进行操作,而我希望它们围绕面部点的质心进行。我应该怎么做才能正确对齐这两组点?这是我的代码:
// R is the rotation as computed by Arun's method
// centered_moving is the moving points with the centroid subtracted from them
// same with centered_base
for (int i = 0; i < base.cols; i++)
{
a = centered_base.col(i);
b = centered_moving.col(i).t();
H += a*b;
}
SVD sv;
Mat W, U, Vt;
sv.compute(H, W, U, Vt);
Mat R = (Vt.t())*(U.t());
// centroid_moving and centroid_base are the centroids of the two point clouds, moving is the cloud that will be translated
Mat trans = -centroid_moving + centroid_base;
Mat Afmat = Mat::zeros(2, 3, ddepth);
Mat tmpmat = Afmat(Rect(0, 0, 2, 2));
R = Mat::eye(2, 2, ddepth);
R.copyTo(tmpmat);
Afmat.at<double>(1, 2) = trans.at<double>(0);
Afmat.at<double>(0, 2) = trans.at<double>(1);
warpAffine(image_moving, affine_result, Afmat, image_moving.size());
【问题讨论】:
-
您不能将getAffineTransform 与两个点向量一起使用吗?还是findHomography?
-
我尝试了 findHomography,但由于某种原因,它导致注册非常扭曲。 getAffineTransform 只能从 3 点获取信息,而我的匹配并不完美,所以 3 点还不够好。我有大约 30 分。
-
您确定传递给 findHomography 的两个向量的点顺序相同吗? CV_RANSAC 应该会给你可靠的结果。
-
实际上,Homography 不起作用,但我用 getAffineTransform 解决了它。问题是我的观点是按行、列顺序排列的,而算法似乎需要 x、y 顺序。解决了!
标签: c++ opencv image-processing image-rotation affinetransform