Results 1 to 2 of 2

Thread: Perspective correction with QTransform

  1. #1
    Join Date
    Jun 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Perspective correction with QTransform

    I have a QGraphicsScene and in this scene I have a QGraphicsPixmapItem. In addition I have four points (QGraphicsItem) in the scene that I can move around on top (z = 1) of the QGraphicsPixmapItem. By placing these points and mapping them to the scene I can select an area of the pixmap:

    (picture: cap-1)

    At the moment I am just using the top left and the bottom right points to create a rectangle to copy an area:
    Qt Code:
    1. QPixmap copy = pixmap.copy(upperLeftX, upperLeftY, rectWidth, rectHeight);
    To copy to clipboard, switch view to plain text mode 

    I can now copy any rectangular area, which is nice, but what I would like to do in addition to this is to position the points in any quadrilateral arrangement:

    (picture: cap-2)

    I then want to perform a perspective correction of that area and finally make a copy of the result.

    So what I want is to make picture "cap-2" look like picture "cap-1". Any ideas regarding how I could aproach this problem? Is this possible with only the QPixmap and the four point coordinates that are available? I'm not expecting it to work perfectly. A fairly good aproximation would be OK.


    So far I have been thinking along these lines:

    1. Use the information from the mapped points to make a QTransform and then use this QTransform to transform the whole pixmap.
    Qt Code:
    1. QTransform transform;
    2. // create transform (use transform.rotate(degree, Qt::?Axis)? or make a QTransform from scratch?)
    3. QPixmap transformedPixmap = originalPixmap.transformed(transform);
    To copy to clipboard, switch view to plain text mode 
    Then select the area I want and copy it:
    Qt Code:
    1. transformedPixmap.copy(upperLeftX, upperLeftY, rectWidth, rectHeight);
    To copy to clipboard, switch view to plain text mode 
    (I don't know how to make a QTransform that will perform the required transformation. Suggestions?)

    OR

    2. Create a QPolygonF with the mapped points. Then use the quadToSquare function to create the transform (bool QTransform::quadToSquare ( const QPolygonF & quad, QTransform & trans )). I am not able to get this to work (quadToSquare() returns false).
    Qt Code:
    1. QPolygonF area(5);
    2. area << selector->getUpperLeftNodePoint() << selector->getLowerLeftNodePoint() <<
    3. selector->getLowerRightNodePoint() << selector->getUpperRightNodePoint() <<
    4. selector->getUpperLeftNodePoint();
    5. QTransform trans;
    6. bool transOK = QTransform::quadToSquare(area, trans);
    7. if (transOK) cout << "Transformed: true" << endl;
    8. else cout << "Transformed: false" << endl;
    To copy to clipboard, switch view to plain text mode 
    If you have any suggestions regarding how I could make one of my approaches work correctly or if you know some other way to tackle this problem I would be very grateful. Questions, general tips, detailed instructions, code and suggested reading are all welcome.

    Thanks

    -----------
    OS: Mac OS X 10.5.3
    Qt version: 4.4.0

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Perspective correction with QTransform

    Quote Originally Posted by dustparticle View Post
    2. Create a QPolygonF with the mapped points. Then use the quadToSquare function to create the transform (bool QTransform::quadToSquare ( const QPolygonF & quad, QTransform & trans )). I am not able to get this to work (quadToSquare() returns false).
    Qt Code:
    1. QPolygonF area(5);
    2. area << selector->getUpperLeftNodePoint() << selector->getLowerLeftNodePoint() <<
    3. selector->getLowerRightNodePoint() << selector->getUpperRightNodePoint() <<
    4. selector->getUpperLeftNodePoint();
    5. QTransform trans;
    6. bool transOK = QTransform::quadToSquare(area, trans);
    7. if (transOK) cout << "Transformed: true" << endl;
    8. else cout << "Transformed: false" << endl;
    To copy to clipboard, switch view to plain text mode 
    I think that Qt doesn't expect to see a quad with five vertices. Try omitting the second upper left node point.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.