利用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;
	}
	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);
}


 

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值