Skip to content

Instantly share code, notes, and snippets.

@BichengLUO
Created January 29, 2015 06:53
Show Gist options
  • Save BichengLUO/0d29262497faf59e644e to your computer and use it in GitHub Desktop.
Save BichengLUO/0d29262497faf59e644e to your computer and use it in GitHub Desktop.
Bilinear Interpolation
for (int i = 0; i < output.rows; i++)
{
uchar *out_data = output.ptr<uchar>(i);
for (int j = 0; j < output.cols; j++)
{
double ox;
double oy;
//Calculate the old point(ox, oy) according to j, i
//Thus, find a function f:(j, i)->(ox, oy) to get the result like (ox, oy)=f(j, i)
int x1 = ox < 0 ? 0 : ox;
int y1 = oy < 0 ? 0 : oy;
int x2 = x1 + 1 >= WIDTH ? x1 : x1 + 1;
int y2 = y1 + 1 >= HEIGHT ? y1 : y1 + 1;
double u = ox - x1;
double v = oy - y1;
const uchar *src_data1 = src.ptr<uchar>(y1);
const uchar *src_data2 = src.ptr<uchar>(y2);
out_data[3 * j] =
(1 - u) * (1 - v) * src_data1[3 * x1] +
u * (1 - v) * src_data1[3 * x2] +
(1 - u) * v * src_data2[3 * x1] +
u * v * src_data2[3 * x2];
out_data[3 * j + 1] =
(1 - u) * (1 - v) * src_data1[3 * x1 + 1] +
u * (1 - v) * src_data1[3 * x2 + 1] +
(1 - u) * v * src_data2[3 * x1 + 1] +
u * v * src_data2[3 * x2 + 1];
out_data[3 * j + 2] =
(1 - u) * (1 - v) * src_data1[3 * x1 + 2] +
u * (1 - v) * src_data1[3 * x2 + 2] +
(1 - u) * v * src_data2[3 * x1 + 2] +
u * v * src_data2[3 * x2 + 2];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment