Question: Calculate the absolute difference frame between two frames as the camera is capturing frames and threshold them with a specific value e.g. 20. Save the

  1. Calculate the absolute difference frame between two frames as the camera is capturing frames and threshold them with a specific value e.g. 20. Save the output abs. diff. frame in a video file.
  2. Try for example to move your hands in front of the camera while capturing and analyzing the frames. You should be able to visually identify well the contour of your hands when these are moving but not so much when they are staying still.
  3. This part continues from our previous demo with capturing frames from the camera using
  4. load_camera.py
  5.  
  6. The load_camera.py:
  7.  
  8. # Load opencv module
  9. import cv2


  10.  
  11. # Creat a VideoCapture object
  12. cap = cv2.VideoCapture(0)

  13.  
  14. # Check if camera opened successfully
  15. if (cap.isOpened() is False):
  16. print("Unable to read camera feed")

  17.  
  18. # Default resolutions of the frame are obtained.
  19. # The default resolutions
  20. # are system dependent.
  21. # We convert the resolutions from float to integer.
  22. frame_width = int(cap.get(3))
  23. frame_height = int(cap.get(4))

  24.  
  25. # Define the codec and create VideoWriter object. The output is
  26. # stored in 'output.avi' file.
  27. out = cv2.VideoWriter('output.avi',
  28. cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'),
  29. 10, (frame_width,frame_height))

  30.  
  31. while(True):
  32. ret, frame = cap.read()

  33.  
  34. if ret:

  35.  
  36. # color to grayscale
  37. # Write the frame into the file 'output.avi'
  38. out.write(frame)
  39. print(frame.dtype)
  40. print(frame.shape)

  41.  
  42. # Display the resulting frame
  43. cv2.imshow('frame', frame)

  44.  
  45. # Press Q on keyboard to stop recording
  46. if cv2.waitKey(1) & 0xFF == ord('q'):
  47. break

  48.  
  49. # Break the loop
  50. else:
  51. break

  52.  
  53. # When everything done,
  54. # release the video capture and write objects
  55. cap.release()
  56. out.release()

  57.  
  58. # Closes all the frames
  59. cv2.destroyAllWindows()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To achieve the desired functionality of calculating the absolute difference frame between two frames ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!