Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / WinMobile

affine transformations for images

4.71/5 (6 votes)
11 Sep 2009CPOL3 min read 40K   1.6K  
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)