Results 1 to 7 of 7

Thread: Grayscale image Color Conversion

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

    Question Grayscale image Color Conversion

    painting a grayscale image into label

    How do i use
    QImage convertToFormat
    & set color

    I need to make whatever is black in the image and make it transparent
    & whatever is white or partially white and make another color
    Qt Code:
    1. while(x < secondaryImage.height()){
    2. while(y < secondaryImage.width()){
    3. if (secondaryImage.pixel(x,y) == 0)
    4. secondaryImage.setPixel(x, y, Qt::red);
    5. y++;
    6. }
    7. x++;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Thats what i got so far from other post
    But its not working

    If someone could please show me an example of how to go about doing it
    or direct me accordingly

    Kind Regards

  2. #2
    Join Date
    Nov 2007
    Posts
    55
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Grayscale image Color Conversion

    I am not sure wether I understood your request the right way. I am assuming that you want to convert all black pixels to full transparency and all other pixels to red.
    The first point is your loop which is not correctly initialized. Even if you initialized x and y outside the loop, latest when scanning the second column your variable y will be out of range, so the inner loop will be performed only once.

    Your loop should look like
    Qt Code:
    1. for ( x = 0; x < secondaryImage.width(); ++x )
    2. {
    3. for ( y = 0; y < secondaryImage.height(); ++y )
    4. {
    5. if ( secondaryImage.pixel( x, y ) == Qt::black )
    6. secondaryImage.setPixel( x, y, QColor( 0, 0, 0, 0 )); // fully transparency, color doesn´t matter
    7. else
    8. secondaryImage.setPixel( x, y, Qt::red );
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    I did not test the code!

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

    2lights (12th August 2013)

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

    Default Re: Grayscale image Color Conversion

    Loop: Thanks that sorted the "out of scope problem"

    Though the image editing.... Nothing happens
    Any Ideas is there another way of changing colour

    For the time being i'm inverting pixels of whole image until I get a more graphic appealing solution

  5. #4
    Join Date
    Nov 2007
    Posts
    55
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Grayscale image Color Conversion

    can you show the whole code you are using?

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

    Default Re: Grayscale image Color Conversion

    The Inversion Color of image
    Loading Image function
    Qt Code:
    1. QFileDialog dialog2(this, "Upload secondary");
    2. dialog2.setNameFilter(tr("Images (*.png *.xpm *.jpg *.bmp)"));
    3. dialog2.setViewMode(QFileDialog::Detail);
    4.  
    5. if(dialog2.exec())
    6. {
    7. imageFileName2 = dialog2.selectedFiles().first();
    8. }
    9.  
    10. primaryImage.load(imageFileName1);
    11. primaryImage.invertPixels();
    To copy to clipboard, switch view to plain text mode 

    In paint Function:
    Qt Code:
    1. painter.drawImage(QPoint(0, 0), secondaryImage);
    To copy to clipboard, switch view to plain text mode 

    The Changing color version of program ie(commented out until solution found)
    Qt Code:
    1. for (int x = 0; x < secondaryImage.width(); ++x)
    2. {
    3. for (int y = 0; y < secondaryImage.height(); ++y)
    4. {
    5. if ( secondaryImage.pixel(x, y) == Qt::black)
    6. secondaryImage.setPixel(x, y, QColor( 0, 0, 0, 0 )); //This line gives compiler error :confused:
    7. else
    8. secondaryImage.setPixel( x, y, Qt::red ); //Nothing happens here :(
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Ideas most welcome

  7. #6
    Join Date
    Nov 2007
    Posts
    55
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Grayscale image Color Conversion

    sorry, my code is not correct. As the documentation stipulates, the last argument of setPixel() should be an uint!

    Try wether next code do what you want.

    Qt Code:
    1. QRgba transparentPixel, redPixel;
    2. transparentPixel = QRgba( 0, 0, 0, 0 ); // fully transparency, color doesn´t matter
    3. redPixel = QRgba( 255, 0, 0, 255 ); // red, no transparency
    4.  
    5. for ( x = 0; x < secondaryImage.width(); ++x )
    6. {
    7. for ( y = 0; y < secondaryImage.height(); ++y )
    8. {
    9. if ( secondaryImage.pixel( x, y ) == Qt::black ) // check wether working right!
    10. secondaryImage.setPixel( x, y, transparentPixel);
    11. else
    12. secondaryImage.setPixel( x, y, redPixel );
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    sorry, my code is not correct. As the documentation stipulates, the last argument of setPixel() should be an uint!

    Try wether next code do what you want.

    Qt Code:
    1. QRgba transparentPixel, redPixel;
    2. transparentPixel = QRgba( 0, 0, 0, 0 ); // fully transparency, color doesn´t matter
    3. redPixel = QRgba( 255, 0, 0, 255 ); // red, no transparency
    4.  
    5. for ( x = 0; x < secondaryImage.width(); ++x )
    6. {
    7. for ( y = 0; y < secondaryImage.height(); ++y )
    8. {
    9. if ( secondaryImage.pixel( x, y ) == Qt::black ) // check wether working right!
    10. secondaryImage.setPixel( x, y, transparentPixel);
    11. else
    12. secondaryImage.setPixel( x, y, redPixel );
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 


    Added after 27 minutes:


    change following line
    Qt Code:
    1. if ( secondaryImage.pixel( x, y ) == Qt::black )
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. if ( secondaryImage.pixel( x, y ) == QRgb( #000000 ) // black
    To copy to clipboard, switch view to plain text mode 
    Last edited by alainstgt; 12th August 2013 at 16:23.

  8. #7

    Default Re: Grayscale image Color Conversion

    here is an example which shows you how to convert color mode. i t helps you convert between white and black,RGB and other image formats. and the above pixel color changing make sense as well.
    hope i am being helpful.
    best luck to you
    Lily

Similar Threads

  1. Replies: 3
    Last Post: 14th August 2012, 15:47
  2. Image conversion black and white
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 25th March 2010, 02:21
  3. image conversion speed?
    By tommy in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2008, 07:49
  4. can you save 8 bpp grayscale bitmaps?
    By eric in forum Qt Programming
    Replies: 3
    Last Post: 18th November 2007, 12:00
  5. conversion of color
    By Stephano in forum Qt Programming
    Replies: 5
    Last Post: 22nd May 2006, 12:56

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.