DIY基于摄像头的激光测距仪
作者:Rockets   文章来源: www.RoboticFan.com    更新时间:2008年05月11日   打印此文    浏览数:

软件

我通过两个方式编写了这个软件,一个是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

截屏如下:




首页 上一页 1 2 3 4 5 6 下一页 尾页 跳转到

转载声明:凡文章出处为www.RoboticFan.com的,系本站的原创文章。其它媒体在注明出处为RoboticFan.com并给出原始链接后可以自由转载,否则将视为侵权!

上一篇:Wall E 真机演示 下一篇:ASIMO上班咯!

最近更新
  • 世嘉玩具与孩之宝将上市音乐机器人
  • Vstone携手KumoTek推出KT-X类人机器人系列
  • iRobot军用机器人最新杰作:“金属风暴”
  • 机器人工具包开始预定
  • 迪士尼与WowWee联手打造Wall-E系列产品
  • Arduino-目前为止最容易开发的控制板
  • 微软发布Robotics Developer Studio 2008 CTP
  • roBlocks——“块状”机器人
  • 后AIBO时代的机器狗——G-dog
  • 机器恐龙Pleo升级战

  • 相关文章
  • 很抱歉!还没有相关内容

  • 推荐新闻

    赞助商广告