Raspberry Pi using opencv to operate cameras save pictures and videos

The basic operation of the camera using the Raspberry Pi to learn the opencv - save pictures and videos

1, Opencv to control Raspberry Pi camera to take pictures and save it to the local, mainly used cv2 and numpy library

! # / usr / bin / to python3 
# - * - Coding: UTF-. 8 - * - 


Import CV2 
Import numpy 

# initialization camera 
Camera = cv2.VideoCapture (0) 

# read image 
RET, camera.read IMG = () 
# Conversion grayscale image 
gray = cv2.cvtColor (IMG, cv2.COLOR_BGR2GRAY) 
# save images 
cv2.imwrite ( 'img.jpg', IMG) 
cv2.imwrite ( 'gray.jpg', gray) 

# released camera 
camera.release ( ) 
cv2.destroyAllWindwos ()

  

2, a video recording to a local

! # / usr / bin / to python3 
# - * - Coding: UTF-. 8 - * - 

# save a video to the local 

Import CV2 
Import numpy 

# initialization camera 
Camera = cv2.VideoCapture (0) 

# Set encoding format 
fourcc = cv2.VideoWriter_fourcc (* 'XVID') # mpeg4 encoding 
# sets the frame rate 
FPS = 24 
# set the resolution 
frameSize = (640,480) 
# set the camera's output 
OUT = cv2.VideoWriter ( 'output.avi', the fourcc, FPS, frameSize) 

the while True: 
        RET, Frame = camera.read () 
        Gray = cv2.cvtColor (Frame, cv2.COLOR_BGR2GRAY) 
        # write data to the local 
        out.write (Frame) 
        IF cv2.waitKey (. 1) == 0xFF & the ord ( 'Q'): press q to exit the loop # 
            BREAK 
# release resources 
camera.release () 
out.release ()
cv2.destroyAllWindows()

  

 

Guess you like

Origin www.cnblogs.com/lw77/p/11963705.html