Define the motions and the change in those motions that you wish to detect. Trying to detect all motions is a difficult and long process and produces unwanted results. For example, you may want to ignore motions of small objects that move just a few pixels per webcam frame, such as a bug). Instead, you may want to look for changes in motion of large forms that move hundreds or thousands of pixels per frame, like people do.
Incorporate an algorithm that compares the pixels of the present webcam frame to the previous webcam frame. Don't use an algorithm that compares the colors of all the pixels in the present frame to the last frame. For detecting motions created from large forms moving, incorporate a frame-to-frame algorithm that compares just the pixels on the center diagonal line of the present frame with the last frame. This will result in less computation time required for the comparison and lower memory storage requirements and will reduce the probability that motions from small objects will trigger a motion detection event.
Include in your motion detection algorithm computer code if a motion detection event is from a lighting change rather than a motion change. When lights are turned on or off, pixel by pixel comparison of the present and last webcam frames will indicate that the frames are different. Preventing this false motion trigger event will require that you write the motion detection code such that it will detect a uniform change in the brightness of all the pixels in the webcam frames.
Protect yourself from the unexpected. When your motion detection code is complete, move different objects (of varying size) in front of the webcam. With your face in front of the webcam, move your head slightly, wink your eye and turn up your lip slightly. Examine the colors of the frame pixel map and determine how many pixels change color, the location of the change and the magnitude of the color change.
Code your motion detector to ignore specific types of motions (as indicated by the location, number and magnitude of specific pixel color change) or detect only specific types of motion changes. Vary the position and intensity of the lighting sources in the area of your webcam. Check if lighting conditions affect the ability of your webcam to detect motions from different sized forms.