给定一组点,如何计算相似度变换(平移,缩放,旋转)? [英] Given a set of points, how to calculate similarity transform(translation,scale,rotation)?
问题描述
我试图通过仿射扭曲来记录一张脸,并注意到有时它歪斜得太多.我想计算一个没有剪切/偏斜分量但仍然在配准误差上保持最小二乘误差条件的变换矩阵.我怎样才能做到这一点?以下是我进行仿射变换的方式.
I am trying to register a face by affine warping and notice that sometimes it is skewed too much. I would like to calculate a transformation matrix that has no shear/skew component but still maintain the least square error condition on registration error. How can I do this? The following is how I am doing the affine transformation.
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int ac, char* av[])
{
vector<Point2f> src;
vector<Point2f> dst;
src.push_back(Point2f(100,100));
src.push_back(Point2f(150,150));
src.push_back(Point2f(200,200));
dst.push_back(Point2f(50,100));
dst.push_back(Point2f(150,150));
dst.push_back(Point2f(210,220));
// we want M,solve for it using the following
// M * U = X
// M = X * inv(U)
Mat U=Mat::ones(3,3,CV_32FC1);
Mat X=Mat::ones(2,3,CV_32FC1);
U.at<float>(0,0)=dst[0].x;
U.at<float>(0,1)=dst[1].x;
U.at<float>(0,2)=dst[2].x;
U.at<float>(1,0)=dst[0].y;
U.at<float>(1,1)=dst[1].y;
U.at<float>(1,2)=dst[2].y;
X.at<float>(0,0)=src[0].x;
X.at<float>(0,1)=src[1].x;
X.at<float>(0,2)=src[2].x;
X.at<float>(1,0)=src[0].y;
X.at<float>(1,1)=src[1].y;
X.at<float>(1,2)=src[2].y;
Mat M = X * U.inv();
//now we have the transform matrix M, we can apply this to any x,y and get the source corrdinates
float x=20,y=20;
Mat DST=Mat::zeros(3,1,CV_32FC1);
DST.at<float>(0,0)=x;
DST.at<float>(1,0)=y;
DST.at<float>(2,0)=1;
Mat SRC = M*DST;
float xf =SRC.at<float>(0,0);
float yf =SRC.at<float>(1,0);
//interpolation etc
return 0;
}
推荐答案
OpenCV的相似度转换是 cv :: estimateRigidTransform
,可让您计算4个自由度(相似度转换)或6个自由度自由(完全仿射)变换,取决于您选择的参数.
OpenCVs similarity transform is cv::estimateRigidTransform
which allows you to compute a 4 degree of freedom (similarity transform) or 6 degree of freedom (full affine) transformation, depending on the parameters you choose.
查看链接以获取更多详细信息:
see the link for more details:
但我不确定为什么它无法为您的某些输入找到解决方案.请尝试使用点的向量而不是矩阵.我已经看到了一些案例(尽管与findHomography相当),OpenCV将3x2矩阵(3个点,每个2维)错误地解释为2个点,每个3维,反之亦然.
but I am not sure why it doesn't find a solution for some of your input. Please try to use a vector of points instead of a matrix. I've seen cases (afair it was with findHomography though) where OpenCV misinterpreted 3x2 matrix (3 points with 2 dimensions each) to be 2 points with 3 dimensions each instead or vice-versa.
这篇关于给定一组点,如何计算相似度变换(平移,缩放,旋转)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!