Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get RGB pixel values from QImage buffer
Forum Updated to NodeBB v4.3 + New Features

Get RGB pixel values from QImage buffer

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 3.1k Views 2 Watching
  • 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.
  • I Offline
    I Offline
    Ioseph12
    wrote on 14 Jul 2017, 10:28 last edited by Ioseph12
    #1

    Hi all,

    I'm currently trying to make some operations over the RGB values of a QImage region. To go over the buffer I use some char* variable but, the values I get are not the same as if I would try to acces with QImage::pixelColor(QPoint). Here is the code:

    QImage img;
    bool res = img.load("./scattering.jpg");
    QImage img2 = img.convertToFormat(QImage::Format_RGB888);
    x_or = 1;
    y_or = 254;
    wdth_r = 1278;
    height_r = 508;
    
    //Get gray values
    char* in = (char*)img2->bits();
    *out = (float*) malloc(wdth_r * height_r*sizeof(float));
    
    char R;
    char G;
    char B;
    int w_step = ((wdth_r) * 3 + 2); //RGB channels. This + 2 is only in Qt RGB24 buffers ?
    
    float * auxPt = *out;
    char* auxInPt = in + (y_or - 1) * w_step;
    char* auxInPtCol;
    
    for (int i = y_or - 1; i < (y_or + height_r - 1); i++)
    {
    	char* line = (char*)inn->scanLine(i); 
    	auxInPtCol = auxInPt; //Equal to line. 
    
    	for (int j = x_or - 1; j < (x_or + wdth_r - 1); j++)
    	{
    		R = *auxInPtCol++;
    		G = *auxInPtCol++;
    		B = *auxInPtCol++;
    
    		Color aux = inn->pixelColor(QPoint(i, j));
    		char R_R = aux.red();
    		char G_R = aux.green();
    		char B_R = aux.blue();
    
                    //Debugger cond
    		if (R_R != R)
    			int a = 2;
    
    		if (G_R != G)
    			int b = 2;
    
    		if (B_R != B)
    			int c = 3;
    
    		*auxPt = (R+G+B) / 3.f;
    	         auxPt++;
    	}
    	auxInPt += w_step;
    }
    
    1 Reply Last reply
    0
    • A artwaw
      14 Jul 2017, 10:47

      Hi,
      why don't you use QColor::getRGB() instead? This will give you a nice set if int values.

      Also docs for QImage::pixelColor have a note:
      Warning: This function is expensive when used for massive pixel manipulations. Use constBits() or constScanLine() when many pixels needs to be read.

      I Offline
      I Offline
      Ioseph12
      wrote on 14 Jul 2017, 11:56 last edited by
      #4

      @artwaw

      I found what was the problem. Qt saves the image pixels in BGR order so, to get the right values it would be necccesary to catch them this way:

      B = *auxInPtCol++;
      G = *auxInPtCol++;
      R = *auxInPtCol++;
      
      1 Reply Last reply
      2
      • A Offline
        A Offline
        artwaw
        wrote on 14 Jul 2017, 10:47 last edited by
        #2

        Hi,
        why don't you use QColor::getRGB() instead? This will give you a nice set if int values.

        Also docs for QImage::pixelColor have a note:
        Warning: This function is expensive when used for massive pixel manipulations. Use constBits() or constScanLine() when many pixels needs to be read.

        For more information please re-read.

        Kind Regards,
        Artur

        I 2 Replies Last reply 14 Jul 2017, 10:53
        2
        • A artwaw
          14 Jul 2017, 10:47

          Hi,
          why don't you use QColor::getRGB() instead? This will give you a nice set if int values.

          Also docs for QImage::pixelColor have a note:
          Warning: This function is expensive when used for massive pixel manipulations. Use constBits() or constScanLine() when many pixels needs to be read.

          I Offline
          I Offline
          Ioseph12
          wrote on 14 Jul 2017, 10:53 last edited by
          #3

          @artwaw

          Hi artwaw,

          Thanks for your reply. The thing is that those values are just to test if what I'm getting is right.

          The code part with the for's is actually a wrapped function where I pass as a parameter the char* buffer of the image. About passing the QImage as a param it is not an option as I want that this function work with all kind of generical RGB24 buffers

          Cheers

          1 Reply Last reply
          0
          • A artwaw
            14 Jul 2017, 10:47

            Hi,
            why don't you use QColor::getRGB() instead? This will give you a nice set if int values.

            Also docs for QImage::pixelColor have a note:
            Warning: This function is expensive when used for massive pixel manipulations. Use constBits() or constScanLine() when many pixels needs to be read.

            I Offline
            I Offline
            Ioseph12
            wrote on 14 Jul 2017, 11:56 last edited by
            #4

            @artwaw

            I found what was the problem. Qt saves the image pixels in BGR order so, to get the right values it would be necccesary to catch them this way:

            B = *auxInPtCol++;
            G = *auxInPtCol++;
            R = *auxInPtCol++;
            
            1 Reply Last reply
            2

            1/4

            14 Jul 2017, 10:28

            • Login

            • Login or register to search.
            1 out of 4
            • First post
              1/4
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved