软件

我通过两个方式编写了这个软件,一个是vc++,一个是VB。你能发现VB版本的软件会比VC++的软件更容易一些,但是各有取舍。VC++版本能够自由的加入其他软件中?VB版本需要第三方软件支持(在Visual Studio中)

Visual Basic

vb_laser_ranger.zip

这里可以下载到我的VB版本软件。
要使用上面的程序,你必须要安装VideoOCX ActiveX 控件

主程序如下:


Private Sub exit_Click()
    ' only if running...
    If (Timer1.Enabled) Then
       
        Timer1.Enabled = False  'Stop Timer
        VideoOCX.Stop
        VideoOCX.Close
               
    End If
   
    End
End Sub

Private Sub Start_Click() 'Init VideoOCX Control, allocate memory and start grabbing
        
    If (Not Timer1.Enabled) Then
        Start.Caption = "Stop"
 
        ' Disable internal error messages in VideoOCX
        VideoOCX.SetErrorMessages False
   
        ' Init control
        If (Not VideoOCX.Init) Then
            ' Init failed. Display error message and end sub
            MsgBox VideoOCX.GetLastErrorString, vbOKOnly, "VideoOCX Error"
            End
        Else
            ' Allocate memory for global image handle
            capture_image = VideoOCX.GetColorImageHandle
            ' result_image = VideoOCX_Processed.GetColorImageHandle
   
            Timer1.Enabled = True 'Start capture timer
   
            ' Start Capture mode
            If (Not VideoOCX.Start) Then
                ' Start failed. Display error message and end sub
                MsgBox VideoOCX.GetLastErrorString, vbOKOnly, "VideoOCX Error"
                End
            End If
        End If
    Else
        Start.Caption = "Start"
        Timer1.Enabled = False  'Stop Timer
        VideoOCX.Stop
        VideoOCX.Close
    End If
   
End Sub

Private Sub Timer1_Timer()
    ' Timer for capturing - handles videoOCXTools
    Dim matrix As Variant
    Dim height, width As Integer
    Dim r, c As Integer
    Dim max_r, max_c As Integer
    Dim max_red As Integer
    Dim gain, offset As Variant
    Dim h_cm As Variant
    Dim range As Integer
    Dim pixels_from_center As Integer
   
    ' Calibrated parameter for pixel to distance conversion
    gain = 0.0024259348
    offset = -0.056514344
    h_cm = 5.842
   
    max_red = 0
   
    ' Capture an image
    If (VideoOCX.Capture(capture_image)) Then
       
        ' VideoOCX.Show capture_image
       
        ' Matrix transformation initialization
        matrix = VideoOCX.GetMatrix(capture_image)
       
        height = VideoOCX.GetHeight
        width = VideoOCX.GetWidth
       
        ' Image processing code
       
        ' The laser dot should not be seen above the middle row (with a little pad)
        For r = height / 2 - 20 To height - 1
           
            ' Our physical setup is roughly calibrated to make the laser
            ' dot in the middle columns...dont bother lookng too far away
            For c = width / 2 - 25 To width / 2 + 24
       
                ' Look for the largest red pixel value in the scene (red laser)
                If (matrix(c, r, 2) > max_red) Then
                    max_red = matrix(c, r, 2)
                    max_r = r
                    max_c = c
                End If
            Next c
        Next r
       
        ' Calculate the distance for the laser dot from middle of frame
        pixels_from_center = max_r - 120

        ' Calculate range in cm based on calibrated parameters
        range = h_cm / Tan(pixels_from_center * gain + offset)

        ' Print laser dot position row and column to screen
        row_val.Caption = max_r
        col_val.Caption = max_c
       
        ' Print range to laser illuminated object to screen
        range_val.Caption = range
       
        ' Draw a red vertical line to intersect target
        For r = 0 To height - 1
            matrix(max_c, r, 2) = 255
        Next r
       
        ' Draw a red horizontal line to intersect target
        For c = 0 To width - 1
            matrix(c, max_r, 2) = 255
        Next c
       
        VideoOCX.ReleaseMatrixToImageHandle (capture_image)RoboticFan
       
    End If
   
    VideoOCX.Show capture_image

End Sub

截屏如下: