#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
using namespace cv;
vector<Point2d> orig_point;
vector<Point2d> control_point;
double u1 = 0.3;//这两个参数取决于中间两个点的位置
double u2 = 0.7;
int main() {
//1.四个贝塞尔曲线上的点
orig_point.push_back(Point2d(0, 0));
orig_point.push_back(Point2d(3, 5));
orig_point.push_back(Point2d(7, 5));
orig_point.push_back(Point2d(10, 0));
Mat P(4, 2, CV_64F);
//将点赋值给4*2的矩阵
for (int i = 0; i < 4; i++) {
P.at<double>(i, 0) = orig_point[i].x;
P.at<double>(i, 1) = orig_point[i].y;
}
cout << "贝塞尔曲线上的四个点分别为:\n" << P << endl;
//2.定义一个4*4的U矩阵,每一行对应的是四个贝塞尔曲线上的点参数
Mat U = (Mat_ <double>(4, 4) <<
0, 0, 0, 1, //对应第一个点,此时u=0
u1 * u1 * u1, u1 * u1, u1, 1, //对应第二个点,(u1^3,u1^2,u1,1)
u2 * u2 * u2, u2 * u2, u2, 1,
1, 1, 1, 1);//对应第四个点,此时u=1
//3.定义好M矩阵,这个是固定的
Mat M = (Mat_ <double>(4, 4) <<
-1, 3, -3, 1,
3, -6, 3, 0,
-3, 3, 0, 0,
1, 0, 0, 0);
//4.求U*M的逆
Mat UM = U * M;
//cout << UM.size() << endl;
//cout << UM << endl;
Mat UM_inv = UM.inv();
//5.控制点,因为P=U*M*C,所以控制点C=UM的逆*P
Mat C;
C = UM_inv * P;
cout << endl << "求得的控制点为:\n" << C << endl;
}
结果验证,首尾两点坐标一定相同
2021年9月22日 -- add