owenizedd
owenizedd

Reputation: 1295

How to output all color of each pixel of an image in hexadecimal color (6digit)

How can i print all the color in image using C++ in hexadecimal code such as (#FFFFFF) ? What kind of library that i need in order to extract all the color pixel by pixel? and how to code to loop to all pixel one by one code using that library? Sorry my bad english. Thanks.

Upvotes: 0

Views: 2616

Answers (2)

user3811082
user3811082

Reputation: 251

All you need is the OpenCV. To load picture, get pixel and write it hex value:

#include <opencv\cv.h>

//....
Mat img = imread(filename);
for (int x = 0; x  < img.cols; x++) {
   for (int y = 0; y  < img.rows; y++) {
      Vec3b color = img.at<Vec3b>(y, x);
      cout << std::hex << color.val[0]; //blue
      cout << std::hex << color.val[1]; //green
      cout << std::hex << color.val[2]; //red
      //or
      int colorint = img.at<int>(y, x);
      cout << std::hex << colorint; //redgreenblue
   }
}

Upvotes: 1

Mark Setchell
Mark Setchell

Reputation: 207728

If I may be so bold as to quote Sir Isaac Newton, the easiest way to dump an image's pixels in hex is by "standing on the shoulders of giants". The giant in this case is ImageMagick, which is installed on most Linux distros and is available for free, for macOS and Windows.

At the command line, simply type:

convert picture.jpg txt:

Output

# ImageMagick pixel enumeration: 300,168,65535,srgb
0,0: (64507,56283,34952)  #FBDB88  srgb(251,219,136)
1,0: (65535,58596,37779)  #FFE493  srgb(255,228,147)
2,0: (65535,56026,36237)  #FFDA8D  srgb(255,218,141)
3,0: (62708,51400,33153)  #F4C881  srgb(244,200,129)
4,0: (62965,49858,33153)  #F5C281  srgb(245,194,129)
5,0: (63222,48830,33153)  #F6BE81  srgb(246,190,129)
6,0: (63479,48316,33924)  #F7BC84  srgb(247,188,132)
7,0: (64250,48830,34952)  #FABE88  srgb(250,190,136)

Second easiest option is the CImg C++ library which you can obtain from here. I believe it is significantly simpler than OpenCV which, on a Raspberry Pi for example, takes around 1GB of space and over an hour to build, whereas CImg is a single, header-only file that you include in your code with no libraries needing to be installed, which can do what you ask.

////////////////////////////////////////////////////////////////////////////////
// main.cpp
//
// CImg example of dumping pixels in hex
//
// Build with: g++-6 -std=c++11  main.cpp -lpng -lz -o main
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cstdlib>

#define cimg_use_png       // Do this if you want CImg to process PNGs itself without resorting to ImageMagick
#define cimg_use_jpeg      // Do this if you want CImg to process JPEGs itself without resorting to ImageMagick
#define cimg_use_tiff      // Do this if you want CImg to process TIFFs itself without resorting to ImageMagick
#define cimg_use_curl      // Do this if you want CImg to be able to load images via a URL
#define cimg_display 0     // Do this if you don't need a GUI and don't want to link to GDI32 or X11
#include "CImg.h"

using namespace cimg_library;
using namespace std;

int main() {
    // Load image
    CImg<unsigned char> img("input.png");

    // Get width, height, number of channels
    int w=img.width();
    int h=img.height();
    int c=img.spectrum();
    cout << "Dimensions: " << w << "x" << h << " " << c << " channels" <<endl;

    // Dump all pixels
    for(int y=0;y<h;y++){
       for(int x=0;x<w;x++){
          char hex[16];
          sprintf(hex,"#%02x%02x%02x",img(x,y,0),img(x,y,1),img(x,y,2));
          cout << y << "," << x << " " << hex << endl;
       }
    }
}

We can test that by creating a small gradient image from red-blue with ImageMagick like this:

convert -size 1x10 gradient:red-blue input.png

Here it is enlarged:

enter image description here

and running the program like this - hopefully you can see the hex goes from ff0000 (full red) to 0000ff (full blue):

./main

Sample Output

Dimensions: 1x10 3 channels
0,0 #ff0000
1,0 #8d0072
2,0 #1c00e3
3,0 #aa0055
4,0 #3800c7
5,0 #c70038
6,0 #5500aa
7,0 #e3001c
8,0 #72008d
9,0 #0000ff

Upvotes: 1

Related Questions