图象处理: 提高对比度 - 3

演示如何直接在程序中提高图像对比度。
语言:.NET C#/Visual Basic
版本:3.0.3
发布于:2006年8月20日
作者:IC Imaging Control 技术支持部
系统要求:IC Imaging Control >2.1
由WDM数据流类驱动程序驱动的相机、视频转换器或图像采集卡

示例程序外观如图所示:

The Testdialog of IC Imaging Control,

在第一和第二部分中,帧滤镜都是从外部的帧滤镜模块中加载的。 在本例中,帧滤镜将直接在C#中实现。

帧滤镜被封装在了模块InternalContrastEnhancement.cs 中的类 InternalContrastEnhancement 里。 关于帧滤镜的详细信息,请参考 IC Imaging Control 文档的相关部分。

帧滤镜类有三个成员。 它们用于控制滤镜的属性:

[C#]
private bool m_bEnabled = false; // This variable is used to enable and disable the
                                 // contrast enhancement image processing.
private long m_lowerBound = 50;  // The lower bound of the interval the pixel values are mapped to.
private long m_upperBound = 250; // The upper bound of the interval the pixel values are mapped to.

程序员可通过这个帧滤镜类提供的"get" 与 "set" 方法改变滤镜参数。 下面的代码显示的是如何改变参数m_bEnabled

[C#]
public void    setEnable( bool bEnable )
{
    m_bEnabled = bEnable;
}

方法 Transform 具体实现提高对比度。 首先,把所有成员拷贝至局部变量中。 这样可以放置运行方法时成员改变后产生的副作用。

[C#]
BeginParameterTransfer();
bool enabled = m_bEnabled;
long upperBound = m_upperBound;
long lowerBound = m_lowerBound;
EndParameterTransfer();

第二步,创建一个 LUT (look up table)。 这个 LUT 用于映射图像中每个像素的亮度值。

[C#]
for( int i = 0; i < 256; ++i )
{
    if( i <= lowerBound )
    {
        LUT[i] = 0;
    }
    else if( i >= upperBound )
    {
        LUT[i] = 255;
    }
    else
    {
        // Map the pixel values between lowerBound and upperBound to the range 0 to 255.
        LUT[i] = System.Convert.ToByte( System.Convert.ToDouble(i - lowerBound) /
                        System.Convert.ToDouble(upperBound - lowerBound) * 256.0);
    }
}

最后,使用处理过的像素值组成新的帧。

[C#]
while( pSource < pEnd )
{
    *pTarget = LUT[*pSource];
    pSource++;
    pTarget++;
}

程序的主类必须创建一个滤镜并把它插入 IC imaging Control 的设备路径中去。 只有这样,这项图像处理功能才能开始工作。

[C#]
// Create an instance of the frame filter class.
MyInternalContrastEnhancement = new InternalContrastEnhancement();
// Create an abstract wrapper for the frame filter class.
TIS.Imaging.FrameFilter abstractFilter = icImagingControl1.FrameFilterCreate( MyInternalContrastEnhancement );
// Insert the filter in the device path of the image stream.
icImagingControl1.DeviceFrameFilters.Add( abstractFilter );
// Enable the image processing in the filter.
MyInternalContrastEnhancement.setEnable(true);

主窗体有两个方法。第一个初始化用户界面,第二个更新用户界面。 方法 InitControls 初始化两个 scrollbars,tbLowerBoundtbUpperBound。 方法 UpdateControls 保持控件的状态与滤镜内部状态一致。 现在,只需实现这两个scrollbars的事件处理程序,开启复选框。 作为例子,下面是 scrollbar tbLowerBound 的事件处理程序:

[C#]
private void tbLowerBound_Scroll(object sender, System.EventArgs e)
{
    MyInternalContrastEnhancement.setLowerBound( tbLowerBound.Value );
    UpdateControls();
}

相关源代码示例

责任声明
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.