您的位置:首页 > 运维架构

利用openCV画出B样条基函数的曲线

2016-03-26 16:32 429 查看
最近要做双三次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;
}
else if (t>=0&&t<1)
{
val = ((double)1/6)*pow(t,3);
}
else 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));
}
else 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));

}
else if(t>=3&&t<4)
{
val = ((double)1/6)*pow(4-t,3);
}
else
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 != curvePoint.end())
{
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);
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: