Results 1 to 2 of 2

Thread: Increasing contrast of QImage

  1. #1
    Join Date
    Jul 2010
    Posts
    72
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Question Increasing contrast of QImage

    Hello,

    I develop GUI and algorithm for editing images , ideally it should work like Photoshop ( hhh ).
    I already made brightness control algorithm and UI and now working on contrast increase/decrease of QImage,

    next code ( part of a class method ) computes histogram of QImage ( imageToProcess and prev_image are class variables ), finds histogram center ( middle ) and tries to change image contrast by input value factor ( from 0 to 1 ) :

    Qt Code:
    1. QMap<QRgb,long> histo; // histogram map
    2. QMap<QRgb,long>::iterator j;
    3. QRgb c = 0;
    4. int l, k;
    5. histo.clear();
    6. for(k = 0; k < prev_image.width(); ++k)
    7. for( l = 0; l < prev_image.height(); ++l) {
    8. c = prev_image.pixel(k,l);
    9.  
    10. if(!histo.contains(c))
    11. histo.insert(c,0);
    12. }
    13. //computation of occurences
    14. for( k = 0; k < prev_image.width(); ++k)
    15. for( l = 0; l < prev_image.height(); ++l) {
    16. c = prev_image.pixel(k,l);
    17. histo[c] = histo[c]+1;
    18. }
    19. //compute average value
    20. long sum_1 = 0;
    21. long sum_2 = 0;
    22. for(j = histo.begin();j!=histo.end();j++)
    23. {
    24. sum_1+=j.value()*j.key();
    25. }
    26. for(j = histo.begin();j!=histo.end();j++)
    27. {
    28. sum_2+=j.value();
    29. }
    30. long av = sum_1/sum_2;
    31. av_r = qRed(av);
    32. av_g = qGreen(av);
    33. av_b = qBlue(av);
    34. //changing contrast of an image by factor getted from horizontal slider ui:
    35. double factor = (double)( (double)ie->horizontalSlider_2->value() )/100 ; // to change also //got to be a value between 0 - 1
    36. if(factor!=0.99)
    37. for (int y = 0; y < prev_image.height(); ++y) {
    38. for (int x = 0; x < prev_image.width(); ++x) {
    39. c = QRgb(prev_image.pixel(x,y));
    40. QColor col(c);
    41. col.getRgb(&r,&g,&b);
    42. r = (int)(r*(1-factor)+av_r*factor);
    43. g = (int)(g*(1-factor)+av_g*factor);
    44. b = (int)(b*(1-factor)+av_b*factor);
    45. if(r>255)
    46. r=255;
    47. if(g>255)
    48. g=255;
    49. if(b>255)
    50. b=255;
    51. imageToProcess->setPixel(x,y,qRgb(r,g,b));
    52. }
    53. }
    54. if(factor!=0.99)
    55. this->ie->label_11->setPixmap(QPixmap::fromImage(imageToProcess->scaled(this->ie->label_11->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation)));
    56. else
    57. this->ie->label_11->setPixmap(QPixmap::fromImage(prev_image.scaled(this->ie->label_11->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation)));
    To copy to clipboard, switch view to plain text mode 

    But actually what happens is that a contrast of an image changes ( only decreases in such a way ) and a background gets green when initial background is black or red when initial background is white.

    ( screenshot of part of UI with the result is in the attachment )

    How to increase contrast also ?

    What to fix so that a background stays the same after contrast changing manipulations and does not get green/red ?

    Sorry if the code is not formatted / eased to read.

    Thanks in advance.
    Attached Images Attached Images
    Last edited by freely; 16th November 2011 at 12:09.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Increasing contrast of QImage

    The problem is in changing the RGB values so that the white balance stays the same, as the system is not linear. I think this is best done in HSV space. A code snipped for doing pixel value conversion using qimageblitz (http://api.kde.org/kdesupport-api/kd...ageblitz/html/)
    Qt Code:
    1. unsigned int *data, *end;
    2. data = (unsigned int *)qimage->bits();
    3. end = data + (qim->width() * qim->height());
    4. InlineHSV hsv;
    5. while(data < end){
    6. // convert pixel to HSV
    7. hsv.convertRGB2HSV(BlitzPrivate::convertFromPremult(*data));
    8. // change H, S or V as needed
    9. hsv.setSaturation(somethingBasedOn(hsv.saturation());
    10. // and back to RGB
    11. hsv.convertHSV2RGB();
    12. (*data) = BlitzPrivate::convertToPremult(qRgba(hsv.red(), hsv.green(),
    13. hsv.blue(), qAlpha(*data)));
    14. data++;
    15. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Changing brightness & contrast of image with alpha channel
    By Thomas_Lerman in forum Qt Programming
    Replies: 4
    Last Post: 25th March 2011, 16:33
  2. Image contrast
    By toutarrive in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2009, 13:54
  3. Adjusting contrast and brighness of QGraphicsView
    By olidem in forum Qt Programming
    Replies: 6
    Last Post: 18th March 2009, 10:23
  4. contrast, transparency, brightness...
    By Noxxik in forum Qt Programming
    Replies: 6
    Last Post: 28th February 2009, 23:29
  5. Image brightness/contrast adjustments
    By striker303 in forum Qt Programming
    Replies: 2
    Last Post: 8th September 2008, 09:29

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.