2値化表示
指定したしきい値を使ってモノクロ画像の2値化を行う方法を示しています。
| Software | IC Imaging Control 3.4, Visual Studio™ 2019 | 
|---|---|
| サンプル(C#) | simple_binarization_cs_3.4.zip | 

ここでは独自の画像処理を作成する方法について例示します。最初にプログラムは.ShowDeviceSettingsDialogを使ってデバイス選択ダイアログを表示します。Form1_Load()の最後では、.LiveStartを使用してライブ表示をスタートさせています。
.MemoryCurrentGrabberColorformat = ICY8では、このアルゴリズムが8bitグレースケールで入力された画像データを使用する事を明示しています。
private void Form1_Load(object sender, System.EventArgs e)
{
    icImagingControl1.ShowDeviceSettingsDialog();
    if( !icImagingControl1.DeviceValid )
    {
        Close();
        return;
    }
    icImagingControl1.MemoryCurrentGrabberColorformat = TIS.Imaging.ICImagingControlColorformats.ICY800;
    icImagingControl1.LiveDisplay = false;
    icImagingControl1.LiveStart();
}
”Snap Image”ボタンがクリックされると、.MemorySnapImageがイメージストリームの中から1枚のイメージを取得し、それを内部のリングバッファに書き出します。
private void btnSnapImage_Click(object sender, System.EventArgs e)
{
    icImagingControl1.MemorySnapImage();
    icImagingControl1.Display();
    btnBinarize.Enabled = true;
}"Binarize"ボタンがクリックされると、プログラムは txtThreshold.Textに指定されているしきい値を取得します。その後 ibで参照されるイメージバッファを作成し、リングバッファの現在のイメージ (ImageBuffers.CurrentIndex)をセットします。下のコードに示している2つのループは実際の画像処理のアルゴリズムです。ピクセルの輝度値 .imageData(x,y) が指定した閾値よりも大きければ255に、でなければ0にセットしています。
private void btnBinarize_Click(object sender, System.EventArgs e)
{
    int threshold = int.Parse( txtThreshold.Text );
    if( threshold < 0 || threshold > 255 )
    {
        MessageBox.Show( "Invalid threshold value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error    );
    }
    TIS.Imaging.ImageBuffer ib = icImagingControl1.ImageActiveBuffer;
    for( int y = 0; y < ib.Lines; ++y )
    {
        for( int x = 0; x < ib.PixelPerLine; ++x )
        {
            if( ib[x,y] < threshold )
                ib[x,y] = 0;
            else
                ib[x,y] = 255;
        }
    }
    icImagingControl1.Display();
}


