<p>This is more of a long comment, with an image, than an answer.</p>

<p>Seems to me, at a quick glance, that you consider your pixels sample point to be at integer values. Instead in reality sample is located offset 0.5 pixels of this location.</p>

<p><a href="https://i.sstatic.net/0UGXJ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/0UGXJ.png" alt="enter image description here"></a></p>

<p><strong>Image 1</strong>: Is the pixel sample located at its integer coordinate? Or is it located offset by 0.5 pixels?</p>

CC BY-SA 3.0
2016-02-20T07:18:32.540Z
2016-02-20T07:24:04.553Z
2016-02-20T07:24:04.553Z
joojaa
<p>Thanks for the replies. According to joojaa's comments, I modified my codes as below,</p>

<pre><code>public void RotateImageClockwise(double degree)
    {
        Image&lt;double&gt; image = (Image&lt;double&gt;)(PreservedRawImage.Clone());      
        double sinAngle = Math.Sin(Math.PI * degree / 180.0);
        double cosAngle = Math.Cos(Math.PI * degree / 180.0);
        double newHeightRaw, newWidthRaw;

        if (Math.Abs(degree) &lt;= 90)
        {
            newHeightRaw = image.Width * Math.Sin(Math.PI * (Math.Abs(degree)) / 180.0) + image.Height * Math.Cos(Math.PI * Math.Abs(degree) / 180.0);
            newWidthRaw = image.Width * Math.Cos(Math.PI * Math.Abs(degree) / 180.0) + image.Height * Math.Sin(Math.PI * (Math.Abs(degree)) / 180.0);
        }
        else if (Math.Abs(degree) &lt;= 180)
        {
            newHeightRaw = image.Width * Math.Sin(Math.PI * (180 - Math.Abs(degree)) / 180.0) + image.Height * Math.Sin(Math.PI * (Math.Abs(degree) - 90) / 180.0);
            newWidthRaw = image.Width * Math.Cos(Math.PI * (180 - Math.Abs(degree)) / 180.0) + image.Height * Math.Cos(Math.PI * (Math.Abs(degree) - 90) / 180.0);
        }

        int newWidth = (int)Math.Round(newWidthRaw);
        int newHeight = (int)Math.Round(newHeightRaw);
        Image&lt;double&gt; newImage = new Image&lt;double&gt;(newWidth, newHeight);
        for (int x = 0; x &lt; newHeight; x++)
        {
            for (int y = 0; y &lt; newWidth; y++)
            {
                double xt = x - (newHeightRaw - 1) / 2.0;
                double yt = y - (newWidthRaw - 1) / 2.0;

                double originalX = xt * cosAngle - yt * sinAngle + (image.Height - 1) / 2.0;
                double originalY = xt * sinAngle + yt * cosAngle + (image.Width - 1) / 2.0;

                double fractionX = Math.Abs(originalX - (int)Math.Round(originalX));
                double fractionY = Math.Abs(originalY - (int)Math.Round(originalY));

                IList&lt;Point&lt;int&gt;&gt; neighbors = new List&lt;Point&lt;int&gt;&gt; { new Point&lt;int&gt;{x = (int)Math.Round(originalX), y = (int)Math.Round(originalY), weight = (1 - fractionX) * (1 - fractionY)}, 
                                                           new Point&lt;int&gt;{x = (int)Math.Round(originalX), y = (int)Math.Round(originalY) + 1, weight = fractionX * (1 - fractionY)},
                                                           new Point&lt;int&gt;{x = (int)Math.Round(originalX) + 1, y = (int)Math.Round(originalY), weight = (1 - fractionX) * fractionY},
                                                           new Point&lt;int&gt;{x = (int)Math.Round(originalX) + 1, y = (int)Math.Round(originalY) + 1, weight = fractionX * fractionY}
                                                         };
                bool hasData = false;
                double newData = 0;
                double weight = 0;
                foreach (var neighbor in neighbors)
                {
                    if (neighbor.x &gt;= 0 &amp;&amp; neighbor.x &lt; image.Height
                        &amp;&amp; neighbor.y &gt;= 0 &amp;&amp; neighbor.y &lt; image.Width
                        &amp;&amp; (neighbor.x) * image.Width + neighbor.y &lt; m_preservedImageValidDataBitmap.Length &amp;&amp; m_preservedImageValidDataBitmap[(neighbor.x) * image.Width + neighbor.y] == 'T')
                    {
                        hasData = true;
                        newData += neighbor.weight * image.Data[(neighbor.x) * image.Width + neighbor.y];
                        weight += neighbor.weight;
                    }
                }                    
                if (hasData &amp;&amp; weight != 0)
                {
                    newData /= weight;
                    newImage.Data[x * newWidth + y] = newData;                        
                }
            }                
        }
    } 
</code></pre>

<p>Now it's working for 90 and 180 degree rotations. But for 45 degree rotation, the bottom line is still blank, which makes me crazy. I'm guessing it's due to the computation inaccuracy when the new image width and height are computed, but I still can't figure it out.</p>

<p><a href="https://i.sstatic.net/OQB9E.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/OQB9E.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2016-02-22T21:48:31.237Z
2016-02-22T21:48:31.237Z
Ames ISU
<p>I'm struggling with a problem with image rotation. I'm using the idea of "Rotation by area mapping" in the following link:
<a href="http://www.leptonica.com/rotation.html" rel="noreferrer">http://www.leptonica.com/rotation.html</a></p>

<p>My c# codes are as follows. The idea is first to find the dimension of the rotated image, then find the gray level of each pixel in the rotated image. To find the gray level for each pixel, the pixel coordinates are transformed back to the coordinates in the original image (called original pixel). The original pixel has fractional coordinate values, then the bilinear interpolation method in the link is used to obtain the gray level of the pixel in the rotated image. My codes did most of the job, except there are some clipping at the corners in the rotated image. I put the rotated images of a 16*16 image, we can clearly see the effect: The bottom line in the 45 degree rotated has no gray data; in the 90 degree rotated one, the vertical line in the left has no gray data; and in the 180 degree rotated one, both the vertical line in the left and the horizontal line on the top have no gray. I believe it's caused by the coordinate computation, and I could modify my code by subtracting one of the coordinates by 1 to achieve correct rotation by a certain angle, but it doesn't work for the other case. So I guess there must be something I ignored in my codes. Any suggestions and comments are appreciated!</p>

<pre><code>public void RotateImageClockwise(double degree)
{
    Image&lt;double&gt; image = (Image&lt;double&gt;)(PreservedRawImage.Clone());      
        double sinAngle = Math.Sin(Math.PI * degree / 180.0);
        double cosAngle = Math.Cos(Math.PI * degree / 180.0);
        double newHeightRaw, newWidthRaw;

        ////Find the new dimensions of the rotated image.
        if (Math.Abs(degree) &lt;= 90)
        {
            newHeightRaw = image.Width * sinAngle + image.Height * cosAngle;
            newWidthRaw = image.Width * cosAngle + image.Height * sinAngle;
        }
        else if (Math.Abs(degree) &lt;= 180)
        {
            newHeightRaw = image.Width * Math.Sin(Math.PI * (180 - degree) / 180.0) + image.Height * Math.Sin(Math.PI * (degree - 90) / 180.0);
            newWidthRaw = image.Width * Math.Cos(Math.PI * (180 - degree) / 180.0) + image.Height * Math.Cos(Math.PI * (degree - 90) / 180.0);
        }
        else
            throw new Exception("Rotation angle is not right.");
        ////Done with finding the new dimensions

        double newWidthHalf = newWidthRaw / 2;
        double newHeightHalf = newHeightRaw / 2;

        int newWidth = (int)Math.Round(newWidthRaw);
        int newHeight = (int)Math.Round(newHeightRaw);
        Image&lt;double&gt; newImage = new Image&lt;double&gt;(newWidth, newHeight);
        byte[] bitmap = new byte[newWidth * newHeight];
        double newMax = double.MinValue, newMin = double.MaxValue;
        for (int x = 0; x &lt; newHeight; x++)
        {
            for (int y = 0; y &lt; newWidth; y++)
            {
                ////Transform the coordinates to be centered at the center of the image
                double xt = x - newHeightHalf;
                double yt = y - newWidthHalf;

                double originalX = xt * cosAngle - yt * sinAngle + image.Height / 2.0;
                double originalY = xt * sinAngle + yt * cosAngle + image.Width / 2.0;

                double fractionX = Math.Abs(originalX - (int)Math.Round(originalX));
                double fractionY = Math.Abs(originalY - (int)Math.Round(originalY));

                ////Find the neighbors of the pixel in the original image
                IList&lt;Point&lt;int&gt;&gt; neighbors = new List&lt;Point&lt;int&gt;&gt; { new Point&lt;int&gt;{x = (int)Math.Round(originalX), y = (int)Math.Round(originalY), weight = (1 - fractionX) * (1 - fractionY)}, 
                                                           new Point&lt;int&gt;{x = (int)Math.Round(originalX), y = (int)Math.Round(originalY) + 1, weight = fractionX * (1 - fractionY)},
                                                           new Point&lt;int&gt;{x = (int)Math.Round(originalX) + 1, y = (int)Math.Round(originalY), weight = (1 - fractionX) * fractionY},
                                                           new Point&lt;int&gt;{x = (int)Math.Round(originalX) + 1, y = (int)Math.Round(originalY) + 1, weight = fractionX * fractionY}
                                                         };
                bool hasData = false;
                double newData = 0;
                double weight = 0;
                foreach (var neighbor in neighbors)
                {
                    if (neighbor.x &gt;= 0 &amp;&amp; neighbor.x &lt; image.Height
                        &amp;&amp; neighbor.y &gt;= 0 &amp;&amp; neighbor.y &lt; image.Width
                       )
                    {
                        hasData = true;
                        newData += neighbor.weight * image.Data[(neighbor.x) * image.Width + neighbor.y];
                        weight += neighbor.weight;
                    }
                }                    
                if (hasData &amp;&amp; weight != 0)
                {
                    newData /= weight;
                    newImage.Data[x * newWidth + y] = newData;
                }
            }                
        }            
    } 
</code></pre>

<p><a href="https://i.sstatic.net/dyDw6.png" rel="noreferrer"><img src="https://i.sstatic.net/dyDw6.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2016-02-20T05:41:31.877Z
After you fix the main problem, if you want better image quality you should try bicubic interpolation instead of bilinear.  http://blog.demofox.org/2015/08/15/resizing-images-with-bicubic-interpolation/
Alan Wolfe
<image-processing>
Rotate image around its center
user2719
# Rotate image around its center

(Asked by: username_0 on 2016-02-19)

I'm struggling with a problem with image rotation. I'm using the idea of "Rotation by area mapping" in the following link:
<http://www.leptonica.com/rotation.html>

My c# codes are as follows. The idea is first to find the dimension of the rotated image, then find the gray level of each pixel in the rotated image. To find the gray level for each pixel, the pixel coordinates are transformed back to the coordinates in the original image (called original pixel). The original pixel has fractional coordinate values, then the bilinear interpolation method in the link is used to obtain the gray level of the pixel in the rotated image. My codes did most of the job, except there are some clipping at the corners in the rotated image. I put the rotated images of a 16\*16 image, we can clearly see the effect: The bottom line in the 45 degree rotated has no gray data; in the 90 degree rotated one, the vertical line in the left has no gray data; and in the 180 degree rotated one, both the vertical line in the left and the horizontal line on the top have no gray. I believe it's caused by the coordinate computation, and I could modify my code by subtracting one of the coordinates by 1 to achieve correct rotation by a certain angle, but it doesn't work for the other case. So I guess there must be something I ignored in my codes. Any suggestions and comments are appreciated!

```
public void RotateImageClockwise(double degree)
{
    Image<double> image = (Image<double>)(PreservedRawImage.Clone());      
        double sinAngle = Math.Sin(Math.PI * degree / 180.0);
        double cosAngle = Math.Cos(Math.PI * degree / 180.0);
        double newHeightRaw, newWidthRaw;

        ////Find the new dimensions of the rotated image.
        if (Math.Abs(degree) <= 90)
        {
            newHeightRaw = image.Width * sinAngle + image.Height * cosAngle;
            newWidthRaw = image.Width * cosAngle + image.Height * sinAngle;
        }
        else if (Math.Abs(degree) <= 180)
        {
            newHeightRaw = image.Width * Math.Sin(Math.PI * (180 - degree) / 180.0) + image.Height * Math.Sin(Math.PI * (degree - 90) / 180.0);
            newWidthRaw = image.Width * Math.Cos(Math.PI * (180 - degree) / 180.0) + image.Height * Math.Cos(Math.PI * (degree - 90) / 180.0);
        }
        else
            throw new Exception("Rotation angle is not right.");
        ////Done with finding the new dimensions

        double newWidthHalf = newWidthRaw / 2;
        double newHeightHalf = newHeightRaw / 2;

        int newWidth = (int)Math.Round(newWidthRaw);
        int newHeight = (int)Math.Round(newHeightRaw);
        Image<double> newImage = new Image<double>(newWidth, newHeight);
        byte[] bitmap = new byte[newWidth * newHeight];
        double newMax = double.MinValue, newMin = double.MaxValue;
        for (int x = 0; x < newHeight; x++)
        {
            for (int y = 0; y < newWidth; y++)
            {
                ////Transform the coordinates to be centered at the center of the image
                double xt = x - newHeightHalf;
                double yt = y - newWidthHalf;

                double originalX = xt * cosAngle - yt * sinAngle + image.Height / 2.0;
                double originalY = xt * sinAngle + yt * cosAngle + image.Width / 2.0;

                double fractionX = Math.Abs(originalX - (int)Math.Round(originalX));
                double fractionY = Math.Abs(originalY - (int)Math.Round(originalY));

                ////Find the neighbors of the pixel in the original image
                IList<Point<int>> neighbors = new List<Point<int>> { new Point<int>{x = (int)Math.Round(originalX), y = (int)Math.Round(originalY), weight = (1 - fractionX) * (1 - fractionY)}, 
                                                           new Point<int>{x = (int)Math.Round(originalX), y = (int)Math.Round(originalY) + 1, weight = fractionX * (1 - fractionY)},
                                                           new Point<int>{x = (int)Math.Round(originalX) + 1, y = (int)Math.Round(originalY), weight = (1 - fractionX) * fractionY},
                                                           new Point<int>{x = (int)Math.Round(originalX) + 1, y = (int)Math.Round(originalY) + 1, weight = fractionX * fractionY}
                                                         };
                bool hasData = false;
                double newData = 0;
                double weight = 0;
                foreach (var neighbor in neighbors)
                {
                    if (neighbor.x >= 0 && neighbor.x < image.Height
                        && neighbor.y >= 0 && neighbor.y < image.Width
                       )
                    {
                        hasData = true;
                        newData += neighbor.weight * image.Data[(neighbor.x) * image.Width + neighbor.y];
                        weight += neighbor.weight;
                    }
                }                    
                if (hasData && weight != 0)
                {
                    newData /= weight;
                    newImage.Data[x * newWidth + y] = newData;
                }
            }                
        }            
    } 

```

[![enter image description here](https://i.sstatic.net/dyDw6.png)](https://i.sstatic.net/dyDw6.png)

**Tags:** image-processing

> **Comments:**
> **username_1**: After you fix the main problem, if you want better image quality you should try bicubic interpolation instead of bilinear.  http://blog.demofox.org/2015/08/15/resizing-images-with-bicubic-interpolation/  

---

## 2 answers

### Answer by username_2 on 2016-02-20. Score: 3

This is more of a long comment, with an image, than an answer.

Seems to me, at a quick glance, that you consider your pixels sample point to be at integer values. Instead in reality sample is located offset 0.5 pixels of this location.

[![enter image description here](https://i.sstatic.net/0UGXJ.png)](https://i.sstatic.net/0UGXJ.png)

**Image 1**: Is the pixel sample located at its integer coordinate? Or is it located offset by 0.5 pixels?


---

### Answer by username_3 on 2016-02-22. Score: 3

Thanks for the replies. According to username_2's comments, I modified my codes as below,

```
public void RotateImageClockwise(double degree)
    {
        Image<double> image = (Image<double>)(PreservedRawImage.Clone());      
        double sinAngle = Math.Sin(Math.PI * degree / 180.0);
        double cosAngle = Math.Cos(Math.PI * degree / 180.0);
        double newHeightRaw, newWidthRaw;

        if (Math.Abs(degree) <= 90)
        {
            newHeightRaw = image.Width * Math.Sin(Math.PI * (Math.Abs(degree)) / 180.0) + image.Height * Math.Cos(Math.PI * Math.Abs(degree) / 180.0);
            newWidthRaw = image.Width * Math.Cos(Math.PI * Math.Abs(degree) / 180.0) + image.Height * Math.Sin(Math.PI * (Math.Abs(degree)) / 180.0);
        }
        else if (Math.Abs(degree) <= 180)
        {
            newHeightRaw = image.Width * Math.Sin(Math.PI * (180 - Math.Abs(degree)) / 180.0) + image.Height * Math.Sin(Math.PI * (Math.Abs(degree) - 90) / 180.0);
            newWidthRaw = image.Width * Math.Cos(Math.PI * (180 - Math.Abs(degree)) / 180.0) + image.Height * Math.Cos(Math.PI * (Math.Abs(degree) - 90) / 180.0);
        }

        int newWidth = (int)Math.Round(newWidthRaw);
        int newHeight = (int)Math.Round(newHeightRaw);
        Image<double> newImage = new Image<double>(newWidth, newHeight);
        for (int x = 0; x < newHeight; x++)
        {
            for (int y = 0; y < newWidth; y++)
            {
                double xt = x - (newHeightRaw - 1) / 2.0;
                double yt = y - (newWidthRaw - 1) / 2.0;

                double originalX = xt * cosAngle - yt * sinAngle + (image.Height - 1) / 2.0;
                double originalY = xt * sinAngle + yt * cosAngle + (image.Width - 1) / 2.0;

                double fractionX = Math.Abs(originalX - (int)Math.Round(originalX));
                double fractionY = Math.Abs(originalY - (int)Math.Round(originalY));

                IList<Point<int>> neighbors = new List<Point<int>> { new Point<int>{x = (int)Math.Round(originalX), y = (int)Math.Round(originalY), weight = (1 - fractionX) * (1 - fractionY)}, 
                                                           new Point<int>{x = (int)Math.Round(originalX), y = (int)Math.Round(originalY) + 1, weight = fractionX * (1 - fractionY)},
                                                           new Point<int>{x = (int)Math.Round(originalX) + 1, y = (int)Math.Round(originalY), weight = (1 - fractionX) * fractionY},
                                                           new Point<int>{x = (int)Math.Round(originalX) + 1, y = (int)Math.Round(originalY) + 1, weight = fractionX * fractionY}
                                                         };
                bool hasData = false;
                double newData = 0;
                double weight = 0;
                foreach (var neighbor in neighbors)
                {
                    if (neighbor.x >= 0 && neighbor.x < image.Height
                        && neighbor.y >= 0 && neighbor.y < image.Width
                        && (neighbor.x) * image.Width + neighbor.y < m_preservedImageValidDataBitmap.Length && m_preservedImageValidDataBitmap[(neighbor.x) * image.Width + neighbor.y] == 'T')
                    {
                        hasData = true;
                        newData += neighbor.weight * image.Data[(neighbor.x) * image.Width + neighbor.y];
                        weight += neighbor.weight;
                    }
                }                    
                if (hasData && weight != 0)
                {
                    newData /= weight;
                    newImage.Data[x * newWidth + y] = newData;                        
                }
            }                
        }
    } 

```

Now it's working for 90 and 180 degree rotations. But for 45 degree rotation, the bottom line is still blank, which makes me crazy. I'm guessing it's due to the computation inaccuracy when the new image width and height are computed, but I still can't figure it out.

[![enter image description here](https://i.sstatic.net/OQB9E.png)](https://i.sstatic.net/OQB9E.png)


---

