Contrast Enhancement - Part 2 Programming example that extends the first part by illustrating how to control the parameters for the contrast enhancement frame filter with customized controls.
The window of the resulting application looks as follows: The "Contrast Enhancement" frame filter's VC++ .NET project can also be downloaded from the top of this page. Please note that the frame filter's source code is not required in order to run and understand the following programming example. The filter applies a function to the image that stretches its histogram. This means that the pixel brightness values between an upper and lower bound are mapped to the entire brightness value range. The pixels that are darker than the lower bound are set to 0. Those that are brighter than the upper bound are set to 255. First of all, a variable filter of type TIS.Imaging.FrameFilter must be declared in the class Form1. This variable will contain the frame filter and is used to communicate with the frame filter. C# private TIS.Imaging.FrameFilter filter; VB.NET Dim filter As TIS.Imaging.FrameFilter The upper and lower bounds of the frame filter can be manipulated with two scrollbars (sldLowerBound and sldUpperBound). The beginning of the program is extended by two function calls that initialize the controls on the main form. C# private void Form1_Load(object sender, System.EventArgs e) { icImagingControl1.ShowDeviceSettingsDialog(); if (!icImagingControl1.DeviceValid) return; filter = icImagingControl1.FrameFilterCreate("ContrastEnhancement", ""); // Insert the frame filter in the device path of IC. icImagingControl1.DeviceFrameFilters.Add(filter); // Enable the filter. cbEnable.Checked = true; // Initialize the Enable check box. filter.SetBoolParameter("Enable", cbEnable.Checked); InitControls(); UpdateControls(); icImagingControl1.LiveStart(); } VB.NET Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load IcImagingControl1.ShowDeviceSettingsDialog() If Not IcImagingControl1.DeviceValid Then 'Unload(Me) Exit Sub End If filter = IcImagingControl1.FrameFilterCreate("ContrastEnhancement", "") ' Insert the frame filter in the device path of IC. IcImagingControl1.DeviceFrameFilters.Add(Filter) ' Enable the filter filter.SetBoolParameter("Enable", True) InitControls() UpdateControls() IcImagingControl1.LiveStart() End Sub The function InitControls initializes the minimum and maximum values of the scrollbars sldLowerBound and sldUpperBound. C# private void InitControls() { sldLowerBound.Minimum = 0; sldLowerBound.Maximum = 255; sldUpperBound.Minimum = 0; sldUpperBound.Maximum = 255; } VB.NET Private Sub InitControls() sldLowerBound.Minimum = 0 sldLowerBound.Maximum = 255 sldUpperBound.Minimum = 0 sldUpperBound.Maximum = 255 End Sub The function UpdateControls assigns the filter's parameters "Enable", "Lower Bound" and "Upper Bound" to the checkbox cbEnable and the scrollbars sldLowerBound und sldUpperBound. C# private void UpdateControls() { if (filter.GetBoolParameter("Enable")) cbEnable.Checked = true; else cbEnable.Checked = false; sldLowerBound.Value = filter.GetIntParameter("Lower Bound"); txLowerBound.Text = sldLowerBound.Value.ToString(); sldUpperBound.Value = filter.GetIntParameter("Upper Bound"); txUpperBound.Text = sldUpperBound.Value.ToString(); } VB.NET Private Sub UpdateControls() If filter.GetBoolParameter("Enable") Then cbEnable.Checked = True Else cbEnable.Checked = False End If sldLowerBound.Value = filter.GetIntParameter("Lower Bound") txLowerBound.Text = sldLowerBound.Value sldUpperBound.Value = filter.GetIntParameter("Upper Bound") txUpperBound.Text = sldUpperBound.Value End Sub The event handler for the scrollbar sldLowerBound ensures that its value is equal to or less than the value of the scrollbar sldUpperBound. After this check, the scrollbar value is assigned to the filter's parameter "Lower Bound" and the text field to the right of the scrollbar is updated. The event handler for the scrollbar sldUpperBound works similarly. C# private void sldLowerBound_Scroll(object sender, System.EventArgs e) { if (sldLowerBound.Value >= sldUpperBound.Value) sldLowerBound.Value = sldUpperBound.Value - 1; filter.SetIntParameter("Lower Bound", sldLowerBound.Value); txLowerBound.Text = sldLowerBound.Value.ToString(); } private void sldUpperBound_Scroll(object sender, System.EventArgs e) { if (sldUpperBound.Value <= sldLowerBound.Value) sldUpperBound.Value = sldLowerBound.Value + 1; filter.SetIntParameter("Upper Bound", sldUpperBound.Value); txUpperBound.Text = sldUpperBound.Value.ToString(); } VB.NET Private Overloads Sub sldLowerBound_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sldLowerBound.Scroll If sldLowerBound.Value >= sldUpperBound.Value Then sldLowerBound.Value = sldUpperBound.Value - 1 End If filter.SetIntParameter("Lower Bound", sldLowerBound.Value) txLowerBound.Text = sldLowerBound.Value End Sub Private Overloads Sub sldUpperBound_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sldUpperBound.Scroll If sldUpperBound.Value <= sldLowerBound.Value Then sldUpperBound.Value = sldLowerBound.Value + 1 End If filter.SetIntParameter("Upper Bound", sldUpperBound.Value) txUpperBound.Text = sldUpperBound.Value End Sub |