金子邦彦研究室プログラミングCImg を使う C++ プログラム例

CImg を使う C++ プログラム例

CImgは、画像処理の機能を持った C++ のソフトウェア. 次の特徴を持つ.

※ ライセンス条項は各自で確認すること。

前準備

  1. UbuntuUbuntu で OS のシステム更新を行うときは, 次のコマンドを実行.
    sudo apt -y update
    sudo apt -yV upgrade
    sudo /sbin/shutdown -r now
    
  2. CImg のインストール

    Ubuntu での操作手順

    sudo apt -y update
    sudo apt -y install cimg-dev
    sudo apt -y install cimg-doc
    sudo apt -y install cimg-examples
    

    CImg.h は /usr/include/CImg.h に置かれる. 見本となるプログラムは /usr/share/doc/cimg-dev/examples に置かれる。

    Ubuntu での操作手順(例) (CImg の Web ページからダウンロードしてインストールする場合

    1. CImg Library の Web ページを開く

      http://cimg.sourceforge.net/

    2. 「Download」をクリック
    3. 「Standard Package」をクリック
    4. zip ファイルをダウンロード
    5. 解凍してできた CImg.h を使う. CImg Library の機能はすべてこのファイルに入っている.

CImg での画像ファイル操作, 表示

画像ファイル読み込みと表示

◆プログラムの例

hoge.cpp を次のように作成する.

#include "CImg.h"
int main(int argc, char **argv) {
  cimg_library::CImg<unsigned char> img01( /* file name */ "/usr/local/share/opencv/samples/c/fruits.jpg" ); 
  img01.display();
  return 0;
}

◆ ビルド手順の例

※ 64 ビット Ubuntu での手順例を下に示す.32ビットUbuntuのときは 「x86_64」のところを 「i386」に変える.

g++ -c -o hoge.o  -Dcimg_use_vt100 -Dcimg_use_xshm -Dcimg_use_ -Dcimg_use_png -Dcimg_use_magick -Dcimg_use_fftw3 -Dcimg_use_lapack -Dcimg_use_opencv -I/usr/X11R6/include -I/usr/include/GraphicsMagick -I/usr/local/include/opencv hoge.cpp
g++ -o a.out hoge.o -lm  -L/usr/lib/x86_64-linux-gnu -lpng -lGraphicsMagick++ -lGraphicsMagickWand -lfftw3 -lopencv_core -llapack -lblas -latlas -lgfortran -lgomp -lX11 -lXext -lpthread -ldl -lquadmath

[image]

◆ 実行手順と実行結果の例

./a.out

[image]

[image]

画像ファイルへの保存,ファイル形式の変更

◆プログラムの例

hoge.cpp を次のように作成する.

#include "CImg.h"
int main(int argc, char **argv) {
  cimg_library::CImg<unsigned char> img01( /* file name */ "/usr/local/share/opencv/samples/c/fruits.jpg" ); 
  img01.save( "hoge.png" );
  return 0;
}

◆ ビルド手順の例

※ 64 ビット Ubuntu での手順例を下に示す.32ビットUbuntuのときは 「x86_64」のところを 「i386」に変える.

g++ -c -o hoge.o  -Dcimg_use_vt100 -Dcimg_use_xshm -Dcimg_use_ -Dcimg_use_png -Dcimg_use_magick -Dcimg_use_fftw3 -Dcimg_use_lapack -Dcimg_use_opencv -I/usr/X11R6/include -I/usr/include/GraphicsMagick -I/usr/local/include/opencv hoge.cpp
g++ -o a.out hoge.o -lm  -L/usr/lib/x86_64-linux-gnu -lpng -lGraphicsMagick++ -lGraphicsMagickWand -lfftw3 -lopencv_core -llapack -lblas -latlas -lgfortran -lgomp -lX11 -lXext -lpthread -ldl -lquadmath
# (上と同じ)

◆ 実行手順と実行結果の例

./a.out

[image]

行列

Cimg での画像の基本操作

() あるいは atXYZC() を用いた画素値の操作

◆プログラムの例

hoge.cpp を次のように作成する.

#include "CImg.h"
int main(int argc, char **argv) {
  cimg_library::CImg<unsigned char> img01( /* file name */ "/usr/local/share/opencv/samples/c/fruits.jpg" ); 
//  img01.RGBtoXYZ();
  for ( int i = 100; i < 400; i++ ) {
    for ( int j = 100; j < 200; j++ ) {
      img01(i, j, 0, 0) = 0;
      img01(i, j, 0, 1) = 0;
      img01(i, j, 0, 2) = 0;
    }
  }

  img01.display();  
  return 0;
}

atXYZC() メソッドを使う場合

#include "CImg.h"
int main(int argc, char **argv) {
  cimg_library::CImg<unsigned char> img01( /* file name */ "/usr/local/share/opencv/samples/c/fruits.jpg" ); 
//  img01.RGBtoXYZ();
  for ( int i = 100; i < 400; i++ ) {
    for ( int j = 100; j < 200; j++ ) {
      img01.atXYZC(i, j, 0, 0) = 0;
      img01.atXYZC(i, j, 0, 1) = 0;
      img01.atXYZC(i, j, 0, 2) = 0;
    }
  }

  img01.display();  
  return 0;
}

◆ ビルド手順の例

※ 64 ビット Ubuntu での手順例を下に示す.32ビットUbuntuのときは 「x86_64」のところを 「i386」に変える.

g++ -c -o hoge.o  -Dcimg_use_vt100 -Dcimg_use_xshm -Dcimg_use_ -Dcimg_use_png -Dcimg_use_magick -Dcimg_use_fftw3 -Dcimg_use_lapack -Dcimg_use_opencv -I/usr/X11R6/include -I/usr/include/GraphicsMagick -I/usr/local/include/opencv hoge.cpp
g++ -o a.out hoge.o -lm  -L/usr/lib/x86_64-linux-gnu -lpng -lGraphicsMagick++ -lGraphicsMagickWand -lfftw3 -lopencv_core -llapack -lblas -latlas -lgfortran -lgomp -lX11 -lXext -lpthread -ldl -lquadmath
# (上と同じ)

◆ 実行手順と実行結果の例

./a.out

[image]

画像情報の取得

◆プログラムの例

hoge.cpp を次のように作成する.

#include "CImg.h"
int main(int argc, char **argv) {
  cimg_library::CImg<unsigned char> img01( /* file name */ "/usr/local/share/opencv/samples/c/fruits.jpg" ); 
  printf( "width = %d\n", img01.width() );
  printf( "height = %d\n", img01.height() );
  printf( "depth = %d\n", img01.depth() );
  printf( "spectrun = %d\n", img01.spectrum() );
  return 0;
}

◆ ビルド手順の例

g++ -c -o hoge.o  -Dcimg_use_vt100 -Dcimg_use_xshm -Dcimg_use_ -Dcimg_use_png -Dcimg_use_magick -Dcimg_use_fftw3 -Dcimg_use_lapack -Dcimg_use_opencv -I/usr/X11R6/include -I/usr/include/GraphicsMagick -I/usr/local/include/opencv hoge.cpp
g++ -o a.out hoge.o -lm  -L/usr/lib/x86_64-linux-gnu -lpng -lGraphicsMagick++ -lGraphicsMagickWand -lfftw3 -lopencv_core -llapack -lblas -latlas -lgfortran -lgomp -lX11 -lXext -lpthread -ldl -lquadmath
# (上と同じ)

◆ 実行手順と実行結果の例

./a.out

[image]

数学関数 (Mathematical Functions)

◆プログラムの例

hoge.cpp を次のように作成する.

#include "CImg.h"
int main(int argc, char **argv) {
  cimg_library::CImg<unsigned char> img01( /* file name */ "/usr/local/share/opencv/samples/c/fruits.jpg" ); 
  unsigned char max, min; 
  max = img01.max_min( min );
  printf( "max = %d\n", max );
  printf( "min = %d\n", min );
  printf( "kth_smallest(1) = %d\n", img01.kth_smallest(1) );
  printf( "kth_smallest(2) = %d\n", img01.kth_smallest(2) );
  printf( "kth_smallest(3) = %d\n", img01.kth_smallest(3) );
  printf( "sum = %f\n", img01.sum() );
  printf( "mean = %f\n", img01.mean() );
  printf( "variance(1) = %f\n", img01.variance(1) );
  printf( "variance(2) = %f\n", img01.variance(2) );
  return 0;
}

◆ ビルド手順の例

※ 64 ビット Ubuntu での手順例を下に示す.32ビットUbuntuのときは 「x86_64」のところを 「i386」に変える.

g++ -c -o hoge.o  -Dcimg_use_vt100 -Dcimg_use_xshm -Dcimg_use_ -Dcimg_use_png -Dcimg_use_magick -Dcimg_use_fftw3 -Dcimg_use_lapack -Dcimg_use_opencv -I/usr/X11R6/include -I/usr/include/GraphicsMagick -I/usr/local/include/opencv hoge.cpp
g++ -o a.out hoge.o -lm  -L/usr/lib/x86_64-linux-gnu -lpng -lGraphicsMagick++ -lGraphicsMagickWand -lfftw3 -lopencv_core -llapack -lblas -latlas -lgfortran -lgomp -lX11 -lXext -lpthread -ldl -lquadmath
# (上と同じ)

◆ 実行手順と実行結果の例

./a.out

[image]

画像の操作

◆プログラムの例

hoge.cpp を次のように作成する.

#include "CImg.h"
int main(int argc, char **argv) {
  cimg_library::CImg<unsigned char> img01( /* file name */ "/usr/local/share/opencv/samples/c/fruits.jpg" ); 
  printf( "original\n" );
  img01.display();
  printf( "equalize\n" );
  img01.equalize(64).display();
  printf( "threshold\n" );
  img01.threshold(30).display();
  return 0;
}

◆ ビルド手順の例

※ 64 ビット Ubuntu での手順例を下に示す.32ビットUbuntuのときは 「x86_64」のところを 「i386」に変える.

g++ -c -o hoge.o  -Dcimg_use_vt100 -Dcimg_use_xshm -Dcimg_use_ -Dcimg_use_png -Dcimg_use_magick -Dcimg_use_fftw3 -Dcimg_use_lapack -Dcimg_use_opencv -I/usr/X11R6/include -I/usr/include/GraphicsMagick -I/usr/local/include/opencv hoge.cpp
g++ -o a.out hoge.o -lm  -L/usr/lib/x86_64-linux-gnu -lpng -lGraphicsMagick++ -lGraphicsMagickWand -lfftw3 -lopencv_core -llapack -lblas -latlas -lgfortran -lgomp -lX11 -lXext -lpthread -ldl -lquadmath
# (上と同じ)

◆ 実行手順と実行結果の例

./a.out

元画像

[image]

equalize (ヒストグラム平坦化) の結果(例)

[image]

threshold の結果(例)

[image]

色空間

◆プログラムの例

hoge.cpp を次のように作成する.

#include "CImg.h"
int main(int argc, char **argv) {
  cimg_library::CImg<unsigned char> img01( /* file name */ "/usr/local/share/opencv/samples/c/fruits.jpg" ); 

  printf( "RDBtoLab\n" );
  img01.RGBtoLab().save("fruits.ppm");

  return 0;
}

◆ ビルド手順の例

※ 64 ビット Ubuntu での手順例を下に示す.32ビットUbuntuのときは 「x86_64」のところを 「i386」に変える.

g++ -c -o hoge.o  -Dcimg_use_vt100 -Dcimg_use_xshm -Dcimg_use_ -Dcimg_use_png -Dcimg_use_magick -Dcimg_use_fftw3 -Dcimg_use_lapack -Dcimg_use_opencv -I/usr/X11R6/include -I/usr/include/GraphicsMagick -I/usr/local/include/opencv hoge.cpp
g++ -o a.out hoge.o -lm  -L/usr/lib/x86_64-linux-gnu -lpng -lGraphicsMagick++ -lGraphicsMagickWand -lfftw3 -lopencv_core -llapack -lblas -latlas -lgfortran -lgomp -lX11 -lXext -lpthread -ldl -lquadmath
# (上と同じ)

幾何操作、空間操作

◆プログラムの例

hoge.cpp を次のように作成する.

#include "CImg.h"
int main(int argc, char **argv) {
  cimg_library::CImg<unsigned char> img01( /* file name */ "/usr/local/share/opencv/samples/c/fruits.jpg" ); 

  // resize test 
  cimg_library::CImg<unsigned char> img02 = img01;
  img02.resize(128, 128, 1, 3); 
  img02.save("1.png");

  // crop test 
  cimg_library::CImg<unsigned char>; img03 = img01;
  img03.crop(100, 40, 160, 120, /* border */ false); 
  img03.save("2.png");
  return 0;
}

◆ ビルド手順の例

※ 64 ビット Ubuntu での手順例を下に示す.32ビットUbuntuのときは 「x86_64」のところを 「i386」に変える.

g++ -c -o hoge.o  -Dcimg_use_vt100 -Dcimg_use_xshm -Dcimg_use_ -Dcimg_use_png -Dcimg_use_magick -Dcimg_use_fftw3 -Dcimg_use_lapack -Dcimg_use_opencv -I/usr/X11R6/include -I/usr/include/GraphicsMagick -I/usr/local/include/opencv hoge.cpp
g++ -o a.out hoge.o -lm  -L/usr/lib/x86_64-linux-gnu -lpng -lGraphicsMagick++ -lGraphicsMagickWand -lfftw3 -lopencv_core -llapack -lblas -latlas -lgfortran -lgomp -lX11 -lXext -lpthread -ldl -lquadmath
# (上と同じ) 

◆ 実行手順と実行結果の例

./a.out

resize の結果(例)

[image]

crop の結果(例)

[image]

画像フィルタ,画像変換

画像を入力し、画像を出力とするような処理

◆プログラムの例

hoge.cpp を次のように作成する.

#include "CImg.h"
int main(int argc, char **argv) {
  cimg_library::CImg<unsigned char> img01( /* file name */ "/usr/local/share/opencv/samples/c/fruits.jpg" ); 

  // erode, dilate
  cimg_library::CImg<unsigned char> img02 = img01;
  img02.erode(8); 
  img02.display();

  cimg_library::CImg<unsigned char> img03 = img01;
  img03.dilate(8); 
  img03.display();

  cimg_library::CImg<unsigned char> img04 = img01;
  img04.erode(8).dilate(8); 
  img04.display();

  // deriche
  cimg_library::CImg<unsigned char> img05 = img01;
  img05.deriche(4, /* order */ 0, /* axis */ 'x' );
  img05.display();

  cimg_library::CImg<unsigned char> img06 = img01;
  img06.deriche(4, /* order */ 1, /* axis */ 'x' );
  img06.display();

  cimg_library::CImg<unsigned char> img07 = img01;
  img07.deriche(4, /* order */ 2, /* axis */ 'x' );
  img07.display();

  cimg_library::CImg<unsigned char> img08 = img01;
  img08.deriche(4, /* order */ 0, /* axis */ 'y' );
  img08.display();

  cimg_library::CImg<unsigned char> img09 = img01;
  img09.deriche(4, /* order */ 1, /* axis */ 'y' );
  img09.display();

  cimg_library::CImg<unsigned char> img10 = img01;
  img10.deriche(4, /* order */ 2, /* axis */ 'y' );
  img10.display();

  return 0;
}

◆ ビルド手順の例

※ 64 ビット Ubuntu での手順例を下に示す.32ビットUbuntuのときは 「x86_64」のところを 「i386」に変える.

g++ -c -o hoge.o  -Dcimg_use_vt100 -Dcimg_use_xshm -Dcimg_use_ -Dcimg_use_png -Dcimg_use_magick -Dcimg_use_fftw3 -Dcimg_use_lapack -Dcimg_use_opencv -I/usr/X11R6/include -I/usr/include/GraphicsMagick -I/usr/local/include/opencv hoge.cpp
g++ -o a.out hoge.o -lm  -L/usr/lib/x86_64-linux-gnu -lpng -lGraphicsMagick++ -lGraphicsMagickWand -lfftw3 -lopencv_core -llapack -lblas -latlas -lgfortran -lgomp -lX11 -lXext -lpthread -ldl -lquadmath
# (上と同じ) 

◆ 実行手順と実行結果の例

./a.out

erode, dilate

deriche

3d Objects Management

描画 (Drawing)

参考WEbページ

http://cimg.sourceforge.net/reference/structcimg__library_1_1CImg.html

#include "CImg.h"

void fade( const double pmin, const double pmax, const double angle, cimg_library::CImg<unsigned char>& img, cimg_library::CImg<unsigned char> dest )
{
  const double ca = std::cos(angle), sa = std::sin(angle);
  //
  double alpha;
  cimg_forXYZC(dest,x,y,z,k) {
    const double X = ((double)x/img.width() - 0.5)*ca + ((double)y/img.height() - 0.5)*sa;
    if (X+0.5<pmin) alpha = 0; else {
      if (X+0.5>pmax) alpha = 1; else
        alpha = (X+0.5-pmin)/(pmax-pmin);
    }
    dest(x,y,z,k) = (unsigned char)( (1 - alpha) * img(x,y,z,k) );
  }
  dest.display("Image fading");
  return; 
}