• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1519
  • Last Modified:

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

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.
  • 4
  • 2
  • 2
  • +1
1 Solution
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.  
mysdaaoAuthor Commented:
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.
yup thats MFC... Do you use g++ ? what compiler are you using on your Unix box?
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


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:
mysdaaoAuthor Commented:
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.
#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 example.cc -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 example.cc -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.

mysdaaoAuthor Commented:
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.
mysdaaoAuthor Commented:
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.
Closed, 250 points refunded.

Community Support Moderator
Experts Exchange

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now