Click here to Skip to main content
13,354,605 members (64,898 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 11 Sep 2009

affine transformations for images

, 11 Sep 2009
Rate this:
Please Sign up or sign in to vote.
image transformations for C# .NET CF
Download class library only (source) - 43.8 KB

Download SampleApplication for windows mobile (source) - 97.79 KB  


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)


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
    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. 

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;


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);
matTrans1.Translation((float)(buffer.Width) / 2.0f, (float)(buffer.Height) / 2.0f);
    Transforms.ImageTransform(buffer, srcBuffer, matTrans1 * (matRot * matTrans0), ImageTransforms.Transforms.NearestNeigbour);
//now buffer contains our transformed image


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Poland Poland
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionFor Windows Pin
sasan3d4-Oct-13 6:13
membersasan3d4-Oct-13 6:13 
QuestionHow to acheive it in real time !! Pin
mani_manu210-Mar-10 18:49
membermani_manu210-Mar-10 18:49 
AnswerRe: How to acheive it in real time !! Pin
luker716-Mar-10 8:53
memberluker716-Mar-10 8:53 
QuestionPerspective Pin
Ashok Gowtham30-Jan-10 16:59
memberAshok Gowtham30-Jan-10 16:59 
AnswerRe: Perspective Pin
luker731-Jan-10 12:52
memberluker731-Jan-10 12:52 
GeneralRe: Thanks! - it works fine! Pin
Ashok Gowtham1-Feb-10 4:11
memberAshok Gowtham1-Feb-10 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 11 Sep 2009
Article Copyright 2009 by luker7
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid