Pendulum Worksheet - PDF

Document Sample
Pendulum Worksheet - PDF Powered By Docstoc
					                      Pendulum Worksheet

                             January 22, 2010

    In this worksheet, we will be finishing our development of a program to cal-
culate the period of a pendulum using video. We have finished our background
subtraction and now need a way of figuring out whether the pendulum is in a
specified area or not. The simplest way to measure the period of a pendulum is
to pick the lowest point in its swing and mark down the times when it passes
this point. (Note that the pendulum will pass this point twice during one cycle
so what we really measure is the half-period). With our background subtraction
scheme, this is as simple as asking whether a set of pixles is white or black.

Measuring and Timing the Pendulum
  1. To access the pixel information of an image, we need to point to the
     information and de-refrence it. For this, there are two elements of data in
     the IplImage structure that we can use.

        • The first is imageData. This points to the first pixel in an image.
        • The second is widthStep. This contains information on how many
          columns there are in the image. Remember that a pixel can have
          anywhere between 1 and 4 elements so that the number of columns
          in an image is not always equal to the image width in pixels.
        • To get the information we first create a pointer.
          uchar* ptr = (uchar*)(img->imageData + y*img->widthStep)
          This creates a pointer to the head of row y.
        • Finally, we can access the information we want by de-refrencing the
          value we want
          where x is the pixel column we want, n is the number of elements per
          pixel, and m is the element of the pixel we want.
        • Add code to your previous pendulum file that will find out whether
          a pixel is black or white to see if the pendulum is there or not.

  2. We need a way to measure the time between two instances of the pendulum
     passing through our point of interest.

     • With a video file, there are a number of ways to figure out the timing
       cvGetCaptureProperty( capture, property )
       Where capture is the name of the video capture and property is
       one of the video properties:
          –   CV_CAP_PROP_POS_MSEC position in the video in ms.
          –   CV_CAP_PROP_POS_FRAME current frame of the video.
          –   CV_CAP_PROP_FPS frames per second.
          –   CV_CAP_PROP_FRAME_COUNT total number of frames in the video.
     • Use one of these to measure the times between successive measure-
       ments of the pendulum and print the time to the screen.

3. With a live video feed, many of these values are not accessible. We there-
   fore need a way to do “live timing”. C++ provides standard libraries
   “time.h” and “sys/time.h” to do this. I leave this as an excercise to you
   because we will not use a live feed and there are many C++ tutorials that
   cover this on the web.
4. When writing a program, it is always a good idea to make it as transparent
   as possible. For example, instead of specifying a specific file name in the
   code, it is better to require the user to input a name in the command line.
   In our case, we use a pre-determined picture of the background for the
   background subtraction.

     • Re-write the program so that you can save one of the initial frames of
       the video and use it as the background image. This is especially useful
       for live video, as the background will most likely change between
       successive runs of the program.
     • Can you think of a way to make sure that the pixel we select for
       measurements is always in the path of the pendulum?