Skip to main content

Saving a Video OpenCV Python

Saving a Video

In this lesson, we will learn how to record video in OpenCV. This process is very simple for images. Only the cv2.imwrite () function is used. Recording videos is a bit more difficult. Let's create a VideoWriter object for this process. We must determine the name of the file to be saved. Then we have to specify the FourCC code. 

Then the number of frames per second and frame size must be selected.

import cv2
capture = cv2.VideoCapture(
0)
# Create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(
'recorded.avi',fourcc, 20.0, (640,480))
while(capture.isOpened()):
 ret
, frame = capture.read()
 
if ret==True:
  frame = cv2.flip(frame
,0)
  out.write(frame)
  cv2.imshow(
'frame',frame)
 
if cv2.waitKey(1) & 0xFF == ord('e'):
  
break
  else
:
  
break
capture.release()
out.release()
cv2.destroyAllWindows()

Comments

Popular posts from this blog

OpenCV Crop an image in Python

 Crop operation in OpenCV is very simple. You should first read the image. Then the values x, y, h, w must be determined to specify the region to be cropped. You can do this using the code below. import cv2 image = cv2.imread( 'image.jpg' ) y= 10 x= 20 h= 200 w= 250 crop = image[y:y+h , x:x+w] cv2.imshow( 'Image' , crop) cv2.waitKey( 0 )

OpenCV Drawing a Line

How To Draw a Line In OpenCV Python? To draw a line, we need to select the starting and ending coordinates of the line. First, let's create a black image and draw a green line. We need to use the code below to create a black image: image = np.zeros(( 360 , 360 , 3 ) , np.uint8) Now we can create our line. cv2.line(image , ( 0 , 0 ) , ( 511 , 511 ) , ( 0 , 255 , 0 ) , 5 ) The code for creating a line is as follows: import numpy as np import cv2 image = np.zeros(( 360 , 360 , 3 ) , np.uint8) cv2.line(image , ( 0 , 0 ) , ( 511 , 511 ) , ( 0 , 255 , 0 ) , 5 ) cv2.imshow( "image" , image) cv2.waitKey( 0 ) cv2.destroyAllWindows() Output: Another Example import numpy as np import cv2 image = np.zeros(( 360 , 360 , 3 ) , np.uint8) cv2.line(image , ( 0 , 180 ) , ( 360 , 180 ) , ( 0 , 255 , 0 ) , 5 ) cv2.imshow( "image" , image) cv2.waitKey( 0 ) cv2.destroyAllWindows() Output:

Opencv Image Properties - rows, columns and channels, type of image data, number of pixels

Opencv Image Properties  OpenCV allows access to image properties. These properties are rows, columns and channels, type of image data, number of pixels. The img.shape command is used to access the shape of the image. Returns rows, columns and channels. import  cv2 img = cv2.imread( 'flower.jpg' ) print (img.shape) Result: (400, 400, 3) The data type of the image is obtained with img.dtype import cv2 img = cv2.imread( 'flower.jpg' ) print (img.dtype) Result: uint8