I implemented this algorithm to fit given image to specified quadrilateral but the output is blank.
I implemented it in C# as follows
Bitmap inputImage = new Bitmap("D:\\Image\\A2.jpg");
Bitmap outputBitmap = new Bitmap(inputImage.Width, inputImage.Height);
double a11, a21, a31, a41;
double a12, a22, a32, a42;
double first, second;
double denom;
int u, v;
double floatu, floatv;
int i, j;
ulong sourcebase; /// this has been replaced by u,v i think its same
double x, y;
double A, B, C;
int x0 = 50, y0 = 100, x1 = 65, y1 = 110, x2 = 55, y2 = 120, x3 = 450, y3 = 110;// fix value quadrilaeral for checking purpose.
a11 = x0 - x1 - x3 + x2;
a21 = x1 - x0;
a31 = x3 - x0;
a41 = x0;
a12 = y0 - y1 - y3 + y2;
a22 = y1 - y0;
a32 = y3 - y0;
a42 = y0;
Console.WriteLine("inside of here");
A = a11 * a22 - a21 * a12;
if (A == 0)
A = 0.00001;
int rows = inputImage.Height, cols = inputImage.Width;
for (i = 0; i < rows; i++)
{
y = Convert.ToDouble(i) / Convert.ToDouble(rows);
for (j = 0; j < cols; j++)
{
x = Convert.ToDouble(j) / Convert.ToDouble(cols);
B = a12 * x - a11 * y + a11 * a42 - a41 * a12 + a31 * a22 - a21 * a32;
C = a32 * x - a31 * y + a31 * a42 - a41 * a32;
first = B * B;
second = 4.0 * A * C;
if (first < second)
floatu = B / (-2.0 * A);
else
floatu = (-1.0 * B + Math.Sqrt(first - second)) / (2.0 * A);
if((floatu < 0.0) | (floatu > 1.0))
{
if (first < second)
floatu = B / -2.0 * A;
else
floatu = (1.0 * B - Math.Sqrt(first - second)) / (2.0 * A);
}
denom = (a11 * floatu + a31);
if (denom == 0.0)
denom = 0.000001;
floatv = (x - a21 * floatu - a41) / denom;
u = Convert.ToInt32(floatu * cols + 0.5);
v = Convert.ToInt32(floatv * rows + 0.5);
Color pixelColor;
if ((u >= cols) | (u < 0) | (v >= rows) | (v < 0))
{
// Console.WriteLine("escapaed");
pixelColor = Color.Black;
continue;
}
else { /// program ne
Console.WriteLine("here");
//sourcebase = ((ulong)u * (ulong)cols + (ulong)v);
pixelColor = inputImage.GetPixel((int)u, (int)v);
outputBitmap.SetPixel(i, j, pixelColor);// =pixelColor;
// pictureBox1.Image = outputBitmap;
}
}
// Console.WriteLine(i);
}
pictureBox1.Image = outputBitmap;
outputBitmap.Save("output.jpg");
Console.WriteLine("Out Of here");
pictureBox1.Invalidate();