Results 1 to 20 of 21

Thread: QImage Rotation and Translation & Invert Specific Colors

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Question QImage Rotation and Translation & Invert Specific Colors

    Good Day,

    Part 1 of 2

    Im superimposing 2 images(Overlaying images, with the 2nd image having a 50% opacity)

    Now I have a rotation angle, a Row Offset & Column Offset Value.

    When I rotate the 2nd image... it works
    When a call the TRANSLATE function nothing happens!

    Overlay Images Function:
    Qt Code:
    1. primaryImage.load(imageFileName1);
    2.  
    3. secondaryImage.load(imageFileName2);
    4. secondaryImage.invertPixels(); //Inverting Grayscale Image
    5.  
    6. QTransform rotating;
    7. rotating.rotate(RotationAngle);
    8. secondaryImage = secondaryImage.transformed(rotating); // Works
    9. QTransform translating;
    10. translating.translate(RowOffset, ColOffset);
    11. secondaryImage = secondaryImage.transformed(translating); // Does not work
    12.  
    13. paintScreen = true;
    14. update();
    To copy to clipboard, switch view to plain text mode 


    this is what I got:
    Qt Code:
    1. //Paint Function
    2. if(paintScreen = true){
    3.  
    4. //Repaint Primary Image
    5. painter.drawImage(QPoint(0, 0), primaryImage);
    6. painter.setOpacity(0.5);// drawn at n% opacity
    7. painter.drawImage(QPoint(0, 0), secondaryImage); }
    To copy to clipboard, switch view to plain text mode 

    Ideas Please
    Solutions welcome


    Part 2 of 2

    Also Instead of inverting colors of 2nd image(grayscale)
    Is there a way to convert the whole image into like a "Red Scale" or any other color of different shades

    Solution
    Kind Regards
    Last edited by 2lights; 14th August 2013 at 09:22. Reason: updated contents

  2. #2
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QImage Rotation and Translation & Invert Specific Colors

    reference from THIS

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char** argv)
    4. {
    5. QApplication app(argc, argv);
    6. QPixmap pixmap("logo.png");
    7. QImage image = pixmap.toImage();
    8. QRgb col;
    9. int gray;
    10. int width = pixmap.width();
    11. int height = pixmap.height();
    12. for (int i = 0; i < width; ++i)
    13. {
    14. for (int j = 0; j < height; ++j)
    15. {
    16. col = image.pixel(i, j);
    17. gray = qGray(col);
    18. image.setPixel(i, j, qRgb(gray, gray, gray));
    19. }
    20. }
    21. pixmap = pixmap.fromImage(image);
    22. QLabel label;
    23. label.setPixmap(pixmap);
    24. label.show();
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    following example convert the image into an gray scale image.
    HERE:
    gray = qGray(col);
    convert the QRgb color set into a gray color.
    You can use QRed, QBlue etc. according to your need.
    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

  3. The following user says thank you to karankumar1609 for this useful post:

    2lights (14th August 2013)

  4. #3
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QImage Rotation and Translation & Invert Specific Colors

    Part 1 of 2
    Any ideas

    Part 2 of 2
    Tried it, this is what i get
    No compile error, But nothing happens no change to color
    Though
    I get this debug output err:
    QImage:: pixel: coordinate (320,478) out of range
    QImage:: setPixel: coordinate (320,478) out of range
    QImage:: pixel: coordinate (320,479) out of range
    ...

    Qt Code:
    1. void Compare::OverLayImages()
    2. {
    3. primaryImage.load(imageFileName1);
    4.  
    5. QRgb colour;
    6. int newCol;
    7. int width = primaryImage.width();
    8. int height = primaryImage.height();
    9.  
    10. for(int i = 0; i <= width; ++i)
    11. {
    12. for (int j = 0; j <= height; ++j)
    13. {
    14. colour = primaryImage.pixel(i, j);
    15. newCol = qRed(colour);
    16. primaryImage.setPixel(i, j, qRgb(newCol, newCol, newCol));
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QImage Rotation and Translation & Invert Specific Colors

    for(int i = 0; i <= width; ++i)
    for (int j = 0; j <= height; ++j)
    Loop should be i < width, and j < height.

    mentioned code should work. may be you are doing it differently.
    Where do you use this primaryImage?

    Have you tried Image Composition Example
    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

  6. #5
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QImage Rotation and Translation & Invert Specific Colors

    i < width, and j < height.
    Same problem
    QImage::setPixel: Index -16645630 out of range
    QImage::setPixel: Index -16645630 out of range
    ....
    Nothing happens

    Part of my program requires me to paint the image in a label
    Could that be the problem... I doubt it thiugh because the editing takes place before the paint function is called

    paint function
    Qt Code:
    1. painter.setWindow(QRect(0,0,totalWidth,totalHeight));
    2. update();
    3.  
    4. //Repaint Primary Image
    5. painter.drawImage(QPoint(0, 0), primaryImage);
    To copy to clipboard, switch view to plain text mode 

    painting no problem...
    color is the issue!

    & Any ideas on the translation of image

  7. #6
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QImage Rotation and Translation & Invert Specific Colors

    You require to set a result image on QLabel..
    The better way to do this, is have a result image and paint on it (Outside the paint event).
    After loading of primaryImage and secondaryImage you can do your calculations on images and create a result image

    Qt Code:
    1. void ImageComposer::recalculateResult()
    2. {
    3. QPainter::CompositionMode mode = currentMode();
    4.  
    5. QPainter painter(&resultImage);
    6. painter.setCompositionMode(QPainter::CompositionMode_Source);
    7. painter.fillRect(resultImage.rect(), Qt::transparent);
    8. painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    9. painter.drawImage(0, 0, destinationImage);
    10. painter.setCompositionMode(mode);
    11. painter.drawImage(0, 0, sourceImage);
    12. painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
    13. painter.fillRect(resultImage.rect(), Qt::white);
    14. painter.end();
    15.  
    16. resultLabel->setPixmap(QPixmap::fromImage(resultImage));
    17. }
    To copy to clipboard, switch view to plain text mode 

    the above code is the part of the Image Composition Example

    As per your program need you have to look at the Image Composition Example
    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

  8. The following user says thank you to karankumar1609 for this useful post:

    2lights (14th August 2013)

  9. #7
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Post Re: QImage Rotation and Translation & Invert Specific Colors

    Getting there...

    Remember, the image im loading is grayscale
    Im converting it to Red/Green or Blue scale

    I tried for testing purposes only,
    I uploaded a color image...
    the ConvertColor Function
    instead of converting everyting to red(different shades of red), it made it black & white
    but no "Index out of range error"

    When I upload a grayscale image(program needs to do)
    I get an Index out of range err in application output

    Qt Code:
    1. QImage::setPixel: Index -16645630 out of range
    2. QImage::setPixel: Index -16645630 out of range
    3. QImage::setPixel: Index -16645630 out of range
    4. QImage::setPixel: Index -16514044 out of range
    5. QFSFileEngine::open: No file name specified
    To copy to clipboard, switch view to plain text mode 
    In the label the original image(grayscale image) is uploadded
    no change in color

    Qt Code:
    1. primaryImage.load(imageFileName1);
    2.  
    3. QRgb colour;
    4. int newCol;
    5. int width = primaryImage.width();
    6. int height = primaryImage.height();
    7.  
    8. for(int i = 0; i < width; ++i)
    9. {
    10. for (int j = 0; j < height; ++j)
    11. {
    12. colour = primaryImage.pixel(i, j);
    13. newCol = qRed(colour);
    14. primaryImage.setPixel(i, j, qRgb(newCol, newCol, newCol));
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    Im not seeing the problem,
    Solution?

    Translation SOLVED.... Thanks

  10. #8
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QImage Rotation and Translation & Invert Specific Colors

    I tried for testing purposes only,
    I uploaded a color image...
    the ConvertColor Function
    instead of converting everyting to red(different shades of red), it made it black & white
    but no "Index out of range error"
    Your grayscale image might be of different format.
    Load it in QImage::Format_RGB32 and then use the same function. it will solve your index out of range problem.

    try:
    Qt Code:
    1. ...
    2. primaryImage.load(imageFileName1);
    3. primaryImage = primaryImage.convertToFormat(QImage::Format_RGB32);
    4. ...
    To copy to clipboard, switch view to plain text mode 
    Last edited by karankumar1609; 14th August 2013 at 13:31.
    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QImage Rotation and Translation & Invert Specific Colors

    Quote Originally Posted by 2lights View Post
    Remember, the image im loading is grayscale
    If the image is originally grayscale then it is likely that it is indexed and if so, changing colours of the image can be simplified to modify just the color table.

    E.g. to convert your image to shades of red, you'd do:

    Qt Code:
    1. QImage img(...);
    2. QVector<QRgb> colorTable = img.colorTable();
    3. for(int i=0;i<colorTable.size();++i) {
    4. colorTable[i] = qRed(qGray(colorTable[i]));
    5. }
    6. img.setColorTable(colorTable);
    To copy to clipboard, switch view to plain text mode 

    Note -- the image has to be indexed and not true color!
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #10
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QImage Rotation and Translation & Invert Specific Colors

    I created an if statement to check if grayscale.
    image1.AllGray();

    if(false)
    @karankumar1609
    That did solve the out of scope issue
    though No Colour change on image

    if(true){
    @wysota
    implemented wysota solution
    Unfortunately, No Image is displayed at all
    }

    It seemed simple at first,
    But now im just hitting barriers

    Just to reiterate my problem
    (Most of images are black and white and shades of gray)
    and convert entire image to a shade of green, red or blue

    Where am i going wrong?

  13. #11
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QImage Rotation and Translation & Invert Specific Colors

    well the above example should work for you.,
    Please paste your code for more clarification on what exactly you are doing.
    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QImage Rotation and Translation & Invert Specific Colors

    Did you check if the image was indexed prior to implementing my solution?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #13
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QImage Rotation and Translation & Invert Specific Colors

    Not sure if im checking if image is indexed correctly
    this is what i got: an if statement around your solution
    Qt Code:
    1. if(image1.allGray()){ //if grayscale than it should be indexed... I think
    2. QVector<QRgb> colorTable = image1.colorTable();
    3. for(int i=0;i<colorTable.size();++i) {
    4. colorTable[i] = qRed(qGray(colorTable[i]));
    5. }
    To copy to clipboard, switch view to plain text mode 
    No image is shown

  16. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QImage Rotation and Translation & Invert Specific Colors

    Quote Originally Posted by 2lights View Post
    When a call the TRANSLATE function nothing happens!
    That's correct, translating an image will have no effect. The result of calling transformed() is a new image that can encompass the whole original image after applying a transformation to it. When rotating an image, the new resulting image will be larger and will contain all the contents or the original image. When translating, the new image will have the same size as the original one and will contain the whole original image -- which is identical to the original image.

    this is what I got:
    Qt Code:
    1. //Paint Function
    2. if(paintScreen = true){
    3.  
    4. //Repaint Primary Image
    5. painter.drawImage(QPoint(0, 0), primaryImage);
    6. painter.setOpacity(0.5);// drawn at n% opacity
    7. painter.drawImage(QPoint(0, 0), secondaryImage); }
    To copy to clipboard, switch view to plain text mode 
    That's very easy:
    Qt Code:
    1. painter.drawImage(0, 0, primaryImage);
    2. painter.setOpacity(0.5);
    3. painter.drawImage(colShift, rowShift, secondaryImage);
    To copy to clipboard, switch view to plain text mode 
    You can do the same with rotation, by the way:

    Qt Code:
    1. painter.drawImage(..., ..., primaryImage);
    2. painter.save();
    3. painter.translate(primaryImage.width()/2, primaryImage.height()/2);
    4. painter.rotate(45);
    5. painter.drawImage(..., ..., secondaryImage);
    6. painter.restore();
    To copy to clipboard, switch view to plain text mode 

    Is there a way to convert the whole image into like a "Red Scale" or any other color of different shades
    Sure, iterate over all pixels and change their color in any way you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. The following user says thank you to wysota for this useful post:

    2lights (14th August 2013)

Similar Threads

  1. Replies: 0
    Last Post: 23rd July 2013, 13:19
  2. Graph rotation
    By jomarin in forum Qwt
    Replies: 2
    Last Post: 16th August 2010, 09:21
  3. Ellipse and rotation
    By navi1084 in forum Qt Programming
    Replies: 3
    Last Post: 9th March 2009, 10:43
  4. Rotation on Qframe
    By Pharell in forum Qt Programming
    Replies: 11
    Last Post: 2nd April 2008, 16:31
  5. Replies: 3
    Last Post: 15th March 2006, 11:44

Tags for this Thread

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.