Help to QImage



  • Hi everyone, i am a newbie in qt.I want to convert Image to oil painting with follow the instruction https://www.codeproject.com/Articles/471994/OilPaintEffect using qt creator.But when I run it, the results are not as expected.Maybe I was wrong somewhere.Please help me.Thanks for your help.Sorry my english...( the code below here):

    QImage effect::OilPainting(QImage &img,int radius,int intensity)
    {
        QImage img_out(img.width(),img.height(),QImage::Format_ARGB32);
        int nR,nG,nB,R_final,G_final,B_final;
        int nCurMax=0;
        int nMaxIndex=0;
        int nIntensityCount[256],sumR[256],sumG[256],sumB[256];
        QColor color;
    
        for(int i = 0;i < 256;i++)
        {
         nIntensityCount[i]=sumR[i]=sumG[i]=sumB[i]=0;
        }
        for(int y = radius; y< img.height()-radius ;y++)
            {
                for(int x = radius; x<img.width()-radius;x++)
                {
    
                    for(int ny= -radius;ny <= radius ;ny++)
                      {
                        for(int nx= -radius;nx <= radius ;nx++)
                        {
                           color = img.pixel(x+nx,y+ny);
                           nR=color.red();
                           nG=color.green();
                           nB=color.blue();
    
                           int nCurrentIntensity =((((nR+nG+nB)/3)*intensity)/255);
                           if(nCurrentIntensity > 255){
                              nCurrentIntensity=255;
                           }
    
                           int i = nCurrentIntensity;
                           nIntensityCount[i]++;
                           sumR[i]=sumR[i]+nR;
                           sumG[i]=sumG[i]+nG;
                           sumB[i]=sumB[i]+nB;
                           for(int nI=0;nI < 256;nI++)
                           {
                               if(nIntensityCount[nI] > nCurMax)
                               {
                                   nCurMax = nIntensityCount[nI];
                                   nMaxIndex = nI;
                               }
                           }
                            R_final=sumR[nMaxIndex]/nCurMax;
                            G_final=sumG[nMaxIndex]/nCurMax;
                            B_final=sumB[nMaxIndex]/nCurMax;
                          // qDebug()<<"R:"<<R_final<<"G:"<<G_final<<"B:"<<B_final;
                          img_out.setPixel(x,y,qRgb(R_final,G_final,B_final));
                        }
                      }
                }
            }
            return img_out;
    }
    

    [Added code tags ~kshegunov]


  • Moderators

    @QuangDungNguyen In what way was the output not as expected?



  • Sorry for my late, this is my image (left) and the results (right).And here is full of code.Thanks for your help!(0_1508423470824_test.png
    and this is the link to code: https://drive.google.com/drive/u/1/folders/0Bx0MuyLjnifFRmlBM3NHVHBrY2M



  • @QuangDungNguyen

    Using setPixel() is not a good idea. Why not use the original code?
    If you are using the original code, you can do it very simply.

    1. Change BYTE to uchar
    2. Convert the original image to QImage::Format_RGB888 using convertToFormat()
    3. Use the bits() for the pbyDataIn_i


  • Thanks i will try this, but i dont know why setpixel is not a good idea?



  • @QuangDungNguyen
    Reading the documentation, I would recommand it :-)

    Warning: This function is expensive due to the call of the internal detach() function called within; if performance is a concern, we recommend the use of scanLine() or bits() to access pixel data directly.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.