金子邦彦研究室プログラミングOctave の活用Octave で濃淡画像の勾配強度画像とガウシアンフィルタ(濃淡画像を入力、出力とする)

Octave で濃淡画像の勾配強度画像とガウシアンフィルタ(濃淡画像を入力、出力とする)

ガウシアンフィルタと勾配の Octave のプログラム例を示す.

◆ このWeb ページで行うこと

Octave での画像ファイルの入出力については,別のページで説明している.

前準備

Octave のインストール

必見 Web ページ: http://www.csse.uwa.edu.au/~pk/Research/MatlabFns/

必見 Web ページ: http://www.eecs.berkeley.edu/Research/Projects/CS/vision/bsds/

説明に使う濃淡画像  

この Web ページの手順をそのままなぞる場合には, 下記の手順で画像ファイルのダウンロードと確認を行う.

  1. 画像ファイルのダウンロード

    ◆ 使用する画像ファイルのダウンロード手順例

    cd /tmp
    curl -O https://www.kkaneko.jp/sample/lena_std.jpg
    curl -L https://github.com/opencv/opencv/blob/master/samples/data/fruits.jpg?raw=true -o fruits.jpg
    
  2. (オプション) MatlabFns を入手し、分かりやすいディレクトリ(例えば /usr/local/MatlabFns)に展開
  3. (オプション) MatlabFns を使うように .octaverc を設定する

    ◆ 設定例 

    setenv("CFLAGS", "-D__STDC_CONSTANT_MACROS");
    setenv("CXXFLAGS", "-D__STDC_CONSTANT_MACROS");
    addpath ("/usr/local/MatlabFns/FingerPrints");
    addpath ("/usr/local/MatlabFns/FrequencyFilt");
    addpath ("/usr/local/MatlabFns/GreyTrans");
    addpath ("/usr/local/MatlabFns/LineSegments");
    addpath ("/usr/local/MatlabFns/Match");
    addpath ("/usr/local/MatlabFns/Misc");
    addpath ("/usr/local/MatlabFns/PhaseCongruency");
    addpath ("/usr/local/MatlabFns/Projective");
    addpath ("/usr/local/MatlabFns/Robust");
    addpath ("/usr/local/MatlabFns/Rotations");
    addpath ("/usr/local/MatlabFns/Shapelet");
    addpath ("/usr/local/MatlabFns/Spatial");
    addpath ("/usr/local/share/octave/mex");
    
  4. Octave の起動
    octave
    
  5. 確認のため,表示してみる。

    MatlabFns を使わないときは mono3, mono4 を含む行(下記では太字)を無視すること

    Windows では「imread("r:/lena_std.jpg");」のようになる.

    rgb = imread("/tmp/lena_std.jpg");
    mono = rgb2gray( rgb );
    
    rgb2 = imread("/tmp/fruits.jpg");
    mono2 = rgb2gray( rgb2 );
    
    mono3 = uint8( step2line(100, -1) * 255 );
    mono4 = uint8( starsine( sze = 256, wavelength = 8, nScales = 1, ampExponent = 3 ) * 255 );
    
    colormap(gray(256));
    imshow(mono);
    imshow(mono2);
    imshow(mono3, [0 1]);
    imshow(mono4, [0 1]);
    
元画像 [image] [image]
濃淡画像 [image] [image] [image] [image]

◆ Ubuntu 11.10 での実行結果例

[image]

[image]

Octave で勾配強度画像を生成する

勾配強度画像

勾配強度画像を表示する Octave プログラム例

勾配強度は gradient を使って求める. imadjust を使い,最小値が黒、最大値が白になるように調整して表示.

rgb = imread("/tmp/lena_std.jpg");
mono = rgb2gray( rgb );

[Dx, Dy] = gradient(double(mono));
D = sqrt( Dx .* Dx + Dy .* Dy );
colormap(gray(256));
imshow( imadjust(D, [min(min(D)); max(max(D))]) );

◆ 実行結果の例

[image] [image] [image] [image]

横方向の勾配

横方向の勾配を表示する Octave プログラム例

横方向の勾配は gradient を使って求める(Dx に入る). imadjust を使い,最小値が黒、最大値が白になるように調整して表示.

rgb = imread("/tmp/lena_std.jpg");
mono = rgb2gray( rgb );

[Dx, Dy] = gradient(double(mono));
D = abs(Dx); 
colormap(gray(256));
imshow( imadjust(D, [min(min(D)); max(max(D))]) );

◆ 実行結果の例

[image]

縦方向の勾配

縦方向の勾配を表示する Octave プログラム例

縦方向の勾配は gradient を使って求める(Dy に入る). imadjust を使い,最小値が黒、最大値が白になるように調整して表示.

rgb = imread("/tmp/lena_std.jpg");
mono = rgb2gray( rgb );

[Dx, Dy] = gradient(double(mono));
D = abs(Dy); 
colormap(gray(256));
imshow( imadjust(D, [min(min(D)); max(max(D))]) );

◆ 実行結果の例

[image] [image] [image] [image]

ガウシアン平滑化 (Isotropic Gaussian Smoothing)

ガウシアン平滑化を行う Octave プログラム例 (Octave program for Isotropic Gaussian Smoothing)

「"Bilateral"」は ガウシアン平滑化 「sigma = 6)」の部分は調整してください

※ Octave 3.6.1, Ubuntu 11.10 で動作確認済み.

rgb = imread("/tmp/lena_std.jpg");
mono = rgb2gray( rgb );
r = imsmooth(mono, "Gaussian", sigma = 6); 
colormap(gray(256));
imshow(r);

◆ 実行結果の例

[image] [image] [image] [image]

Octave でガウシアンフィルタと勾配強度の組み合わせ

ガウシアンフィルタと勾配強度の組み合わせ

ガウシアンフィルタを適用した後で,勾配強度を求める(勾配強度の値を正規化して表示)

rgb = imread("/tmp/lena_std.jpg");
mono = rgb2gray( rgb );

r = imsmooth(mono, "Gaussian", sigma = 6); 
[Dx, Dy] = gradient(double(r));
D = sqrt( Dx .* Dx + Dy .* Dy );
colormap(gray(256));
imshow( imadjust(D, [min(min(D)); max(max(D))]) );

◆ 実行結果の例

[image] [image] [image] [image]

ガウシアンフィルタと横方向の勾配の組み合わせ

横方向の勾配を表示する Octave プログラム例

ガウシアンフィルタを適用した後で,横方向の勾配(求まった勾配値を正規化して表示)

横方向の勾配は gradient を使って求める(Dx に入る). imadjust を使い,最小値が黒、最大値が白になるように調整して表示.

rgb = imread("/tmp/lena_std.jpg");
mono = rgb2gray( rgb );

r = imsmooth(mono, "Gaussian", sigma = 6); 
[Dx, Dy] = gradient(double(r));
D = abs(Dx); 
colormap(gray(256));
imshow( imadjust(D, [min(min(D)); max(max(D))]) );

◆ 実行結果の例

[image] [image] [image] [image]

ガウシアンフィルタと横方向の勾配の組み合わせ

縦方向の勾配を表示する Octave プログラム例

ガウシアンフィルタを適用した後で,縦方向の勾配(求まった勾配値を正規化して表示)

縦方向の勾配は gradient を使って求める(Dx に入る). imadjust を使い,最小値が黒、最大値が白になるように調整して表示.

rgb = imread("/tmp/lena_std.jpg");
mono = rgb2gray( rgb );

r = imsmooth(mono, "Gaussian", sigma = 6); 
[Dx, Dy] = gradient(double(r));
D = abs(Dy); 
colormap(gray(256));
imshow( imadjust(D, [min(min(D)); max(max(D))]) );

◆ 実行結果の例

[image] [image] [image]