CImgは、画像処理の機能を持った C++ のソフトウェア. 次の特徴を持つ.
※ ライセンス条項は各自で確認すること。
sudo apt -y update sudo apt -yV upgrade sudo /sbin/shutdown -r now
◆ 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 ページからダウンロードしてインストールする場合)
◆プログラムの例
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
◆ 実行手順と実行結果の例
./a.out
◆プログラムの例
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
◆プログラムの例
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
◆プログラムの例
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
画素値の最大と最小
k番目に小さい画素値
メディアン(中央値)
画素値の合計
画素値の平均
画素値の分散
MSE (Mean-Squared Error). 誤差の二乗の平均
2つの画像の PSNR (Peak Signal-to-Noise Ratio)
◆プログラムの例
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
L2-norm (L2 ノルム) を用いた画素値の正規化
線形式での画素値の正規化
L2-norm を求める
指定された範囲で画素値をカット (cut)
Uniformly quantize pixel values.
閾値処理
ヒストグラム平坦化 (Equalize histogram of pixel values)
接続成分へのラベル付け (Label connected components)
◆プログラムの例
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
元画像
equalize (ヒストグラム平坦化) の結果(例)
threshold の結果(例)
RGB から Lab への 変換
◆プログラムの例
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 # (上と同じ)
画像サイズの変更
画像の切り取り (crop)
◆プログラムの例
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 の結果(例)
crop の結果(例)
画像を入力し、画像を出力とするような処理
画像の Erode 操作. 構造要素 (structuring element) は、指定されたサイズの正方形.
画像の Dilate 操作. 構造要素 (structuring element) は、指定されたサイズの正方形.
Watershed 変換
再帰 Deriche フィルタ (recursive Deriche filter) の適用
画像の Blur 操作
画像の Isotropic Blur 操作
画像の Anisotropic Blur 操作. directed by a field of diffusion tensors.
画像の Anisotropic Blur 操作. in an edge-preserving way.
画像の Blur 操作 bilateral filter を用いる。
画像の Blur 操作 bilateral filter を用いる。
ラプラシアン (laplacian).
2つの画像の変異場 (displacement field) を推定
Compute distance to a specified value.
Compute n-d Fast Fourier Transform.
◆プログラムの例
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