【.NET】カラー画像を白黒2値画像に変換する
VB.NET、C#でカラー画像の白黒2値化を行う方法です。
(写真はうちの犬です。)
画像の2値化は、【.NET】カラー行列(ColorMatrix)で画像をグレースケール化する の手順でカラー行列(ColorMatrix)を使ってグレースケールに変換し、さらにImageAttributes.SetThreshold()で、しきい値を指定することで簡単にできます。
(カラー行列の説明は、 【.NET】カラー行列(ColorMatrix)で画像のRGBの値を調整するをご覧ください。)
グレースケール画像にしきい値を設定する
ImageAttributes.SetThreshold()メソッドを使用すると、引数のしきい値(0~1が範囲、0.5で50%)に応じて、各ピクセルのRGB値を最小か最大に振り分けることができます。
グレースケール画像では、RGB値がR = G = Bと等しくなっているので、グレースケール状態でSetThreshold()を使うと2値画像ができあがります。
プログラムのサンプルです。
'VB.NETのサンプル '(Imports System.Drawing.Imagingが必要) 'ファイルを読み込む Dim sourceImage As Bitmap = _ DirectCast(Bitmap.FromFile("ファイル名.bmp", True), Bitmap) 'RGBの比率(YIQカラーモデル) Const r As Single = 0.298912F Const g As Single = 0.586611F Const b As Single = 0.114478F 'ColorMatrixにセットする行列を 5 * 5 の配列で用意 '入力のRGBの各チャンネルを重み付けをした上で、 '出力するRGBがR = G = B となるような行列をセット Dim matrixElement As Single()() = _ {New Single() {r, r, r, 0, 0}, _ New Single() {g, g, g, 0, 0}, _ New Single() {b, b, b, 0, 0}, _ New Single() {0, 0, 0, 1, 0}, _ New Single() {0, 0, 0, 0, 1}} 'ColorMatrixオブジェクトの作成 Dim matrix As ColorMatrix = New ColorMatrix(matrixElement) 'ImageAttributesにセット Dim attr As ImageAttributes = New ImageAttributes() attr.SetColorMatrix(matrix) '閾値の設定 attr.SetThreshold(0.5F) '50%を指定 Dim imageWidth As Integer = sourceImage.Width Dim imageHeight As Integer = sourceImage.Height '新しいビットマップを用意 Dim changedImage As Bitmap = New Bitmap(imageWidth, imageHeight) '新しいビットマップにImageAttributesを指定して '元のビットマップを描画 Dim graph As Graphics = Graphics.FromImage(changedImage) graph.DrawImage(sourceImage, _ New Rectangle(0, 0, imageWidth, imageHeight), _ 0, 0, imageWidth, imageHeight, _ GraphicsUnit.Pixel, _ attr) graph.Dispose() '--表示や保存などの処理をここに記述-- '使い終わったら後始末 'sourceImage.Dispose() 'changedImage.Dispose()
//C#のサンプル //(using System.Drawing.Imaging;が必要) //元のイメージを読み込む Bitmap sourceImage = (Bitmap)Bitmap.FromFile(@"ファイル名.bmp", true) //RGBの比率(YIQカラーモデル) const float r = 0.298912F; const float g = 0.586611F; const float b = 0.114478F; //ColorMatrixにセットする行列を 5 * 5 の配列で用意 //入力のRGBの各チャンネルを重み付けをした上で、 //出力するRGBがR = G = B となるような行列をセット float[][] matrixElement = {new float[]{r, r, r, 0, 0}, new float[]{g, g, g, 0, 0}, new float[]{b, b, b, 0, 0}, new float[]{0, 0, 0, 1, 0}, new float[]{0, 0, 0, 0, 1}}; //ColorMatrixオブジェクトの作成 ColorMatrix matrix = new ColorMatrix(matrixElement); //ImageAttributesにセット ImageAttributes attr = new ImageAttributes(); attr.SetColorMatrix(matrix); //閾値の設定 attr.SetThreshold(0.5F); //50%を指定 int imageWidth = sourceImage.Width; int imageHeight = sourceImage.Height; //新しいビットマップを用意 Bitmap changedImage = new Bitmap(imageWidth, imageHeight); //新しいビットマップにImageAttributesを指定して //元のビットマップを描画 Graphics graph = Graphics.FromImage(changedImage); graph.DrawImage(sourceImage, new Rectangle(0, 0, imageWidth, imageHeight), 0, 0, imageWidth, imageHeight, GraphicsUnit.Pixel, attr); graph.Dispose(); //--表示や保存などの処理をここに記述-- //使い終わったら後始末 //sourceImage.Dispose(); //changedImage.Dispose();
サンプルプログラムのダウンロード
*このサンプルプログラムは、トラックバーでリアルタイムに制御していますが、大きな解像度の画像だと動きが鈍いと思います。
実際にリアルタイムのプレビューが必要であれば、確認に必要な程度の小さな解像度の画像に変換してからプレビューさせて、最終的に元の画像に適用させるなどのアプローチが良いと思われます。
関連図書
ディジタル画像処理の基礎と応用 改訂版―Visual C#.NET&Visual Basic.NETによる 基本概念から
関連記事
TrackBack URL :
Comments (0)