26、.NET與Halcon混合程式設計(縮放、平移)
1、get_image_size(Image : : : Width, Height)
功能:獲取影象的真實大小(不受放縮、平移影響)
read_image (Image, 'fabrik')
get_image_size (Image, Width, Height)
2、get_part( : : WindowHandle : Row1, Column1, Row2, Column2)
功能:獲取圖形視窗左上角、右下角座標。
read_image (Image,
'fabrik')
get_part (3600, Row1, Column1, Row2, Column2)
3、dev_set_part( : : Row1, Column1, Row2, Column2 : )
功能:設定圖形視窗左上角、右下角座標。
read_image (Image, 'fabrik')
dev_set_part (-120, -90, 600, 800)
*要顯式呼叫顯示運算元才看到效果
dev_display (
4、get_mposition( : : WindowHandle : Row, Column, Button)
功能:該運算元在HDevelop中無法執行,它的主要是在.NET程式設計中獲取HWindowControl控制元件滑鼠當前位置的像
素座標。
例項說明:
在呼叫
HOperatorSet.SetPart(hv_WindowHandle,-50, -50, 300, 400)
HOperatorSet.ClearWindow(hv_WindowHandle)
HOperatorSet.DispObj(ho_Image12,
後
若:
Private Sub HWindowControl1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles HWindowControl1.MouseMove
Me.Text = "(" + e.X.ToString + "," + e.Y.ToString + ")"
End Sub
則
若:
Private Sub HWindowControl1_MouseMove(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles HWindowControl1.MouseMove
Dim X As HTuple = Nothing
Dim Y As HTuple = Nothing
'畫素精度
HOperatorSet.GetMposition(hv_WindowHandle, X, Y, 0)
'亞畫素精度
'HOperatorSet.GetMpositionSubPix(hv_WindowHandle, X, Y, 0)
Me.Text = "(" + X.ToString + "," + Y.ToString + ")"
End Sub
則:
5、get_mposition_sub_pix( : : WindowHandle : Row, Column, Button)
功能:和4一樣,但它提取的畫素座標為亞畫素精度
一、繪製區域:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'p是一個布林型別的變數,用於判斷是繪製區域還是移動影象,下面程式碼表示繪製區域,所以想把它置為True,
'防止在滑鼠按下時被認為執行移動影象操作
p = True
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)
p = False
End Sub
二、移動影象
1、HWindowControl1.HMouseDown事件中記錄初識當前滑鼠在影象中的座標,以及GetPart
Private Sub HWindowControl1_HMouseDown(ByVal sender As System.Object, ByVal e As HalconDotNet.HMouseEventArgs) Handles HWindowControl1.HMouseDown
'p是一個布林型別的變數,用於判斷是繪製區域還是移動影象
If p = False And e.Button = Windows.Forms.MouseButtons.Left Then
HWindowControl1.Cursor = System.Windows.Forms.Cursors.NoMove2D
HOperatorSet.GetMposition(hv_WindowHandle, X1, Y1, 1)
Label1.Text = "(" + X1.ToString + "," + Y1.ToString + ")"
HOperatorSet.GetPart(hv_WindowHandle, row1, column1, row2, column2)
End If
End Sub
2、HWindowControl1.HMouseUp事件中記錄移動後的滑鼠在影象中的座標,再根據移動前後的座標差值
呼叫SetPart來平移影象
Private Sub HWindowControl1_HMouseUp(ByVal sender As System.Object, ByVal e As HalconDotNet.HMouseEventArgs) Handles HWindowControl1.HMouseUp
'p是一個布林型別的變數,用於判斷是繪製區域還是移動影象
If p = False And e.Button = Windows.Forms.MouseButtons.Left Then
HOperatorSet.GetMposition(hv_WindowHandle, X2, Y2, 1)
Label2.Text = "(" + X2.ToString + "," + Y2.ToString + ")"
Dim t1 As Integer = X1 - X2
Dim t2 As Integer = Y1 - Y2
Label3.Text = t1.ToString
HOperatorSet.ClearWindow(hv_WindowHandle)
HOperatorSet.SetPart(hv_WindowHandle, row1 + t1, column1 + t2, row2 + t1, column2 + t2)
HOperatorSet.DispObj(ho_Image12, hv_WindowHandle)
HWindowControl1.Cursor = System.Windows.Forms.Cursors.Default
End If
End Sub
三、縮放影象
原理:先獲取原影象的大小(GetPart),若滑鼠滾輪向上滑動,則它大小變為原來的0.9倍,若向下滑動,則變為1.1倍。
Private Sub HWindowControl1_HMouseWheel(ByVal sender As System.Object, ByVal e As HalconDotNet.HMouseEventArgs) Handles HWindowControl1.HMouseWheel
HOperatorSet.GetPart(hv_WindowHandle, row1, column1, row2, column2)
If e.Delta > 0 Then
row1 = row1 * 0.9
column1 = column1 * 0.9
row2 = row2 * 0.9
column2 = column2 * 0.9
HOperatorSet.ClearWindow(hv_WindowHandle)
HOperatorSet.SetPart(hv_WindowHandle, row1, column1, row2, column2)
HOperatorSet.DispObj(ho_Image12, hv_WindowHandle)
ElseIf e.Delta < 0 Then
HOperatorSet.ClearWindow(hv_WindowHandle)
HOperatorSet.SetPart(hv_WindowHandle, row1 * 1.1, column1 * 1.1, row2 * 1.1, column2 * 1.1)
HOperatorSet.DispObj(ho_Image12, hv_WindowHandle)
End If
End Sub