利⽤openCV画出B样条基函数的曲线
最近要做双三次B样条插值(bicubic),从基础的三次B样条开始看起,但是⽹上基本没有介绍B样条基函数的⽂章,所以在这⾥写下来,⽅便他⼈参考。
对于三次均匀的B样条曲线,其递推公式如下:
⼀般的B样条⽤的最多的是三次B样条曲线,也就是k = 4 时的曲线。公式太长,我懒得打,可以在程序⾥⾯看。
下⾯⽤openCV画出三次B样条基函数:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
#include <math.h>
#define NUMPOINT 40 //采样点个数
Point polyNomial (double t)
{
double val = 0;
Point point;
//下⾯是B样条三次基函数的曲线表达式
小区服务if(t<0)
{
val = 0;
}
枸杞红茶el if (t>=0&&t<1)
{
val = ((double)1/6)*pow(t,3);
桑蚕丝的洗涤方法
}
el if (t>=1&&t<2)
防汛救灾
{
val = ((double)1/6)* (pow(t,2)*(2-t)+t*(3-t)*(t-1)+(4-t)*pow(t-1,2));
}怎么炒米粉
el if (t>=2&&t<3)
{
val = ((double)1/6)* (pow(3-t,2)*t + (4-t)*(3-t)*(t-1)+(t-2)*pow(4-t,2));
}
el if(t>=3&&t<4)
{
val = ((double)1/6)*pow(4-t,3);
}
el
val =0;
point = cvPoint(t*100,val*100);//对⼩数进⾏放⼤,并转换成int型存⼊Point中
return point;
}
void main()
{
Mat img = Mat::zeros(800,800,CV_8UC3);//创建画布
vector<Point> curvePoint ;//⽤于保存point的Vector
Point tmpPoint;创业访谈
for (int i =0;i<NUMPOINT;++i)
{
tmpPoint = polyNomial(i*(4/(double)NUMPOINT));//返回基函数的值 curvePoint.push_back(tmpPoint);
}
vector<Point>::iterator it;
it = curvePoint.begin();
Point pointPre = cvPoint(0,800);//起始点
while(it != d())
ISTP收录
{
Point pointTmp = (*it);
pointTmp =cvPoint(pointTmp.x ,800 - pointTmp.y);//坐标翻转
line(img,pointPre,pointTmp,cvScalarAll(255),4);
pointPre = pointTmp;
++it;
比较广告
}
cvNamedWindow("out");
imshow("out",img);
waitKey(0);
}