Click here to Skip to main content
15,868,141 members
Articles / Mobile Apps / Windows Mobile

affine transformations for images

Rate me:
Please Sign up or sign in to vote.
4.71/5 (6 votes)
11 Sep 2009CPOL3 min read 39K   1.6K   20   6
image transformations for C# .NET CF
Download class library only (source) - 43.8 KB

Download SampleApplication for windows mobile (source) - 97.79 KB  

Image 1

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 

C#
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:

Image 2

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

Image 3

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. 

C#
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 

C#
using ImageTransforms;
C#
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

License

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


Written By
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFor Windows Pin
sasan3d4-Oct-13 5:13
professionalsasan3d4-Oct-13 5:13 
QuestionHow to acheive it in real time !! Pin
mani_manu210-Mar-10 17:49
mani_manu210-Mar-10 17:49 
AnswerRe: How to acheive it in real time !! Pin
luker716-Mar-10 7:53
luker716-Mar-10 7:53 
QuestionPerspective Pin
Ashok Gowtham30-Jan-10 15:59
Ashok Gowtham30-Jan-10 15:59 
AnswerRe: Perspective Pin
luker731-Jan-10 11:52
luker731-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
GeneralRe: Thanks! - it works fine! Pin
Ashok Gowtham1-Feb-10 3:11
Ashok Gowtham1-Feb-10 3: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.