Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Help to QImage
Qt 6.12 LTS is out! See what's new in the release blog

Help to QImage

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QuangDungNguyen
    wrote on last edited by kshegunov
    #1

    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]

    jsulmJ 1 Reply Last reply
    0
    • Q QuangDungNguyen

      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]

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QuangDungNguyen
        wrote on last edited by QuangDungNguyen
        #3

        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

        D 1 Reply Last reply
        0
        • Q QuangDungNguyen

          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

          D Offline
          D Offline
          Devopia53
          wrote on last edited by
          #4

          @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
          1 Reply Last reply
          4
          • Q Offline
            Q Offline
            QuangDungNguyen
            wrote on last edited by
            #5

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

            J.HilkJ 1 Reply Last reply
            0
            • Q QuangDungNguyen

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

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

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


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              5

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt
              • Unsolved