Get the RGB values of a pixel from an image in C++

Posted on 2004-11-18
Last Modified: 2008-02-07
Given an image (probably JPG, but we haven't decided what format), I'm trying to get all the RGB values of all the pixels in the image, using C++ on Unix.  I don't need to display the image, just retrieve the data.
I'm looking for the code to this in C++ or free libraries that can handle it.  Thanks.
Question by:mysdaao
    LVL 2

    Expert Comment

    COLORREF array[width][height];

    HDC temp = CreateCompatibleDC(hdc);
    SelectObject(temp, bm);
    for(int i = 0; i < width; i++)
         for(int j = 0; j < height; j++)
              array[i][j] = GetPixel(other,i,j);

    That will give you an array of RGB values.  you can use GetRValue(), GetGValue() and GetBvalue() to find out the specific RGB values.  
    LVL 2

    Author Comment

    The compiler doesn't recognize most of those data types.
    I think you've given me an example of how to do it in Visual C++.  I'm working on Unix here.
    LVL 2

    Expert Comment

    yup thats MFC... Do you use g++ ? what compiler are you using on your Unix box?
    LVL 2

    Expert Comment


    They have a cool library for C++ (4 unix g++ is recommended) to do image manipulation with... You can easily read images into containers and easily get color data 4 pixels...

    Checkout the sample as well:
    LVL 2

    Author Comment

    Yes, I am using G++ on Unix.
    From the description, that library looks like exactly what I need.  But I can't access the source.  The repository directory is forbidden.  I've sent the people listed an e-mail, but since it says it was last modified in 2000, I don't know how long it'll take for a response, or if they'll care.
    So I'm still looking for a solution.
    LVL 2

    Expert Comment

    #include <iostream.h>

    // This is the main include file required for the ISL library
    #include <pictemp.h>

    int main()
        Greylevel greyPix; // This defines a single variable of
          // standard data type for greylevel images
        RGBdata colorPix; // And this is for color pixel

        Picture<Greylevel> BWPicture; // Picture class is a template.
          // here we defined a new container for greylevel images
          // initially it is empty

        BWPicture << "lenna256.gif"; // Using operator "<<" we can read image
          // files directly into our container. Besides GIF, there are
          // some other file types that are supported.
        int sizeX, sizeY;

        BWPicture.getDims(sizeX, sizeY); // This method retrieves the size
          // of an image in the container;
          // Please mind the order of coordinates
        cout < <"Picture is "<< sizeY << "x" << sizeX << endl;

        greyPix = BWPicture(sizeY/2, sizeX/2); // By using  operator() we
          // can address specific pixel by its coordinates

        BWPicture[sizeY/2][sizeX/2] = 20; // Direct operations are also allowed

        BWPicture >> "copy_of_lenna.pgm"; // Using operator ">>" we can
          // write the images from the container to a file. Only ASCII
          // PPM/PGM  formats are available

        Picture<RGBdata> ColorPicture; // here we defined a new container
          // for color images

        ColorPicture << "lenna.rgb"; // Here we open a color image in SGI
          // standard format
        colorPix = ColorPiucture(10, 15); // This is the same as [10][15]
        colorPix.R = 255; // The color pixel has 3 fields of R, G, and B
        colorPix.G = colorPix.B = 0; // This makes our pixel red

        ColorPicture(10,15) = colorPix; // we can assign RGB in the same manner

        BWPicture = ColorPicture; // this will transform color image into
          // greylevel authomatically

        ColorPicture = BWPicture; // back assignments sets R=G=B

          // Library Class is able to use built-in cameras like this:
        Picture<CamData> VideoPicture; // Container for Camera images

        cout << "Cheese!" << endl; // get ready to shot
        VideoPicture.CamShot(); // Here the camera is activated and the
          // picture is transferred from the camera into the container
        VideoPicture >> "capture.pgm"; // And now save it to a disk
          // That's all for now
        return 0;


    After the program has been written to a file, you can compile it by using a C++ compiler (in UNIX case, g++ is recommended) by issuing the following command:

    on a Sun computer:
    > g++ -I/net/emc2-32/www/public_html/Labs/Isl/Project/Projects_done/vsoft/dl/include -I/usr/local/include/X11 -o example -L/net/emc2-32/www/public_html/Labs/Isl/Project/Projects_done/vsoft/dl/lib -lisls -ljpeg -lm
    This can be explained as follows:
    -I flag tells the compiler where to look for the #include files
    -L flag tells the compiler where to look for libraries during the linkage step
    -lisls adds ISL library for the compilation
    -ljpeg is required for working with JPG files
    on a SGI computer:
    > CC -n32 -ptall -I/net/emc2-32/www/public_html/Labs/Isl/Project/Projects_done/vsoft/dl/include -I/usr/local/include/X11 -o example -L/net/emc2-32/www/public_html/Labs/Isl/Project/Projects_done/vsoft/dl/lib -lisl -limage -lvl -ljpeg -lm
    In this case, SGI must add more libraries in order to work with camera and images.
    on a PC the compilation process is compiler-dependent. Details can be found on the Compilaton page
    Note, that the library name is DIFFERENT for SUNs (isls) and for SGIs (isl). This is because the machine architecture is different between those two.

    LVL 2

    Author Comment

    Compiling it that way would only work if I was on their network.  It will look for the libraries in the included files in /net/emc2-32/www/public_html/Labs/Isl/Project/Projects_done/vsoft/dl/include which of course doesn't exist outside their network.  I would need to download pictemp.h and whatever other files and libraries are used, but I can't find a way to do that.
    LVL 2

    Author Comment

    I found a library myself that can do what I need.  Corona can read and write many image formats and get pixel information.
    Thank you for trying to help me.

    Accepted Solution

    Closed, 250 points refunded.

    Community Support Moderator
    Experts Exchange

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now