oyouareatubeo

mouse-events-opencv

Feb 12th, 2012
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3.  
  4.  
  5. """
  6. 201202120259am
  7.  
  8. This is an example of how to gather data from various mouse events (in this
  9. case, (x,y) coordinates), and use this data to control parameters for various
  10. opencv functions.
  11.  
  12. <-*.JMJ.*->  
  13. """
  14.  
  15.  
  16. import cv
  17.  
  18.  
  19.  
  20. # Define mouse events.
  21. #_____________________________________________________________________________
  22.  
  23. def onMove(event, x, y, flags, output):
  24.     font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0, 1)
  25.     cv.PutText(output, ("%s--%s" % (x,y)), (50,50), font, cv.RGB(x, y, x))
  26.     cv.ShowImage('face_camera', output)
  27.     flags = cv.CV_EVENT_FLAG_LBUTTON
  28.  
  29.  
  30. # Generate the viewing window and designate the video source.
  31. #_____________________________________________________________________________
  32.  
  33. def create_window(name,width,height):
  34.     cv.NamedWindow("face_camera", 1)
  35.     stream = cv.CreateCameraCapture(0)
  36.     cv.SetCaptureProperty(stream, cv.CV_CAP_PROP_FRAME_HEIGHT, height)
  37.     cv.SetCaptureProperty(stream,cv. CV_CAP_PROP_FRAME_WIDTH, width)
  38.     return stream
  39.  
  40.  
  41. # Stream the video, already dangit!
  42. #_____________________________________________________________________________
  43.  
  44. def get_webcam_stream(width,height):
  45.  
  46.  
  47.     stream = create_window('face_camera',width,height)
  48.     # Live feed loop.
  49.     while True:
  50.         output = cv.QueryFrame(stream)
  51.         cv.SetMouseCallback("face_camera", onMove, output)
  52.         cv.ShowImage("face_camera", output)
  53.  
  54.  
  55.         k = cv.WaitKey(1);
  56.         if k == 'f':
  57.             break
  58.  
  59.  
  60. # ::: : - - ---        <-*.execute.*.->                          Â²Â¸Â¹Â°Â°Â­Â²Â­Â¹Â·Â³Â²Â±
  61. #_____________________________________________________________________________
  62.  
  63. def main():
  64.  
  65.     get_webcam_stream(400,300)
  66.  
  67. if __name__ == "__main__":
  68.     main()
Advertisement
Add Comment
Please, Sign In to add comment