1. Intermediate user
Join Date
Jul 2010
Posts
72
Thanked 3 Times in 3 Posts
Qt products
Platforms

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:
`QMap<QRgb,long> histo; // histogram map     QMap<QRgb,long>::iterator j;     QRgb c = 0;     int l, k;     histo.clear();      for(k = 0; k < prev_image.width(); ++k)        for( l = 0; l < prev_image.height(); ++l) {              c = prev_image.pixel(k,l);             if(!histo.contains(c))                histo.insert(c,0);        }     //computation of occurences     for( k = 0; k < prev_image.width(); ++k)        for( l = 0; l < prev_image.height(); ++l) {            c = prev_image.pixel(k,l);            histo[c] = histo[c]+1;        }                 //compute average value                 long sum_1 = 0;                 long sum_2 = 0;                 for(j = histo.begin();j!=histo.end();j++)                 {                     sum_1+=j.value()*j.key();                 }                 for(j = histo.begin();j!=histo.end();j++)                 {                     sum_2+=j.value();                 }                 long av = sum_1/sum_2;                 av_r = qRed(av);                 av_g = qGreen(av);                 av_b = qBlue(av);                //changing contrast of an image by factor getted from horizontal slider ui:                double factor = (double)( (double)ie->horizontalSlider_2->value() )/100 ;    // to change also   //got to be a value between 0 - 1                if(factor!=0.99)                for (int y = 0; y < prev_image.height(); ++y) {                for (int x = 0; x < prev_image.width(); ++x) {                c = QRgb(prev_image.pixel(x,y));                QColor col(c);                col.getRgb(&r,&g,&b);                r = (int)(r*(1-factor)+av_r*factor);                g = (int)(g*(1-factor)+av_g*factor);                b = (int)(b*(1-factor)+av_b*factor);                if(r>255)                    r=255;                if(g>255)                    g=255;                if(b>255)                    b=255;                imageToProcess->setPixel(x,y,qRgb(r,g,b));             }          }            if(factor!=0.99)                    this->ie->label_11->setPixmap(QPixmap::fromImage(imageToProcess->scaled(this->ie->label_11->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation)));            else                    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.

Last edited by freely; 16th November 2011 at 12:09.

2. Intermediate user
Join Date
May 2011
Posts
239
Thanks
4
Thanked 35 Times in 35 Posts
Qt products
Platforms

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:
`unsigned int *data, *end;    data = (unsigned int *)qimage->bits();    end = data + (qim->width() * qim->height());    InlineHSV hsv;    while(data < end){        // convert pixel to HSV        hsv.convertRGB2HSV(BlitzPrivate::convertFromPremult(*data));        // change H, S or V as needed        hsv.setSaturation(somethingBasedOn(hsv.saturation());        // and back to RGB        hsv.convertHSV2RGB();        (*data) = BlitzPrivate::convertToPremult(qRgba(hsv.red(), hsv.green(),                                                     hsv.blue(), qAlpha(*data)));        data++;    }`
To copy to clipboard, switch view to plain text mode