OpenCV 图像插值计算(双线性插值/双三次插值)

图像插值也叫图像重采样,在很多需要进行图像定位的计算中大量用到,OpenCV将图像插值的函数封装到了其与几何变换相关的函数中,并未直接暴露独立进行图像插值计算的接口,而在需要情况下我们需要对图像进行直接的插值计算,本文给出了两个常用的图像插值接口:双线性插值与双三次插值。

1.双线性插值:

double GetBilinarVal(double* val, double dx, double dy)
{
	double r4 = dx * dy;
	return (1 + r4 - dx - dy) * val[0] + (dx - r4) * val[1] + (dy - r4) * val[2] + r4 * val[3];
}

2.双三次插值:

double GetBicubicVal(double* val, double dx, double dy)
{
	const float A = -0.75f;

	double coeffsX[4]; 
	coeffsX[0] = ((A*(dx + 1) - 5*A)*(dx + 1) + 8*A)*(dx + 1) - 4*A;
	coeffsX[1] = ((A + 2)*dx - (A + 3))*dx*dx + 1;
	coeffsX[2] = ((A + 2)*(1 - dx) - (A + 3))*(1 - dx)*(1 - dx) + 1;
	coeffsX[3] = 1.f - coeffsX[0] - coeffsX[1] - coeffsX[2];

	double coeffsY[4]; 
	coeffsY[0] = ((A*(dy + 1) - 5*A)*(dy + 1) + 8*A)*(dy + 1) - 4*A;
	coeffsY[1] = ((A + 2)*dy - (A + 3))*dy*dy + 1;
	coeffsY[2] = ((A + 2)*(1 - dy) - (A + 3))*(1 - dy)*(1 - dy) + 1;
	coeffsY[3] = 1.f - coeffsX[0] - coeffsX[1] - coeffsX[2];

	cv::Mat matWx(1, 4, CV_64FC1, coeffsX);
	cv::Mat matI(4, 4, CV_64FC1, val);
	cv::Mat matWy(4, 1, CV_64FC1, coeffsY);

	cv::Mat matRes = matWx * matI * matWy;
	double res = *(double*)(matRes.data);

	return res;
}


你可能感兴趣的:(OpenCV,拾遗)