【发布时间】:2018-04-06 12:08:21
【问题描述】:
我正在尝试对一组点使用多项式回归将它们映射到曲线。当我构建设计矩阵时,分配值似乎没有正确通过。构造的矩阵是一个 3 x n 矩阵,其中 n 是点数,并且与设计矩阵一样,第一列应该全为 1(来自// HERE IS ASSIGNMENT 部分)。当我打印矩阵时,第一列充满了随机数,而且经常是 infs 和 nans。常量赋值怎么可能?这会导致对矩阵进行任何数学运算时出现更多问题,因为它充满了非数字和 infs。
下面的代码是构造矩阵的地方。
for (auto it = lane_lines.begin(); it != lane_lines.end(); it++) {
vector<pair<Point, Point> > lines = *it;
// Remove random line clusters
if (lines.size() < 10) {
it = --lane_lines.erase(it);
continue;
}
Mat design(lines.size() * 2, 3, CV_64FC1);
Mat y_vec(lines.size() * 2, 1, CV_64FC1);
for (int i = 0; i < lines.size(); i += 2) {
// HERE IS ASSIGNMENT
design.at<double>(i * 2, 0) = 1.0;
design.at<double>(i * 2, 1) = lines[i].first.x;
design.at<double>(i * 2, 2) = pow(lines[i].first.x, 2);
design.at<double>(i * 2 + 1, 0) = 1.0;
design.at<double>(i * 2 + 1, 1) = lines[i].second.x;
design.at<double>(i * 2 + 1, 2) = pow(lines[i].second.x, 2);
y_vec.at<double>(i * 2, 0) = lines[i].first.y;
y_vec.at<double>(i * 2 + 1, 0) = lines[i].second.y;
}
// TODO: Deal with all the NaNs
cout << design << endl;
cout << design.t() * design << endl;
cout << "DET: " << determinant(design.t() * design) << endl << endl;
Mat std_poly = ((design.t() * design).inv() * design.t()) * y_vec;
double a = std_poly.at<double>(0, 2);
double b = std_poly.at<double>(0, 1);
double c = std_poly.at<double>(0, 0);
double h = -b / (2 * a == 0 ? numeric_limits<double>::min() : 2 * a);
double k = c - (a * pow(h, 2));
tuple<double, double, double, Point> curve(a, b, c, Point(h, k));
curves.push_back(curve);
}
编辑:对于不同的数据集,问题似乎是随机发生的。
【问题讨论】: