圖像處理: 簡單二值化圖像處理

下面這個示例程序將介紹如何根據設定的閾值,使用IC Imaging Control對圖像的灰度值進行簡單的二值化處理。
語言:Visual Basic 6
版本:3.0.3
发布于:2005年4月7日
作者:IC Imaging Control技術支持部
需求:IC Imaging Control >2.1
由WDM數據流類驅動程序驅動的相機、視頻轉換器或圖像採集卡

示例程序窗口如下圖所示:

The Testdialog of IC Imaging Control,

程序開始後首先調用內置對話框(.ShowDeviceSettingsDialog)用於選擇視頻設備。 而後通過.LiveStart顯示設備傳來的現場畫面數據流。 .MemoryCurrentGrabberColorformat = ICY8語句確保二值化算法得到的是8比特灰度級的圖像:

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

用戶點擊"Snap Image", .MemorySnapImage就會從現場視頻數據流中抓取一幀圖像並將其寫入環形緩存。

Private Sub cmdSnap_Click()

    ICImagingControl1.MemorySnapImage
    ICImagingControl1.Display

    cmdBinarize.Enabled = True

End Sub

用戶點擊"Binarize",程序首先讀取用戶之前設定好的閾值 (threshold = txtThreshold.Text),而後創建圖像緩存的引用ib, 並將其設為環形緩存中的當前圖像(ImageBuffers.CurrentIndex)。 實際的圖像處理操作都是在下面的兩個for循環中。如果當前像素的灰度值 imageData(x,y)大於所設定的閾值,則將其設為255,反之則設為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

相關源代碼示例

責任聲明
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.