# open the video
cap = ocv.cvCreateFileCapture(invideofile)
# extract the number of frames
max_frames = ocv.cvGetCaptureProperty(cap,ocv.CV_CAP_PROP_FRAME_COUNT)
# interrogate the first frame to get the size
frame = ocv.cvQueryFrame(cap)
frame_size = ocv.cvGetSize(frame)

working =       ocv.cvCreateImage(frame_size,ocv.IPL_DEPTH_32F,3)
accumulator =   ocv.cvCreateImage(frame_size,ocv.IPL_DEPTH_32F,3)
background =    ocv.cvCreateImage(frame_size,ocv.IPL_DEPTH_32F,3)
ones =		ocv.cvCreateImage(frame_size,ocv.IPL_DEPTH_32F,3)
target =	ocv.cvCloneImage(frame) 

# Set divisor image all to one
ocv.cvSet(ones,ocv.cvScalar(1.0,1.0,1.0,0))

if show:
  ocv.cvNamedWindow ('Track', ocv.CV_WINDOW_AUTOSIZE)
  ocv.cvMoveWindow ('Track', 100, 100)

t = 1
n = 1

while True:
  # check to see if we should stop
  if max_frames:
    if t > max_frames:
      break
  # capture the next frame
  frame = ocv.cvQueryFrame(cap)
Exemple #2
0
  # Convert frame to grayscale
  ocv.cvConvertImage(frame,gray)

  # Convert grayscale to float
  ocv.cvConvert(gray,grayfloat)

  # Smooth - CRITICAL to remove codec artifacts
  ocv.cvSmooth(grayfloat,grayfloat,ocv.CV_GAUSSIAN,3,3,0.5)
  #ocv.cvSmooth(gray,gray,ocv.CV_MEDIAN)

  # Subtract current frame from background model
  ocv.cvAbsDiff(grayfloat, background, grayfloat);

  # Normalise image
  ocv.cvSet(ones,ocv.cvScalar(1.0,0,0,0),0) 
  minval = ocv.cvMinMaxLoc(grayfloat,True,False)
  maxval = ocv.cvMinMaxLoc(grayfloat,False,True)
  #print min,max
  ocv.cvSubS(grayfloat,ocv.cvScalar(minval,0,0,0),grayfloat,0)
  if ((maxval-minval)!=0): #Stop divide by zero
    ocv.cvDiv(grayfloat,ones,grayfloat,1.0/(maxval-minval))
  else:
    cvZero(grayfloat)
  ocv.cvScale(grayfloat,grayfloat,255.0) # Scale back to 0-255

  # Convert back to int 
  ocv.cvConvert(grayfloat,gray)
  #ocv.cvMerge(gray,gray,gray,0,output) # Show on output

  #Find the "lightest" things in the image