利用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);
}
对于三次均匀的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);
}
相关文章推荐
- APACHE KYLIN简单介绍
- 解决cocopods不提示第三方库名字的方法
- 鸟哥Linux私房菜笔记
- Linux内核及分析 第五周 扒开系统调用的三层皮(下)
- 20135201李辰希 《Linux内核分析》第五周 扒开系统调用的“三层皮”(下)
- wamp2.5 配置多端口多站点虚拟主机 新建站点
- tomcat的编码设置
- linux命令: touch, mkdir, cp, mv, rm
- VirtualBox network / study environment setup for RHEL
- DB2死锁问题与日常维护
- Linux asm系统调用:32位和64位的区别
- 用pyenv解决在centos7下多版本python共存问题
- 网站设计配色笔记(一)
- tomcat的exploded archive 和packaged archive 有什么区别?
- 架构师的十项技能
- Apache编译安装的时候出现的报错
- Kali linux 2 使用 Burpsuite 1.6.38
- Linux 版本查询
- Linux上find命令详解
- 标签嵌套规范、网站结构制作实例笔记