<p>I implemented <a href="http://math.ewha.ac.kr/~jylee/SciComp/dip-crane/chapter8/list8_4.c" rel="nofollow noreferrer">this algorithm</a> to fit given image to specified quadrilateral but the output is blank.</p>

<p>I implemented it in C# as follows</p>

<pre class="lang-csharp prettyprint-override"><code>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 &lt; rows; i++)
{
    y = Convert.ToDouble(i) / Convert.ToDouble(rows);
    for (j = 0; j &lt; 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 &lt; second)
            floatu = B / (-2.0 * A);
        else
            floatu = (-1.0 * B + Math.Sqrt(first - second)) / (2.0 * A);
        if((floatu &lt; 0.0) | (floatu &gt; 1.0))
        {
            if (first &lt; 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 &gt;= cols) | (u &lt; 0) | (v &gt;= rows) | (v &lt; 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();
</code></pre>

CC BY-SA 3.0
2016-05-09T01:32:42.353Z
The C version to the above code is in the link at description of question.
Sandiip
CC BY-SA 3.0
2016-05-10T00:05:01.570Z
I have removed the "//" and C# implementation of above code is this much only
Sandiip
<image-processing><perspective>
Bilinear Transformation not working
Sandiip
# Bilinear Transformation not working

(Asked by: username_0 on 2016-05-06)

I implemented [this algorithm](http://math.ewha.ac.kr/~jylee/SciComp/dip-crane/chapter8/list8_4.c) to fit given image to specified quadrilateral but the output is blank.

I implemented it in C# as follows

```csharp
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();

```

**Tags:** image-processing, perspective

> **Comments:**
> **username_0**: The C version to the above code is in the link at description of question.  
> **username_0**: I have removed the "//" and C# implementation of above code is this much only  

---

## 0 answers

