2017-04-04 16 views
-1

C++を使用してイメージ内のすべての色を(#FFFFFF)のような16進数で表示する方法はありますか? ピクセル単位ですべての色を抽出するために必要なライブラリの種類は?そのライブラリを使用して、すべてのピクセルに1つずつコードをループする方法を教えてください。 申し訳ありません私の悪い英語。おかげさまで イメージの各ピクセルの全色を16進数で出力する方法(6digit)

+0

あなたがコンソールに進値を取得したいですか? 'long color = pixelmap [pixel]; std :: cout << std :: hex << color; ' – user3811082

+0

@ user3811082イメージをC++にロードするにはどうすればよいですか? – owenizedd

+0

@ user3811082各ピクセルの全画像の16進値を知りたい – owenizedd

答えて

0

は肩の上に立って「であります巨人の "巨人はこの場合、ImageMagickであり、ほとんどのLinuxディストリビューションにインストールされており、macOSとWindowsでは無料で利用できます。コマンドラインで

、単純に入力します。

# 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) 

convert picture.jpg txt: 

を出力は、第2の最も簡単なオプションは、hereから入手することができますCImg C++ライブラリです。私はそれがよりもはるかに簡単だと信じています。たとえば、ラズベリーパイの場合、約1GBのスペースと1時間以上かかるのに対し、CImgは、あなたが聞いたことをすることができます。ここ

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

それが拡大される:

//////////////////////////////////////////////////////////////////////////////// 
// 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; 
     } 
    } 
} 

我々は、このようなImageMagickのと赤青から小さな勾配画像を作成していることをテストすることができ

enter image description here

とこのようなプログラムを実行しています - うまくいけば、16進数がff0000(完全な赤色)から0000ff(青フル):

./main 

サンプル出力

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 
+0

ありがとうございました! – owenizedd

0

あなたが必要なのはOpenCVです。画像をロードするには、ピクセルを取得し、それを進値書き込み:私はアイザック・ニュートン、六角で画像のピクセルをダンプする最も簡単な方法を引用するほど大胆かもしれ場合

#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 
    } 
} 
+0

注意! OpenCVはデフォルトでBGRを使用しているので、インデックス0は青の成分を与え、1は緑を与え、2は赤を与える。 – Baiz

+0

ありがとうございましたコードを変更しました – user3811082

関連する問題