Image Processing: Simple Binarization

The following programming example shows how you can use IC Imaging Control to realize a binarization of a graylevel image with a fixed threshold.
Language:Visual Basic 6
Version:3.0.3
Released on:April 7, 2005
Author:IC Imaging Control Support Department
Requirements:IC Imaging Control >2.1
Camera, converter or grabber with WDM Stream Class drivers.

The sample application's window looks as follows:

The Testdialog of IC Imaging Control,

The program starts by activating a built-in dialog to select a device (.ShowDeviceSettingsDialog). Then the device's live image data stream is displayed using .LiveStart (see dialog on the right). .MemoryCurrentGrabberColorformat = ICY8 makes sure that the binarization algorithm obtains a 8 bit graylevel image:

Private Sub Form_Load()
    ICImagingControl1.ShowDeviceSettingsDialog

    If Not ICImagingControl1.DeviceValid Then
        Unload Me
        Exit Sub
    End If

    ICImagingControl1.MemoryCurrentGrabberColorformat = ICY8

    ICImagingControl1.LiveDisplay = False
    ICImagingControl1.LiveStart
End Sub

When the user clicks "Snap Image", .MemorySnapImage grabs an image from the image data stream and writes it into an internal ring buffer.

Private Sub cmdSnap_Click()

    ICImagingControl1.MemorySnapImage
    ICImagingControl1.Display

    cmdBinarize.Enabled = True

End Sub

When the user clicks "Binarize", first of all the program acquires the threshold determined previously by the user (threshold = txtThreshold.Text). Hereupon the program creates the image buffer reference ib and sets it to the ring buffer's current image (ImageBuffers.CurrentIndex). The two for loops that follow, represent the actual image processing algorithm. If the current pixel's graylevel imageData(x,y) is more than the desired threshold, it is set to 255; otherwise it is set to 0:

Private Sub cmdBinarize_Click()

    Dim threshold As Integer
    threshold = txtThreshold.Text

    With ICImagingControl1

        Dim ib As ImageBuffer
        Set ib = .ImageBuffers(.ImageBuffers.CurrentIndex)

        Dim imageData As Variant
        imageData = ib.GetImageData

        For y = 0 To ib.Lines - 1
            For x = 0 To ib.PixelPerLine - 1

                If imageData(x, y) > threshold Then
                    imageData(x, y) = 255
                Else
                    imageData(x, y) = 0
                End If

            Next x
        Next y

        ib.ReleaseImageData imageData

        .Display

    End With

End Sub

Related Source Code Samples

Disclaimer
The source code that appears in the IC Imaging Control Source Code Library is indented for educational purposes only. The Imaging Source Europe GmbH, the manufacturer of IC Imaging Control, does not assume any kind of warranty expressed or implied, resulting from the use of the content of this page.

This site is part of The Imaging Source Network. Other sites include Company Portal, Image Processing, Astronomy Cameras, Astronomy Cameras Blog, Blog caméras d'astronomie, Astronomy Cameras Competition, TX Text Control, LiveDocx, phpLiveDocx and Forums.