13,042,819 members (88,910 online)
alternative version

#### Stats

25.9K views
18 bookmarked
Posted 11 Sep 2009

# affine transformations for images

, 11 Sep 2009
 Rate this:
image transformations for C# .NET CF

## Introduction

Here is it , My First article for codeproject, I hope you enjoy it. To the point:

Unfortunately CF doesn't support this kind of operation on Images in comaprison with .NET framework.

So, this class library implements affine transformations  on images such as translation, rotation, scaling, schear. Algorithm isn't efficient but  it's simple. This code shoudn't be used for real-time transformations, in that case you need something more efficient, something which apply gpu for the work, not only poor, lonesome cpu ;> (for instance DirectX API)

## Algorithm

`Transforms.ImageTransform(Bitmap dst, Bitmap src, Matrix matTrans, Interpolate sampler)`

-puts source image pixels on destination image using tranformation Matrix with defined interpolation method.  HOW?

First of all we need to find reverse transformation to the defined. WHY?

If you transform pixels of source bitmap using defined matrix and put them on destination bitmap, an output image may have "holes". Just imagine the easiest transformation like 2x scaling:

Solution on that problem is transforming in opposite direction. Just go through all of the destination bitmap's pixels  transforming theirs coords using inverse matrix to get to know where pixel should go on source bitmap, on the other hand we know where source pixel should go on destination bitmap. Now we have to fill destination pixel with color of source pixel using some interpolation method.  If transformed pixel doesn't go on source bitmap, destination pixel may be kept unchanged or filled with defined background color.

What to do with float coords of source pixel? (interpolation problem).

I implemented 2 methods:

• Nearest Neigbour interpolation - we just round coords to the nearest integer number
```unsafe
public static void NearestNeigbour(Pixel* dstPixel, Pixel* src, Vector2 vec, int srcStrideInPixels)
{
int tx, ty;
tx = Convert.ToInt32(vec.x); // Note: Convert.ToInt32 rounds float numbers!
ty = Convert.ToInt32(vec.y); // Note: Convert.ToInt32 rounds float numbers!
*dstPixel= src[ty * srcStrideInPixels + tx];
}
```

• Bilinear interpolation - we sum colors of  4 neigbour pixels with integer coords (using floor, ceiling operations on float coords) with appropriated wages computed as follows:

(1.0f - dinstance in X dimension between transformed and neighbour pixel) * (1.0f - distance in Y dimension)

if  you take a look closer to the picture below ...

and let say transformed pixel P has coords (4.25f, 11.3f) then pixels

P0 has coords (4, 11) and its wage is (1.0f -0.25f) * (1.0f - 0.3f)
P1 has coords (5, 11) and its wage is (1.0f -0.75f) * (1.0f - 0.3f)
P2 has coords (4, 12) and its wage is (1.0f -0.25f) * (1.0f - 0.7f)
P3 has coords (5, 12) and its wage is (1.0f -0.75f) * (1.0f - 0.7f)

Obviously, the closer neigbour pixel to the transformed pixel is, the more essential  becomes in a final color.

```unsafe
public static void BilinearInterpolation(Pixel* dstPixel, Pixel* src, Vector2 vec, int srcStrideInPixels)
{
Pixel result;
int tx, ty;
float f1, f2;
tx = (int)vec.x; // Note: this is just a truncation of float numbers
ty = (int)vec.y; // Note: this is just a truncation of float numbers
f1 = vec.x - (float)tx;
f2 = vec.y - (float)ty;
src = src + ty * srcStrideInPixels + tx;
Pixel upperLeft = *src;
Pixel upperRight = src[1];
Pixel bottomLeft = src[srcStrideInPixels];
Pixel bottomRight = src[srcStrideInPixels + 1];
result.A = 0;
result.R = (Byte)((float)upperLeft.R * (1.0f - f1) * (1.0f - f2) + (float)upperRight.R * f1 * (1.0f - f2) +
(float)bottomLeft.R * (1.0f - f1) * f2 + (float)bottomRight.R * f1 * f2);
result.G = (Byte)((float)upperLeft.G * (1.0f - f1) * (1.0f - f2) + (float)upperRight.G * f1 * (1.0f - f2) +
(float)bottomLeft.G * (1.0f - f1) * f2 + (float)bottomRight.G * f1 * f2);
result.B = (Byte)((float)upperLeft.B * (1.0f - f1) * (1.0f - f2) + (float)upperRight.B * f1 * (1.0f - f2) +
(float)bottomLeft.B * (1.0f - f1) * f2 + (float)bottomRight.B * f1 * f2);
*dstPixel = result;
}
```

## Efficiency

As you can see the efficiency of algorithm depends on how large destination image is and which inerpolation method you choose.

Any improvements?

All math opparation could be replaced with integer operations only. As you know operation on integer numbers are much quicker than float operations. But we lose precision of math operations. To gain precision on integer number just multiply by some large number ( the best power of 2 and do bit shift operation), then do desired operation and finally divide by the same number (bit shift operations would be recommended).

Bitmap is ordered set of pixels. Using that fact you can create incremental algorithm.

## Using the code

```using ImageTransforms;
```
```Bitmap buffer = new Bitmap(128,128);
Bitmap srcBuffer = new Bitmap(bmpArrows); //load image for transformations
Matrix matRot = new Matrix();
Matrix matTrans1 = new Matrix();
Matrix matTrans0 = new Matrix();
matTrans0.Translation(-(float)(srcBuffer.Width) / 2.0f, -(float)(srcBuffer.Height) / 2.0f);
matRot.Rotation(angle);
matTrans1.Translation((float)(buffer.Width) / 2.0f, (float)(buffer.Height) / 2.0f);
unsafe
{
Transforms.ImageTransform(buffer, srcBuffer, matTrans1 * (matRot * matTrans0), ImageTransforms.Transforms.NearestNeigbour);
}
//now buffer contains our transformed image
```

## Share

 Poland
No Biography provided

## You may also be interested in...

 First Prev Next
 For Windows sasan3d4-Oct-13 5:13 sasan3d 4-Oct-13 5:13
 How to acheive it in real time !! mani_manu210-Mar-10 17:49 mani_manu2 10-Mar-10 17:49
 Re: How to acheive it in real time !! luker716-Mar-10 7:53 luker7 16-Mar-10 7:53
 Perspective Ashok Gowtham30-Jan-10 15:59 Ashok Gowtham 30-Jan-10 15:59
 Re: Perspective luker731-Jan-10 11:52 luker7 31-Jan-10 11:52
 For perspective shots you have to define projection's plane: position(center_of_plane), size, normal. If plane's normal is not parallel to Z-axis, do nomalize transformation(translation + rotation). In general projection matrix is a conjunction of matrices: translation(-center_of_projection) - for perspective only, shearXY and projection onto projection's plane. In Ortho projection shearXY is an identity matrix and projection sets Z-coord of vertex to plane's Z-coord. In perspective projection, if direction_of_projection is parallel to Z-axis (if not - shear is needed), projection on the plane is very simple matrix: 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1/d 0 where projection's plane is Z=d. This matrix sets homogeneous coord to z/d, so divide rest of coord by z/d. I hope i help you in some way. Luker
 Re: Thanks! - it works fine! Ashok Gowtham1-Feb-10 3:11 Ashok Gowtham 1-Feb-10 3:11
 Last Visit: 31-Dec-99 18:00     Last Update: 20-Jul-17 18:56 Refresh 1