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
    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.

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

    2lights (14th August 2013)

  3. #2
    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

  4. #3
    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.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  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 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?

  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

    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.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  9. #8
    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

  10. #9
    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

    Implement this
    Qt Code:
    1. ...
    2. ...
    3. image1.setColorTable(colorTable);
    To copy to clipboard, switch view to plain text mode 

    you just calculate the color table but u need to se the new color table to image1.
    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. #10
    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

    I am setting the color table after the loop

    This is my current function to load image
    Qt Code:
    1. void Compare::getImageFileName1()
    2. {
    3. fp1 = true;
    4. QFileDialog dialog(this, "Upload");
    5. dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg *.bmp *.tif)"));
    6. dialog.setViewMode(QFileDialog::Detail);
    7.  
    8. if(dialog.exec())
    9. {
    10. imageFileName1 = dialog.selectedFiles().first();
    11. }
    12.  
    13. image1.load(imageFileName1);
    14. image1Width = image1.width();
    15. image1Height = image1.height();
    16.  
    17. if(image1.allGray()){
    18. QVector<QRgb> colorTable = image1.colorTable();
    19. for(int i=0;i<colorTable.size();++i) {
    20. colorTable[i] = qRed(qGray(colorTable[i]));
    21. }
    22. image1.setColorTable(colorTable);
    23. }
    24. imageDrawFlag1 = true;
    25. }
    To copy to clipboard, switch view to plain text mode 

    In my paint function:
    Qt Code:
    1. //Painting actual Image
    2. if(imageDrawFlag1 == true){
    3. painter.drawImage(QPoint(0, 0), image1);
    To copy to clipboard, switch view to plain text mode 

    Your time & help is much appreciated

  12. #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

    code is correct, it exactly do what you write.
    Well its my assumption you should call update(), may be your paint function not called after returning from the function.

    Qt Code:
    1. void Compare::getImageFileName1()
    2. {
    3. fp1 = true;
    4. QFileDialog dialog(this, "Upload");
    5. dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg *.bmp *.tif)"));
    6. dialog.setViewMode(QFileDialog::Detail);
    7.  
    8. if(dialog.exec())
    9. {
    10. imageFileName1 = dialog.selectedFiles().first();
    11. }
    12.  
    13. image1.load(imageFileName1);
    14. image1Width = image1.width();
    15. image1Height = image1.height();
    16.  
    17. if(image1.allGray()){
    18. QVector<QRgb> colorTable = image1.colorTable();
    19. for(int i=0;i<colorTable.size();++i) {
    20. colorTable[i] = qRed(qGray(colorTable[i]));
    21. }
    22. image1.setColorTable(colorTable);
    23. }
    24. imageDrawFlag1 = true;
    25.  
    26. update();
    27. }
    To copy to clipboard, switch view to plain text mode 
    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.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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
    Not sure if im checking if image is indexed correctly
    Do you understand what an "indexed" image is? allGray() will not tell you if an image is indexed or not.
    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.


  14. #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 what an indexed image is...
    But the images that i will be loading will be grayscale images
    & will have ext: .png *.xpm *.jpg *.bmp *.tif

    I tried loading
    Image is not uploaded

    Maybe the image is being uploaded but the entire image is converted to the color gray!
    hence it looks like no image is uploaded


    I added
    Qt Code:
    1. image1.convertToFormat(QImage::Format_Mono); // if not grayscale converting to grayscale
    To copy to clipboard, switch view to plain text mode 

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

    got rid of the iff statement

    Last edited by 2lights; 14th August 2013 at 16:01. Reason: updated contents

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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
    Not sure what an indexed image is...
    Go to QImage docs, press Ctrl+F or equivalent and type in "indexed".

    Apart from that read this: http://en.wikipedia.org/wiki/Indexed_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.


  16. #15
    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

    ...
    ...
    ...
    ...
    Last edited by 2lights; 15th August 2013 at 09:13. Reason: updated contents

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
  •  
Qt is a trademark of The Qt Company.