ソーベルフィルタ; Sobel Filter
概要
ソーベルフィルタは差分フィルタの一つで、画像のエッジを抽出することができる。差分方向と直交する方向で重みつき平滑化を行うことで微分フィルタに比べノイズに強くなる。
空間フィルタについてはこちらを参照のこと。
重み
微分フィルタにおける重み\(w_{i j}\)は以下で与えられる。
\(\quad \displaystyle \boldsymbol{w} = \begin{bmatrix} 1/4 & 1/2 & 1/4 \\ 0 & 0 & 0 \\ -1/4 & -1/2 & -1/4 \end{bmatrix}, \begin{bmatrix} -1/4 & -1/2 & -1/4 \\ 0 & 0 & 0 \\ 1/4 & 1/2 & 1/4 \end{bmatrix}, \begin{bmatrix} 1/4 & 0 & -1/4 \\ 1/2 & 0 & -1/2 \\ 1/4 & 0 & -1/4 \end{bmatrix}, \begin{bmatrix} -1/4 & 0 & 1/4 \\ -1/2 & 0 & 1/2 \\ -1/4 & 0 & 1/4 \end{bmatrix} \)
横方向と縦方向の微分フィルタの結果を相乗平均することで全方向のエッジが得られる。
結果
ソースコード
GraphicRGB src_graph, dst_graph;
ImageIO.Read(out src_graph, "graph/pepper.png");
var filter_h = new FilterSubtractive(1, new double[,] { { -0.25, -0.5, -0.25 }, { 0, 0, 0 }, { +0.25, +0.5, +0.25 } } );
var filter_v = new FilterSubtractive(1, new double[,] { { -0.25, 0, +0.25 }, { -0.5, 0, +0.5 }, { -0.25, 0, +0.25 } } );
GraphicRGB hdir_graph = Filtering.SubtractFiltering(src_graph, filter_h);
GraphicRGB vdir_graph = Filtering.SubtractFiltering(src_graph, filter_v);
dst_graph = new GraphicRGB(src_graph.Width, src_graph.Height);
for(int x, y = 0, w = dst_graph.Width, h = dst_graph.Height; y < h; y++) {
for(x = 0; x < w; x++) {
RGB ch = hdir_graph.Graph[x, y], cv = vdir_graph.Graph[x, y];
dst_graph.Graph[x, y] = new RGB(Math.Sqrt(ch.R * ch.R + cv.R * cv.R), Math.Sqrt(ch.R * ch.G + cv.G * cv.G), Math.Sqrt(ch.B * ch.B + cv.B * cv.B), 1);
}
}
ImageIO.Write(hdir_graph, "graph/pepper_sobelhdir.png");
ImageIO.Write(vdir_graph, "graph/pepper_sobelvdir.png");
ImageIO.Write(dst_graph, "graph/pepper_sobeledge.png");
関連項目
RGB空間
RGBグラフィックおよびその入出力
フィルタ
グレースケール
ガウシアンフィルタ
バイラテラルフィルタ
モーションブラーフィルタ
微分フィルタ
鮮鋭化フィルタ