Image transformations using Qt and C++
Written on January 21, 2009, by Milot Shala.

I am studying and experimenting with the framework, I ended up writing an application which applies some image transformations on the current image and displays it below the original image. And I thought it would be great to share this information with anyone who is interested in this stuff. Here I will show just one function, how to flip the image vertically, but I have provided the full source code for the application, so you can experiment with it more. But if you come up with some nicer stuff, feel free to comment :D .

The logic of transforming the images is simple, you take a source image, you read it, grab its pixels, form new image but… you transform the pixels. Pretty straight forward but how we can achieve the logic in the code?

First we create a function, which will return QImage (a newly created QImage object), then we create an instance of the new QImage object with the same properties as the current image (same width/height and format), then we have a blank image, as we know, the graphics pixels are represented in a matrix, and our algorithm will be using two nested for loops to iterate through, the QImage object has a method called “setPixel” which allows us to create a new image pixel per pixel, we can create it by passing x and y axis of the current image, but what we want to reach, is to transform the picture vertically, so instead we transform our Y axis by subtracting the current height with one and current Y axis value within the loop as shown in the code below which is well commented.

QImage ImageDialog::flipImageVertically(QImage source_image)
{
    // create a new image instance, based on the information from the source image
    QImage new_image(source_image.width(), source_image.height(), source_image.format());
 
    // we need this variable, because when we create our new image
    // we will read each pixel, but instead of placing the same pixels
    // on our newly create one, we will transform our pixels vertically in this
    // function, so we need that Y axis to be subtracted by one and by source image's
    // Y axis, which we can see on our for loop below.
    int height_subtraction = source_image.height() - 1;
 
    for(int x = 0; x < source_image.width(); ++x)
        for(int y = 0; y < source_image.height(); ++y)
            // we create a new image from the pixels of the source image, but
            // as you can see, we are creating it pixel per pixel, so when we need the
            // pixel information, we can play with it, in this function we are transforming
            // the Y axis so we can get negative values and that will produce us
            // a flipped image.
            new_image.setPixel(x, height_subtraction - y, source_image.pixel(x, y));
 
    return new_image;
}

and below I have shown a screenshot how the image will look like after applying the vertical transformation:

And you can download the code for this project here: http://codespartan.org/files/ImageTransformations.tar.gz

The code provided here, shows you how to apply horizontal transformation, and both vertical and horizontal, the logic is the same, but in the setPixel method, you transform the X axis with the formula like this:

int horizontal_subtraction = source_image.width() - 1 - x; // the x is a local for loop variable

but the rest I will leave it to you to experiment with it.

The photo used in this demo application is copyright of Kushtrim Krasniqi (http://www.flickr.com/photos/kushtrimi_k2/3064680407/) which I’ve found it on flickr.

If you have any thoughts, feel free to comment or email me at milot.shala@gmail.com.

UPDATE: I’ve created a Windows binary, but I have tested it only Windows Vista and I don’t know if it would work on Windows XP, it’s located here: http://codespartan.org/files/ImageTransformations(Vista Build).rar.

  • http://www.admirimi.com Admirim

    Nice blog, I like the template

  • Kushtrim

    For me this is too advanced hahah
    I like your blog and especially for the fact that you used my photo to show your work.

    Well done

  • http://codespartan.org/blog/ Milot Shala

    @Kushtrim

    Well it’s not that advanced! It’s very simple if you are in the field. I used your photo because it has a special meaning for me.

  • Visar

    Hi Milot,

    I’ve been working recently with image manipulation (in C#) and my first approach was similar to the one you use here. I was trying to apply some computer vision algorithms, and this approach (pixel by pixel) is extremely slow.

    What helped me a lot was the OpenCV (Open Computer Vision) C++ library developed by Intel, which is optimized for work with their processors.

    http://sourceforge.net/projects/opencvlibrary/

    All the image processing is done in lower level, thus you will result with great performance. It also supports bit more than 500 advanced algorithms starting from basic image processing to very advanced machine learning tasks.

    Knowing you, I believe soon there will be a new blog post about OpenCV :)

  • http://codespartan.org/blog/ Milot Shala

    @Visar

    LOL, when I finish my exams I will have more time to do that, but exploring Qt and seeing it as very powerful C++ framework I want to experiment with it in various fields just to know the framework better. As for fast image manipulation I don’t know and I don’t have time now to compare them, but I will send you an email with Windows build of that application and you can test it, because application allows you to select any image.

  • http://flakron.net Flakron

    Seeing this, I might make C++ and QT Framework my main developing platform.
    Great Post!!!

  • http://ariya.blogspot.com Ariya

    FYI QImage has mirrored() function which does the mirroring already.

  • http://codespartan.org/blog/ Milot Shala

    @Ariya

    Hello Ariya,

    Thank you for visiting my blog. I’ve seen the function on the documentation already, but it was worth trying to do it my own in order that I can learn new things and for example to see how mirrored() function works just for education purposes, every time I learn new things and make something of my own I post it here but it doesn’t mean that I will use my own implementations on production, but thanks and credits goes to you guys for the great framework!

blog comments powered by Disqus

3,024 views

© Copyright Phalanx Blogosphere - Powered by Wordpress - Phalanx logo is designed by Leopard Cana