1. 程式人生 > 其它 >26、.NET與Halcon混合程式設計(1)

26、.NET與Halcon混合程式設計(1)

HWindowXCtrl與HWindowControl都是Haocon提供的用於顯示影象的互動式控制元件,但它們功能上也存在一些明顯的差異。它們區別如下:

⑴、引用檔案(dll)不同

HWindowXCtrl引用來自:halconx.dll(預設路徑C:\Program Files\MVTec\HALCON-10.0\bin\x86sse2-win32\halconx.dll

HWindowControl引用來自:dotnet35.dll(預設路徑C:\Program Files\MVTec\HALCON-10.0\bin\dotnet35)

⑵、功能

HWindowXCtrl控制元件缺少一些重要有用的事件如HMouseWheel(滑鼠中鍵滾輪滑動,用於縮放影象)

HWindowControl擁有較齊全的事件。

說明:.NET中優先使用HWindowControl,而HWindowXCtrl可用於MFC中(但MFC中也不常用HWindowXCtrl控制元件,MFC顯示影象常用picturebox)下面介紹 HWindowControl控制元件的使用方法

一、處理影象並準備vb.net程式碼

  1. 在HDevelop中處理影象:

    read_image (Image12, 'C:/12.jpg')

    *把彩色圖轉換為灰度圖

    rgb1_to_gray (Image12, GrayImage)

    *顯式關閉視窗

    dev_close_window ()

    *顯式開啟視窗

    dev_open_window (0, 0, 512, 512, 'black', WindowHandle)

    *顯式呼叫顯示影象

    dev_display (GrayImage)

2、從halcon匯出VB.NET程式碼:

3、匯出的程式碼:

'

' File generated by HDevelop for HALCON/DOTNET (Visual Basic .NET) Version 10.0

'

 

Option Strict Off

 

Option Explicit

 

Imports HalconDotNet

 

Imports System

Imports Microsoft.VisualBasic

Module Daochu

 

' Main procedure

Private Sub action()

 

' Local iconic variables

Dim ho_Image12 As HObject = Nothing, ho_GrayImage As HObject = Nothing

 

 

' Local control variables

Dim hv_WindowHandle As HTuple = Nothing

 

' Initialize local and output iconic variables

HOperatorSet.GenEmptyObj(ho_Image12)

HOperatorSet.GenEmptyObj(ho_GrayImage)

 

ho_Image12.Dispose()

HOperatorSet.ReadImage(ho_Image12, new HTuple("C:/12.jpg"))

'把彩色圖轉換為灰度圖

ho_GrayImage.Dispose()

HOperatorSet.Rgb1ToGray(ho_Image12, ho_GrayImage)

'顯式關閉視窗

If (HDevWindowStack.IsOpen()) Then

HOperatorSet.CloseWindow(HDevWindowStack.Pop())

End If

'顯式開啟視窗

HOperatorSet.SetWindowAttr("background_color",new HTuple("black"))

HOperatorSet.OpenWindow(new HTuple(0),new HTuple(0),new HTuple(512),new HTuple(512),0,"","",hv_WindowHandle)

HDevWindowStack.Push(hv_WindowHandle)

'顯式呼叫顯示影象

If (HDevWindowStack.IsOpen()) Then

HOperatorSet.DispObj(ho_GrayImage, HDevWindowStack.GetActive())

End If

ho_Image12.Dispose()

ho_GrayImage.Dispose()

 

End Sub

 

 

 

#If Not NO_EXPORT_APP_MAIN Then

Sub InitHalcon()

' Default settings used in HDevelop

Call HOperatorSet.SetSystem(New HTuple("do_low_error"), New HTuple("false"))

 

End Sub

 

Sub Main(ByVal args() As String)

Call InitHalcon()

Call action()

End Sub

#End If

 

 

End Module

二、把控制元件新增到介面上

1、從工具箱中新增控制元件:

選擇項---在.NET Framework元件選項卡----瀏覽---- 新增C:\Program Files\MVTec\HALCON-10.0\bin目錄下的dotnet35.dll

2、拖拽控制元件到介面上:

注:新增該控制元件後,新增以下選中的引用。

三、根據實際需要把匯出程式碼複製到vb.net工程上

1、在vb.net工程上呼叫呼叫action()函式

把匯出程式碼中的action()函式和相應的名稱空間複製到vb.net工程中,並在button控制元件的響應函式

    中呼叫action()函式

 

Imports HalconDotNet

 

Imports System

Imports Microsoft.VisualBasic

 

 

Public Class Form1

Private Sub action()

 

' Local iconic variables

Dim ho_Image12 As HObject = Nothing, ho_GrayImage As HObject = Nothing

 

 

' Local control variables

Dim hv_WindowHandle As HTuple = Nothing

 

' Initialize local and output iconic variables

HOperatorSet.GenEmptyObj(ho_Image12)

HOperatorSet.GenEmptyObj(ho_GrayImage)

 

ho_Image12.Dispose()

HOperatorSet.ReadImage(ho_Image12, New HTuple("C:/12.jpg"))

'把彩色圖轉換為灰度圖

ho_GrayImage.Dispose()

HOperatorSet.Rgb1ToGray(ho_Image12, ho_GrayImage)

'顯式關閉視窗

If (HDevWindowStack.IsOpen()) Then

HOperatorSet.CloseWindow(HDevWindowStack.Pop())

End If

'顯式開啟視窗

HOperatorSet.SetWindowAttr("background_color", New HTuple("black"))

HOperatorSet.OpenWindow(New HTuple(0), New HTuple(0), New HTuple(512), New HTuple(512), 0, "", "", hv_WindowHandle)

HDevWindowStack.Push(hv_WindowHandle)

'顯式呼叫顯示影象

If (HDevWindowStack.IsOpen()) Then

HOperatorSet.DispObj(ho_GrayImage, HDevWindowStack.GetActive())

End If

ho_Image12.Dispose()

ho_GrayImage.Dispose()

 

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

action()

End Sub

End Class

  1. 執行程式:

    提示錯誤:無法載入 DLL"halcon": 找不到指定的模組。 (異常來自 HRESULT:0x8007007E)。

  2. 解決辦法:

    把halcon安裝目錄(C:\Program Files\MVTec\HALCON-10.0\bin\x86sse2-win32)下的halcon.dll複製到vb.net工程的目錄下(…..\bin\Debug,即跟可執行檔案.exe同一個資料夾)或複製到C:\Windows\System32路徑下。

  3. 再次執行效果:

五、改進

從上面的效果看出,這並不是我們想要的結果,因為在程式顯示影象的程式碼為:

HOperatorSet.DispObj(ho_GrayImage, HDevWindowStack.GetActive())

可以看出,它是把ho_Image12影象顯示到由

HOperatorSet.OpenWindow(New HTuple(0), New HTuple(0), New HTuple(512), New HTuple(512),

                                    0, "", "", hv_WindowHandle)

HDevWindowStack.Push(hv_WindowHandle)

建立的視窗控制代碼HDevWindowStack.GetActive()的視窗上。

  1. 改進方法:把action()函式修改為一下形式

    原理:獲取HWindowControl1的控制代碼,並通過控制代碼把影象顯示到該控制元件上。修改了'********內部分

 

Imports HalconDotNet

 

Imports System

Imports Microsoft.VisualBasic

 

 

Public Class Form1

Private Sub action()

 

' Local iconic variables

Dim ho_Image12 As HObject = Nothing, ho_GrayImage As HObject = Nothing

 

 

' Local control variables

Dim hv_WindowHandle As HTuple = Nothing

 

' Initialize local and output iconic variables

HOperatorSet.GenEmptyObj(ho_Image12)

HOperatorSet.GenEmptyObj(ho_GrayImage)

 

ho_Image12.Dispose()

HOperatorSet.ReadImage(ho_Image12, New HTuple("C:/12.jpg"))

'把彩色圖轉換為灰度圖

ho_GrayImage.Dispose()

HOperatorSet.Rgb1ToGray(ho_Image12, ho_GrayImage)

        '*****************************************************************************

'把用於顯示影象的控制代碼設定為HWindowControl1控制元件的控制代碼

hv_WindowHandle = HWindowControl1.HalconWindow

'適應控制元件大小顯示影象

Dim hv_Width, hv_Height As HTuple

HOperatorSet.GetImageSize(ho_GrayImage, hv_Width, hv_Height)

HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width) '適應視窗顯示

'顯示影象

HOperatorSet.DispObj(ho_GrayImage, hv_WindowHandle)

'*****************************************************************************

ho_Image12.Dispose()

ho_GrayImage.Dispose()

 

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

action()

End Sub

End Class

  1. 效果:

六、實現互動式繪製

在工程實際需要中,互動繪製一個region或繪製一個圓形、矩形等是非常重要的,如在定位中減少模板定義域就可以用滑鼠繪製區域來實現。HWindowControl控制元件本身就有這個動能,在程式中呼叫DrawRegion(out HalconDotNet.HObject region, HalconDotNet.HTuple windowHandle)運算元即可。

Private Sub Button2_Click(………………..) Handles Button2.Click

HWindowControl1.Focus()

Dim region As HObject = Nothing

Dim out_img As HObject = Nothing

HOperatorSet.SetColor(hv_WindowHandle, New HTuple("yellow"))

HOperatorSet.DrawRegion(region, hv_WindowHandle)

HOperatorSet.ReduceDomain(ho_GrayImage, region, out_img)

HOperatorSet.ClearWindow(hv_WindowHandle)

HOperatorSet.DispObj(out_img, hv_WindowHandle)

End Sub

 

效果:

滑鼠左鍵繪製區域

    右鍵單擊確定: