Bilinear Transformation of Bitmap in QGraphicsScene
-
Dear Forum
I'm looking for a way to transform / deform an image bi-linearly.
I would like to give the coordinates of corners of an image in a QGraphicsScene and let the image be deformed to fill the new trapezoidal area.
A good idea what effect I'm looking for is in the image:
"Mesh Transformation":http://www.psastras.com/images/screenshots/mesh1_b.pngI found quadToQuad to let me generate a QTransform, but applying this to an QGraphicsPixmapItem actually deforms the image perspectively:
If you use a chess board pattern as image, you will see, that the length of the black/white pattern is not equal along the edge from the 'front' corner to the 'back' corner.If an image is deformed bi-linearly (like the typical approach with textures and texture coordinates in OpenGL) then the pattern of the chess-board would be equal lengthed along the edges of the deformed image.
Is there a way to have bi-linear deformation / texture filling for an QGraphicsScene item?
Thank you,
Michael -
There is nothing in Qt to give you this effect out of the box (without using OpenGL directly). You will have to convert the pixmap to a QImage and apply your bi-linear transform directly to the bits.
-
I think that OpenCV can do it, can't you use Qt with OpenCV?!
-
I'm sure you can, it'd be cleaner to use Qt though.
I wish there was a set of tools for this sort of thing, possibly with QtOpenCL as optional backend. -
No problem to use OpenCV together with Qt.
moellney, BTW, you may also would want to have a look at IPP (intel performance primitives) -
[quote author="metRo_" date="1291422906"]I think that OpenCV can do it, can't you use Qt with OpenCV?![/quote]
I used OpenCV (in another project) along Qt, it works nicely.
But I could not find any method to help me on bilinear deformation. All I could find is perspective de-/transformations.
Can you give me a hint which method to use?
Thanks Michael
-
moellny, please see http://opencv.willowgarage.com/documentation/cpp/geometric_image_transformations.html?highlight=bilinear
For instance, cv::warpAffine can use any interpolation method. See cv::resize for a list of all interpolation methods available
-
[quote author="infoctopus" date="1291637819"]moellny, please see http://opencv.willowgarage.com/documentation/cpp/geometric_image_transformations.html?highlight=bilinear
For instance, cv::warpAffine can use any interpolation method. See cv::resize for a list of all interpolation methods available[/quote]
Thanks for you answer, but there are no methods in your links that lead to a bilinear deformation. There is only affine deformation or perspectivic deformation. Bilinear in both context refer to the color filtering methods used.
Do not mix up the bilinear color interpolation (bilinear filtering e.g. as in "Wikipedia Bilinear Filtering":http://en.wikipedia.org/wiki/Bilinear_filtering) used for color interpolation with the general concept of a "Bilinear Interpolation":http://en.wikipedia.org/wiki/Bilinear_interpolation where the function of the corners of the [0,1]x[0,1] grid may be polygon corners of a quad leading to a deformation of a rectange in 2-dimensional space.
The problem is filling this deformed rectange.
This deformation is not a generizable as affine deformation (supported by Qt) and is not (in general) the 2d projection of a rotated rectange (perspectivic transformation supported by qt via squareToQuad method).Image a plane rectangular peace of rubber on the table in front of you, that you deform by moving one corner (still on the table). Now imagine an image on the rectangular plane. How does the image deform?
The filling of a biliner deformed rectange on the screen can be done with e.g. a nearest neighbour algorithm or with other color interpolations methods.... but this is a seperate topic:
In Qt you can set this with "Transformation mode":http://doc.troll.no/4.7/qt.html#TransformationMode-enum -
Ok, I see. I'm starting to think that it's easier to implement an algorithm for this transformation from scratch. Also good anti-aliasing method will be of need.
-
I know that Photoshop does the job perfectly. But maybe some of opensource graphic editors has this feature as well, so you could look at the implementation, e.g. gimp
-
Finally found the method(s) I was looking for in Intels
"IPP WarpBilinear (etc.)":http://software.intel.com/sites/products/documentation/hpc/composerxe/en-us/ippxe/ipp_manual_win_mac/IPPI/ippi_ch12/functn_WarpBilinear.htmBut hey, this is US$399... well... including a compiler and other goodies...
OpenCV seems to lack this functionality.