图像双线性插值(Bilinear interpolation)

int BilinearPola(const Mat input, Mat& out, const int rewid, const int rehei)
{
	if (input.empty())
		return 1;
	if (rewid == 0 || rehei == 0)
		return 2;

	int wid = input.cols;
	int hei = input.rows;
	float wid_scale = float(wid) / rewid;
	float hei_scale = float(hei) / rehei;
	float tmp_x = 0.0f;
	float tmp_y = 0.0f;
	int x1, x2, y1, y2;
	for (int i = 0; i < rewid; i++)
	{
		tmp_x = i * wid_scale;
		x1 = max(0, int(tmp_x));
		x2 = min(int(tmp_x + 1), wid - 1);
		for (int j = 0; j < rehei; j++)
		{
			//printf("i = %d,j = %d\n", i, j);
			tmp_y = j * hei_scale;
			y1 = max(0, int(tmp_y));
			y2 = min(int(tmp_y + 1), hei - 1);
			out.at<Vec3b>(j, i)[0] = (y2 - tmp_y) * ((x2 - tmp_x) * input.at<Vec3b>(y1, x1)[0] + (tmp_x - x1) * input.at<Vec3b>(y1, x2)[0])
				+ (tmp_y - y1) * ((x2 - tmp_x) * input.at<Vec3b>(y2, x1)[0] + (tmp_x - x1) * input.at<Vec3b>(y2, x2)[0]);
			out.at<Vec3b>(j, i)[1] = (y2 - tmp_y) * ((x2 - tmp_x) * input.at<Vec3b>(y1, x1)[1] + (tmp_x - x1) * input.at<Vec3b>(y1, x2)[1])
				+ (tmp_y - y1) * ((x2 - tmp_x) * input.at<Vec3b>(y2, x1)[1] + (tmp_x - x1) * input.at<Vec3b>(y2, x2)[1]);
			out.at<Vec3b>(j, i)[2] = (y2 - tmp_y) * ((x2 - tmp_x) * input.at<Vec3b>(y1, x1)[2] + (tmp_x - x1) * input.at<Vec3b>(y1, x2)[2])
				+ (tmp_y - y1) * ((x2 - tmp_x) * input.at<Vec3b>(y2, x1)[2] + (tmp_x - x1) * input.at<Vec3b>(y2, x2)[2]);
		}
	}
	return 0;
}
发布了59 篇原创文章 · 获赞 57 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/xiakejiang/article/details/89668213