gpt4 book ai didi

c++ - 使用 OpenCV 最小化方程中的矩阵

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:39:44 26 4
gpt4 key购买 nike

我需要在以下等式中最小化 H:

enter image description here

其中 H3x3 矩阵。
Pn 是 3x1 矩阵(点)。
Euclidean() 给出两点之间的距离。
Dn 为实际距离。

我有一个 Hm 点(P0 到 Pm)的初步估计
我需要优化 H 的值,使所有 m 点的误差最小化。(表达式中的所有值都是已知的)我如何使用 opencvdlib(或使用 boost/NLopt)实现它。

最佳答案

虽然dlib 库的find_optimal_parameters 函数的文档确实不够,但您可以在github 上找到单元测试。其中显示了如何使用该功能。

我看到了另一个question你问过,似乎解决方案与这个问题有所不同。但是,这里有一个示例,如何使用该库(这是我第一次听说它)来计算您需要什么或非常接近它的东西。您可能需要更改 DistanceQuality() 函数(通过用两个嵌套循环替换现有循环),我会让您自己来做。

请注意,所有代码都是硬编码的,没有错误处理,测试是在 ma​​in() 函数中完成的。尽管您可以找到用于说明目的的代码,但还有很多工作要做。

开始吧:

#include <iostream>
#include <dlib/optimization.h>
#include <dlib/optimization/find_optimal_parameters.h>

using namespace dlib;

typedef matrix<double, 3, 1> MyPoint;

std::vector<MyPoint> points;
std::vector<double> distances;

double MyDistance(MyPoint point1, MyPoint point2)
{
double sum = 0;
for (int i = 0; i < 3; i++)
{
sum += (point1(i, 0) - point2(i, 0)) * (point1(i, 0) - point2(i, 0));
}
return sqrt(sum);
}

double DistanceQuality(const matrix<double, 3, 3>& H)
{
double sum = 0;

for (int i = 0; i < points.size() - 1; i++)
{
auto proj1 = H*points[i];
auto proj2 = H*points[i+1];
sum += abs(MyDistance(proj1, proj2) - distances[i]);
}
return sum;
}

matrix<double, 3, 3> VecToMatrix(matrix<double, 0, 1> vec)
{
matrix<double, 3, 3> matrix;
for (int i = 0; i < 9; i++)
{
matrix(i / 3, i % 3) = vec(i);
}
return matrix;
}

double test_function(matrix<double, 0, 1> H)
{
matrix<double, 3, 3> newH = VecToMatrix(H);
auto result = DistanceQuality(newH);
return result;
}

int main()
{
matrix<double, 3, 1> p1;
matrix<double, 3, 1> p2;
matrix<double, 3, 1> p3;

p1 = { 1, 1, 1 };
p2 = { 2, 2, 3 };
p3 = { 3, 1.6, 7};

points.push_back(p1);
points.push_back(p2);
points.push_back(p3);

double d1 = 2.44949;
double d2 = 4.142463;

distances.push_back(d1);
distances.push_back(d2);

matrix<double, 0, 1> H;
H = { 3, 1, 1,
1, 1, 6,
1, 4, 1 };

matrix<double, 0, 1> H_min;
matrix<double, 0, 1> H_max;

H_min = { 0.5, 0.6, 0.5,
0.5, 0.7, 0.5,
0.8, 0.3, 0.5, };

H_max = { 10, 10, 10,
10, 10, 10,
10, 10, 10, };

dlib::find_optimal_parameters(4, 0.001, 1000, H, H_min, H_max, test_function);
std::cout << "new H: " << std::endl << VecToMatrix(H) << std::endl;

return 0;
}

希望您可以根据具体情况调整参数。

关于c++ - 使用 OpenCV 最小化方程中的矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42157260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com