Contrast Enhancement - Part 3 Programming example that illustrates how to implement contrast enhancement directly in the program.
Language: | .NET C#/Visual Basic
Version: | 3.3
Author: | IC Imaging Control Support Department
Requirements: |
Software: | IC Imaging Control 3.3, Visual Studio™ 2010
Hardware: | Camera, converter or grabber with WDM Stream Class drivers.
Download C# sample | Contrast Enhancement 3 - C#

The window of the resulting application looks as follows:

In the first and second part of this extended sample application, the frame filter was loaded from an external frame filter module. In this sample, the frame filter is going to be directly implemented in C#.

The frame filter is encapsulated in the class InternalContrastEnhancement, in the module InternalContrastEnhancement.cs. For details on filters and their implementation, please refer to the chapter Frame Filters of the IC Imaging Control documentation.

The frame filter class has three members. These are used to determine the behavior of the filter:

    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.

The program can alter frame filter parameters with the "get" and "set" methods, provided by the frame filter class. The following code fragment shows how to alter the parameter m_bEnabled:

    C#

    public void    setEnable( bool bEnable )
    {
        m_bEnabled = bEnable;
    }

The contrast enhancement is done by the method Transform. First of all, all members are copied to local variables. This prevents side effects caused by members being altered, while this method is being executed.

    C#

    BeginParameterTransfer();
    bool enabled = m_bEnabled;
    long upperBound = m_upperBound;
    long lowerBound = m_lowerBound;
    EndParameterTransfer();

In the second step, an LUT (look up table) is created. The LUT is used to map the brightness values of the incoming frames, between the upper and lower bound of the contrast enhancement.

    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);
        }
    }

Finally, all pixel values are mapped to the destination frame, using the previously created look up table.

    C#

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

The main class of the program has to create a filter and insert it into the device path of IC imaging Control. Only then, may the image processing may commence.

    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);

The main form has two methods. The first one initializes the user interface, the second updates the user interface. The method InitControls initializes the two scrollbars tbLowerBound and tbUpperBound. The method UpdateControls keeps the state of the controls consistent with the internal state of the filter. Now, only the event handlers for the two scrollbars and the "Enabled" checkbox are to be implemented. As an example, below is the event handler for the scrollbar tbLowerBound:

    C#

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