圖像處理: 提高對比度 - 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.