home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / eMe_ID_Car2055213222007.psc / ClassMotion.cls < prev    next >
Text File  |  2006-07-23  |  14KB  |  520 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "ClassMotion"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. Dim hwndc As Long
  17. Const SRCCOPY = &HCC0020
  18. Const SRCINVERT = &H660046
  19.  
  20.  
  21. Dim prev_image As StdPicture
  22. Const MotionResolution = 20
  23. Dim motion(MotionResolution + 1, MotionResolution + 1, 2) As Boolean
  24.  
  25. Public NoOfAttentionBoxes As Integer
  26. Dim attentionBox(100, 4)
  27. Dim RegionArea As Integer
  28.  
  29. Dim attention_x As Integer
  30. Dim attention_y As Integer
  31.  
  32. Public Tracking As Boolean
  33. Dim prev_tracking As Boolean
  34. Dim track_x As Integer
  35. Dim track_y As Integer
  36. Public velocity_x As Single
  37. Public velocity_y As Single
  38.  
  39. Const TemplateSize = 10
  40. Dim template(TemplateSize + 1, TemplateSize + 1) As Byte
  41.  
  42. Dim rgbsource As RGBthingy
  43. Dim rgbdest As RGBpoint
  44.  
  45.  
  46. Public Sub Vision_VFWstart(canvas As PictureBox)
  47. 'starts VFW
  48.   Dim temp As Long
  49.  
  50.   hwndc = capCreateCaptureWindow("Rodney Vision", ws_child Or ws_visible, 0, 0, 320, 240, canvas.hwnd, 0)
  51.   If (hwndc <> 0) Then
  52.     temp = SendMessage(hwndc, wm_cap_driver_connect, 0, 0)
  53.     temp = SendMessage(hwndc, wm_cap_set_preview, 1, 0)
  54.     temp = SendMessage(hwndc, WM_CAP_SET_PREVIEWRATE, 30, 0)
  55.     Else
  56.     MsgBox ("Can't open capture window")
  57.   End If
  58. End Sub
  59.  
  60.  
  61. Public Sub Vision_VFWFormatDialog()
  62.   Dim temp As Long
  63.   temp = SendMessage(hwndc, WM_CAP_DLG_VIDEOFORMAT, 0&, 0&)
  64. End Sub
  65.  
  66.  
  67. Public Sub Vision_VFWgrab(Destination As PictureBox)
  68. On Error Resume Next
  69.  
  70. 'grabs a frame to the given picturebox
  71.   Dim temp As Long
  72.   
  73.   temp = SendMessageAsLong(hwndc, WM_CAP_GRAB_FRAME, 0&, 0&)
  74.   temp = SendMessage(hwndc, WM_CAP_EDIT_COPY, 1, 0)
  75.   Destination.Picture = Clipboard.GetData
  76.   
  77. End Sub
  78.  
  79.  
  80. Public Sub Vision_CentreOfMotion(canvas As PictureBox, ByRef cx As Single, ByRef cy As Single, inputImage As PictureBox, targets As PictureBox)
  81. 'returns the centre of motion
  82.   Dim x As Integer
  83.   Dim y As Integer
  84.   Dim sx As Integer
  85.   Dim sy As Integer
  86.   Dim p As Long
  87.   Dim p2 As Long
  88.   Dim tot As Double
  89.   Dim X2 As Integer
  90.   Dim Y2 As Integer
  91.   Dim rc As Long
  92.   Dim surrounding As Integer
  93.   
  94.   If (Not Tracking) Then
  95.   
  96.   X2 = 0
  97.   tot = 0
  98.   sx = canvas.ScaleWidth / MotionResolution
  99.   sy = canvas.ScaleHeight / MotionResolution
  100.   For x = sx To canvas.ScaleWidth - 1 Step sx
  101.     Y2 = 0
  102.     For y = sy To canvas.ScaleHeight - 1 Step sy
  103.       p = canvas.Point(x, y)
  104.       rgbsource.Value = p
  105.       Call CopyMemory(rgbdest, rgbsource, 3)
  106.       motion(X2, Y2, 1) = 0
  107.       If (rgbdest.Red > 10) And (rgbdest.Green > 10) And (rgbdest.Blue > 10) Then
  108.         motion(X2, Y2, 0) = True
  109.         Else
  110.         motion(X2, Y2, 0) = False
  111.       End If
  112.       Y2 = Y2 + 1
  113.     Next
  114.     X2 = X2 + 1
  115.   Next
  116.     
  117.   Call getAttentionBoxes(attention_x, attention_y)
  118.   Call showAttentionBoxes(inputImage, targets)
  119.   
  120.   canvas.FillColor = RGB(0, 255, 0)
  121.   canvas.FillStyle = 0
  122.   canvas.Circle ((attention_x / MotionResolution * canvas.ScaleWidth), (attention_y / MotionResolution * canvas.ScaleHeight)), sx
  123.   
  124.   cx = attention_x / MotionResolution
  125.   cy = attention_y / MotionResolution
  126.   
  127.   End If
  128. End Sub
  129.  
  130.  
  131.  
  132. Public Sub Vision_VFWstop()
  133.   Dim temp As Long
  134.   
  135.   temp = SendMessageAsLong(hwndc, WM_CAP_DRIVER_DISCONNECT, 0&, 0&)
  136. End Sub
  137.  
  138.  
  139. Public Sub Vision_Motion(inputImage As PictureBox, backgroundImage As PictureBox, motionImage As PictureBox)
  140. 'BitBlit motion compare
  141.   Dim rc As Long
  142.   Static firstCall As Integer
  143.   Static t As Integer
  144.   Dim x As Integer
  145.   Dim y As Integer
  146.   
  147.   Call Vision_VFWgrab(inputImage)
  148.   
  149.   If (Not Tracking) Then
  150.   
  151.     If (firstCall = 0) Then
  152.       firstCall = 1
  153.       Set prev_image = inputImage.Picture
  154.     End If
  155.   
  156.     t = t + 1
  157.     If (t > 1) Then
  158.       Set backgroundImage.Picture = prev_image
  159.       Set prev_image = inputImage.Picture
  160.       t = 0
  161.     End If
  162.   
  163.     rc = BitBlt(motionImage.hDC, 0, 0, inputImage.ScaleWidth, inputImage.ScaleHeight, backgroundImage.hDC, 0, 0, SRCCOPY)
  164.     rc = BitBlt(motionImage.hDC, 0, 0, inputImage.ScaleWidth, inputImage.ScaleHeight, inputImage.hDC, 0, 0, SRCINVERT)
  165.     
  166.     Else
  167.     
  168.     Call TrackArea(inputImage)
  169.   End If
  170.  
  171.   prev_tracking = Tracking
  172.  
  173. End Sub
  174.  
  175.  
  176.  
  177. Private Sub TrackArea(inputImage As PictureBox)
  178. 'tracks the movement of a small area of the image
  179.   Static W As Integer
  180.   Static H As Integer
  181.   Static stp_x As Integer
  182.   Static stp_y As Integer
  183.   Static stp_x2 As Integer
  184.   Static stp_y2 As Integer
  185.   Static firstCall As Integer
  186.   Dim p As Long
  187.   Dim x As Integer
  188.   Dim y As Integer
  189.   Dim xx As Integer
  190.   Dim yy As Integer
  191.   Dim i As Integer
  192.   Dim ox As Integer
  193.   Dim oy As Integer
  194.   Dim dist As Long
  195.   Dim minDist As Long
  196.   Dim p1 As Integer
  197.   Dim p2 As Integer
  198.   Static maxDifference As Long
  199.   Static dx As Single
  200.   Static dy As Single
  201.   Static ticks As Long
  202.   Dim tx As Integer
  203.   Dim ty As Integer
  204.   
  205.   If (firstCall = 0) Then
  206.     W = inputImage.ScaleWidth / 15
  207.     H = inputImage.ScaleHeight / 15
  208.     stp_x = (W * 2) / TemplateSize
  209.     stp_y = (H * 2) / TemplateSize
  210.     stp_x2 = stp_x * 2
  211.     stp_y2 = stp_y * 2
  212.     firstCall = 1
  213.     maxDifference = TemplateSize * TemplateSize * 30
  214.   End If
  215.   
  216.   If (Not prev_tracking) And (Tracking) Then
  217.     track_x = attention_x / MotionResolution * inputImage.ScaleWidth
  218.     track_y = attention_y / MotionResolution * inputImage.ScaleHeight
  219.     dx = 0
  220.     dy = 0
  221.     ticks = 0
  222.   End If
  223.     
  224.   tx = track_x
  225.   ty = track_y
  226.   minDist = maxDifference
  227.   For i = 0 To 8
  228.     Select Case i
  229.       Case 0
  230.         ox = tx + stp_x2
  231.         oy = ty - stp_y2
  232.       Case 1
  233.         ox = tx + stp_x2
  234.         oy = ty
  235.       Case 2
  236.         ox = tx + stp_x2
  237.         oy = ty + stp_y2
  238.       Case 3
  239.         ox = tx
  240.         oy = ty + stp_y2
  241.       Case 4
  242.         ox = tx - stp_x2
  243.         oy = ty + stp_y2
  244.       Case 5
  245.         ox = tx - stp_x2
  246.         oy = ty
  247.       Case 6
  248.         ox = tx - stp_x2
  249.         oy = ty - stp_y2
  250.       Case 7
  251.         ox = tx
  252.         oy = ty - stp_y2
  253.       Case 8
  254.         ox = tx
  255.         oy = ty
  256.     End Select
  257.  
  258.     dist = 0
  259.     x = 0
  260.     For xx = ox - W To ox + W Step stp_x
  261.       y = 0
  262.       For yy = oy - H To oy + H Step stp_y
  263.         p = inputImage.Point(xx, yy)
  264.         rgbsource.Value = p
  265.         Call CopyMemory(rgbdest, rgbsource, 3)
  266.         p1 = rgbdest.Red
  267.         p2 = template(x, y)
  268.         dist = dist + Abs(p1 - p2)
  269.         
  270.         If (i = 8) Then
  271.           template(x, y) = rgbdest.Red
  272.         End If
  273.       
  274.         y = y + 1
  275.       Next
  276.       x = x + 1
  277.     Next
  278.    
  279.     If (dist < minDist) Then
  280.       minDist = dist
  281.       tx = ox
  282.       ty = oy
  283.     End If
  284.    
  285.   Next
  286.   
  287.   dx = dx + (track_x - tx)
  288.   dy = dy + (track_y - ty)
  289.   If (ticks > 4) Then
  290.     velocity_x = dx / 5
  291.     velocity_y = dy / 5
  292.     ticks = 0
  293.     dx = 0
  294.     dy = 0
  295.   End If
  296.   ticks = ticks + 1
  297.   
  298.   track_x = tx
  299.   track_y = ty
  300.   
  301.   If (prev_tracking) And (Tracking) Then
  302.     If (minDist <> maxDifference) Then
  303.       attention_x = track_x / inputImage.ScaleWidth * MotionResolution
  304.       attention_y = track_y / inputImage.ScaleHeight * MotionResolution
  305.       Else
  306.       Tracking = False
  307.     End If
  308.   End If
  309.   
  310. End Sub
  311.  
  312.  
  313.  
  314. Public Sub Vision_Filter(inputImage As PictureBox, colourImage As PictureBox, motionImage As PictureBox)
  315. 'BitBlit motion compare
  316.   Const SRCCOPY = &HCC0020
  317.   Const SRCINVERT = &H660046
  318.   Dim rc As Long
  319.     
  320.   Call Vision_VFWgrab(inputImage)
  321.   rc = BitBlt(motionImage.hDC, 0, 0, inputImage.ScaleWidth, inputImage.ScaleHeight, colourImage.hDC, 0, 0, SRCCOPY)
  322.   rc = BitBlt(motionImage.hDC, 0, 0, inputImage.ScaleWidth, inputImage.ScaleHeight, inputImage.hDC, 0, 0, SRCINVERT)
  323. End Sub
  324.  
  325.  
  326. Private Sub getAttentionBoxes(ByRef cx As Integer, ByRef cy As Integer)
  327.   Dim x As Integer
  328.   Dim y As Integer
  329.   Dim minX As Integer
  330.   Dim minY As Integer
  331.   Dim maxX As Integer
  332.   Dim maxY As Integer
  333.   Dim maxRegionArea As Integer
  334.   Dim biggest As Integer
  335.   
  336.   maxRegionArea = 0
  337.   biggest = 0
  338.   NoOfAttentionBoxes = 0
  339.   For x = 0 To MotionResolution - 1
  340.     For y = 0 To MotionResolution - 1
  341.       If (motion(x, y, 0)) And (Not motion(x, y, 1)) Then
  342.         RegionArea = 0
  343.         minX = x
  344.         minY = y
  345.         maxX = x
  346.         maxY = y
  347.         Call fillRegion(x, y, 0, minX, maxX, minY, maxY)
  348.         If (RegionArea > 5) And (NoOfAttentionBoxes < 100) Then
  349.           If (RegionArea > maxRegionArea) Then
  350.             maxRegionArea = RegionArea
  351.             biggest = NoOfAttentionBoxes
  352.           End If
  353.           attentionBox(NoOfAttentionBoxes, 0) = minX
  354.           attentionBox(NoOfAttentionBoxes, 1) = minY
  355.           attentionBox(NoOfAttentionBoxes, 2) = maxX
  356.           attentionBox(NoOfAttentionBoxes, 3) = maxY
  357.           NoOfAttentionBoxes = NoOfAttentionBoxes + 1
  358.         End If
  359.       End If
  360.     Next
  361.   Next
  362.   
  363.   If (NoOfAttentionBoxes > 0) Then
  364.     cx = attentionBox(biggest, 0) + ((attentionBox(biggest, 2) - attentionBox(biggest, 0)) / 2)
  365.     cy = attentionBox(biggest, 1) + ((attentionBox(biggest, 3) - attentionBox(biggest, 1)) / 2)
  366.   End If
  367. End Sub
  368.  
  369.  
  370. Private Sub showAttentionBoxes(inputImage As PictureBox, outputImage As PictureBox)
  371.   Dim minX As Integer
  372.   Dim minY As Integer
  373.   Dim maxX As Integer
  374.   Dim maxY As Integer
  375.   Dim tx As Integer
  376.   Dim ty As Integer
  377.   Dim bx As Integer
  378.   Dim by As Integer
  379.   Dim i As Integer
  380.   Dim rc As Long
  381.   Dim sx As Integer
  382.   Dim sy As Integer
  383.   Dim c As Long
  384.   
  385.   outputImage.Cls
  386.   outputImage.FillStyle = 1
  387.   outputImage.DrawWidth = 1
  388.  
  389.   sx = inputImage.ScaleWidth / MotionResolution
  390.   sy = inputImage.ScaleHeight / MotionResolution
  391.   For i = 0 To NoOfAttentionBoxes - 1
  392.     minX = attentionBox(i, 0)
  393.     minY = attentionBox(i, 1)
  394.     maxX = attentionBox(i, 2)
  395.     maxY = attentionBox(i, 3)
  396.     
  397.     tx = ((minX / MotionResolution) * inputImage.ScaleWidth)
  398.     ty = ((minY / MotionResolution) * inputImage.ScaleHeight)
  399.     bx = ((maxX / MotionResolution) * inputImage.ScaleWidth)
  400.     by = ((maxY / MotionResolution) * inputImage.ScaleHeight)
  401.     
  402.     c = RGB(0, 255, 0)
  403.     outputImage.Line (tx, ty)-(bx, by), c, B
  404.   Next
  405. End Sub
  406.  
  407.  
  408. Public Sub showFovea(inputImage As PictureBox, foveaImage As PictureBox)
  409. 'shows the fovea region
  410.   Dim tx As Integer
  411.   Dim ty As Integer
  412.   Dim bx As Integer
  413.   Dim by As Integer
  414.   Dim txx As Integer
  415.   Dim tyy As Integer
  416.   Dim bxx As Integer
  417.   Dim byy As Integer
  418.   Dim W As Integer
  419.   Dim H As Integer
  420.   Dim rc As Long
  421.   Dim sx As Integer
  422.   Dim sy As Integer
  423.   
  424.   foveaImage.Cls
  425.   
  426.   W = MotionResolution / 4
  427.   H = MotionResolution / 4
  428.   tx = attention_x - W + 1
  429.   If (tx < 0) Then
  430.     tx = 0
  431.   End If
  432.   bx = attention_x + W + 1
  433.   If (bx > MotionResolution - 1) Then
  434.     bx = MotionResolution - 1
  435.   End If
  436.   ty = attention_y - H
  437.   If (ty < 0) Then
  438.     attention_y = attention_y - ty
  439.     ty = 0
  440.   End If
  441.   by = attention_y + H
  442.   If (by > MotionResolution - 1) Then
  443.     by = MotionResolution - 1
  444.   End If
  445.   
  446.   txx = tx / MotionResolution * inputImage.ScaleWidth
  447.   tyy = ty / MotionResolution * inputImage.ScaleHeight
  448.   bxx = bx / MotionResolution * inputImage.ScaleWidth
  449.   byy = by / MotionResolution * inputImage.ScaleHeight
  450.   sx = (bx - tx) / MotionResolution * inputImage.ScaleWidth
  451.   sy = (by - ty) / MotionResolution * inputImage.ScaleHeight
  452.   rc = BitBlt(foveaImage.hDC, 0, 0, sx, sy, inputImage.hDC, txx, tyy, SRCCOPY)
  453.   
  454. End Sub
  455.  
  456.  
  457. Private Sub fillRegion(px As Integer, py As Integer, depth As Integer, ByRef minX As Integer, ByRef maxX As Integer, ByRef minY As Integer, ByRef maxY As Integer)
  458. 'fills a region with the given colour
  459.   
  460.   If (motion(px, py, 0)) And (motion(px, py, 1) = 0) Then
  461.   
  462.     motion(px, py, 1) = True
  463.     RegionArea = RegionArea + 1
  464.     
  465.     If (px < minX) Then
  466.       minX = px
  467.     End If
  468.     If (px > maxX) Then
  469.       maxX = px
  470.     End If
  471.     If (py < minY) Then
  472.       minY = py
  473.     End If
  474.     If (py > maxY) Then
  475.       maxY = py
  476.     End If
  477.     
  478.     If (depth < 100) Then
  479.     
  480.       If (py > 0) Then
  481.         Call fillRegion(px, py - 1, depth + 1, minX, maxX, minY, maxY)
  482.       End If
  483.       
  484.       If (px < MotionResolution - 1) And (py > 0) Then
  485.         Call fillRegion(px + 1, py - 1, depth + 1, minX, maxX, minY, maxY)
  486.       End If
  487.       
  488.       If (px < MotionResolution - 1) Then
  489.         Call fillRegion(px + 1, py, depth + 1, minX, maxX, minY, maxY)
  490.       End If
  491.       
  492.       If (px < MotionResolution - 1) And (py < MotionResolution - 2) Then
  493.         Call fillRegion(px + 1, py + 1, depth + 1, minX, maxX, minY, maxY)
  494.       End If
  495.       
  496.       If (py < MotionResolution - 1) Then
  497.         Call fillRegion(px, py + 1, depth + 1, minX, maxX, minY, maxY)
  498.       End If
  499.       
  500.       If (px > 0) Then
  501.         Call fillRegion(px - 1, py, depth + 1, minX, maxX, minY, maxY)
  502.       End If
  503.     
  504.       If (px > 0) And (py > 0) Then
  505.         Call fillRegion(px - 1, py - 1, depth + 1, minX, maxX, minY, maxY)
  506.       End If
  507.       
  508.       If (px > 0) And (py < MotionResolution - 1) Then
  509.         Call fillRegion(px - 1, py + 1, depth + 1, minX, maxX, minY, maxY)
  510.       End If
  511.       
  512.       If (px < MotionResolution - 1) And (py < MotionResolution - 1) Then
  513.         Call fillRegion(px + 1, py + 1, depth + 1, minX, maxX, minY, maxY)
  514.       End If
  515.       
  516.     End If
  517.   End If
  518. End Sub
  519.  
  520.