图象处理: 简单二值化图象处理

下面这个示例程序将介绍如何根据设定的阈值,使用IC Imaging Control 对图像的灰度值进行简单的二值化处理。
语言:.NET C#/Visual Basic
版本:3.0.3
发布于:2005年4月7日
作者:IC Imaging Control 技术支持部
系统要求:IC Imaging Control >2.1
由WDM数据流类驱动程序驱动的相机、视频转换器或图像采集卡

示例程序窗口如下图所示:

The Testdialog of IC Imaging Control,

程序开始后首先调用内置对话框(.ShowDeviceSettingsDialog)供用户选择视频设备。 而后通过.LiveStart显示设备传来的现场画面数据流。 .MemoryCurrentGrabberColorformat = ICY8 语句确保二值化算法得到的是8比特灰度级的图像:

[C#]
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 就会从现场视频数据流中抓取一帧图像并将其写入环形缓存。

[C#]
private void btnSnapImage_Click(object sender, System.EventArgs e)
{
    icImagingControl1.MemorySnapImage();
    icImagingControl1.Display();

    btnBinarize.Enabled = true;
}

用户点击 "Binarize", 程序首先读取用户之前设定好的阈值 (threshold = txtThreshold.Text)。 而后创建图象缓存的引用 ib 并将其设为环形缓存中的当前图像 (ImageBuffers.CurrentIndex)。 实际的图象处理操作都是在下面的两个for循环中。如果当前像素的灰度值 .imageData(x,y) 大于所设定的阈值,则将其设为255,反之则设为0:

[C#]
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();
}

相关源代码示例

责任声明
IC Imaging Control 源代码库中的所有代码均只用于教学目的,The Imaging Source Europe GmbH 作为IC Imaging Control的开发制造商,不对任何由于使用本文或其中源代码所产生的后果承担责任。

该网站为The Imaging Source网络的一部分。其它的站点包括 公司, Imaging, 天文相机, Astronomy Cameras Blog, Blog caméras d'astronomie, 天文相机有奖竞答, TX Text Control, LiveDocx, phpLiveDocxForum.