krz*_*ych 18 algorithm geometry image-processing
我有对象让我们说模型图像.我想计算模型图像上的对象和目标图像上的对象之间的变换(位移,缩放,旋转).我想假设对象可以被视为2D,因此只应计算2D变换.
首先,我想以手动辅助方式进行操作.用户在模型图像上选择基点,然后在目标图像上选择目标点.点数应由用户定义(但不低于最低2-3点).当点给出不同的信息时,应该对变换进行平均,并且例如由此可以计算匹配的质量.
所以问题是关于计算两组点的变换,但正如我想在图像上做的那样,我添加了图像处理标签.
特别欢迎使用一些代码或伪代码的参考和建议.
有两点是非常容易的问题,只应采用线的旋转,比例和位移,但如何用更多的点来做,并对其进行平均并计算一些质量因素.
目前的解决方案是
void transformFnc(std::vector<PointF> basePoints, std::vector<PointF> targetPoints,
PointF& offset, double rotation, double scale)
{
std::vector<Line> basePointsLines;
std::vector<Line> targetPointsLines;
assert(basePoints.size() == targetPoints.size());
int pointsNumber = basePoints.size();
for(int i = 0; i < pointsNumber; i++)
{
for(int j = i + 1; j < pointsNumber; j++)
{
basePointsLines.push_back(Line(basePoints[i], basePoints[j]));
targetPointsLines.push_back(Line(targetPoints[i], targetPoints[j]));
}
}
std::vector<double> scalesVector;
std::vector<double> rotationsVector;
double baseCenterX = 0, baseCenterY = 0, targetCenterX = 0, targetCenterY = 0;
for(std::vector<Line>::iterator it = basePointsLines.begin(), i = targetPointsLines.begin();
it != basePointsLines.end(), i != targetPointsLines.end(); it++, i++)
{
scalesVector.push_back((*i).length()/(*it).length());
baseCenterX += (*it).pointAt(0.5).x();
baseCenterY += (*it).pointAt(0.5).y();
targetCenterX += (*i).pointAt(0.5).x();
targetCenterY += (*i).pointAt(0.5).y();
double rotation;
rotation = (*i).angleTo((*it));
rotationsVector.push_back(rotation);
}
baseCenterX = baseCenterX / pointsNumber;
baseCenterY = baseCenterY / pointsNumber;
targetCenterX = targetCenterX / pointsNumber;
targetCenterY = targetCenterY / pointsNumber;
offset = PointF(targetCenterX - baseCenterX, targetCenterY - baseCenterY);
scale = sum(scalesVector) / scalesVector.size();
rotation = sum(rotationsVector) / rotationsVector.size();
}
Run Code Online (Sandbox Code Playgroud)
我在这段代码中只能找到的优化是从比例和旋转中消除那些与其他值差别太大的值.
我正在寻找解决方案命题的代码或伪代码.它也可以引用一些代码.
到目前为止,我知道答案:
wic*_*ich 23
首先使用3x3仿射变换矩阵在简单的仿射变换中推广该问题:即
[M11 M12 M13]
[M21 M22 M23]
[M31 M32 M33]
Run Code Online (Sandbox Code Playgroud)
既然我们已经知道第三行总是[0 0 1],我们可以简单地忽略它.
现在我们可以将问题描述为以下矩阵方程
[xp0] [x0 y0 1 0 0 0 ]
[yp0] [0 0 0 x0 y0 1 ] [M11]
[xp1] [x1 y1 1 0 0 0 ] [M12]
[yp1] = [0 0 0 x1 y1 1 ] * [M13]
[xp2] [x2 y2 1 0 0 0 ] [M21]
[yp2] [0 0 0 x2 y2 1 ] [M22]
[xp3] [x3 y3 1 0 0 0 ] [M23]
[yp3] [0 0 0 x3 y3 1 ]
Run Code Online (Sandbox Code Playgroud)
其中xp和yp是投影坐标,x和y是原始坐标.
我们称之为
proj = M * trans
Run Code Online (Sandbox Code Playgroud)
然后我们可以计算出适合转换的最小二乘法
trans = pinv(M) * proj
Run Code Online (Sandbox Code Playgroud)
其中pinv是伪逆.
这给了我们一个最符合最小二乘意义上给出的点的仿射变换.
现在显然这也会给出剪切,坐标翻转以及你不想要的非均匀缩放,因此我们需要以某种方式限制仿射变换以避免剪切.事实证明这很简单,我们可以使用单个向量来描述旋转(向量的方向)和缩放(向量的大小),另一个向量将简单地与它正交.这将自由度降低了两倍.
M21 = -M12
M22 = M11
Run Code Online (Sandbox Code Playgroud)
所以减少到
[xp0] [x0 y0 1 0]
[yp0] [y0 -x0 0 1]
[xp1] [x1 y1 1 0] [M11]
[yp1] = [y1 -x1 0 1] * [M12]
[xp2] [x2 y2 1 0] [M13]
[yp2] [y2 -x2 0 1] [M23]
[xp3] [x3 y3 1 0]
[yp3] [y3 -x3 0 1]
Run Code Online (Sandbox Code Playgroud)
在我们求解上述矩阵方程后,从M12和M11计算出M21和M22.