❌

Reading view

There are new articles available, click to refresh the page.

Write a video using open-cv

Use open-cv VideoWriter function to write a video

Source Code

import cv2

video = cv2.VideoCapture("./data/video.mp4")
fourcc = cv2.VideoWriter.fourcc(*'FMP4')
writeVideo = cv2.VideoWriter('./data/writeVideo.mp4',fourcc,24,(1080,720))

while(video.isOpened()):
    suc, frame = video.read()
    if(suc):
        frame = cv2.resize(frame,(1080,720))
        cv2.imshow("write video",frame)
        writeVideo.write(frame)
        if(cv2.waitKey(24)&0xFF == ord('q')):
            break
    else:
        break

writeVideo.release()
video.release()
cv2.destroyAllWindows()

Video

Pre-Required Knowledge

If you know OpenCV, you can use it to open a video. If you don’t know this, visit this open video blog.

Functions

Explain Code

Import open-cv Library import cv2
Open a Video Using videoCapture Function

fourcc

The fourcc function is used to specify a video codec.
Example: AVI format codec for XVID.

VideoWriter

The videoWriter function initializes the writeVideo object. it specify video properties such as codec, FPS, and resolution.
There are four arguments:

  1. Video Path: Specifies the video write path and video name.
  2. fourcc: Specifies the video codec.
  3. FPS: Sets an FPS value.
  4. Resolution: Sets the video resolution.

The read() function is used to read a frame.

After reading a frame, resize() it.
Note: If you set a resolution in writeVideo, you must resize the frame to the same resolution.

write

This function writes a video frame by frame into the writeVideo object.

The waitKey function is used to delay the program and check key events for program exit using an if condition.

Release objects

Once the writing process is complete, release the writeVideo and video objects to finalize the video writing process.

Additional Link

github code

open-cv write image

Explore the OpenCV imwrite function used to write an image.

Source Code

import cv2

image = cv2.imread("./data/openCV_logo.jpg",cv2.IMREAD_GRAYSCALE)
image = cv2.resize(image,(600,600))
cv2.imwrite("./data/openCV_logo_grayscale.jpg",image)

Image

Function

imwrite()

Explain Code

Import the OpenCV library import cv2.

The imread function reads an image. Since I need a grayscale image, I set the flag value as cv2.IMREAD_GRAYSCALE.

Resize the image using the resize() function.

imwrite

The imwrite function is used to save an image. It takes two arguments:

  1. Image path – Set the image path and name.
  2. Image – The image as a NumPy array.

Additional Link

github code

open-cv open video

Playing a video in OpenCV is similar to opening an image, but it requires a loop to continuously read multiple frames.

Source Code

import cv2

video = cv2.VideoCapture("./data/video.mp4")

while(video.isOpened()):
    isTrue, frame = video.read()
    
    if(isTrue):
        frame = cv2.resize(frame,(800,500))
        cv2.imshow("play video",frame)
        if(cv2.waitKey(24)&0xFF == ord('q')):
            break
    else:
        break

video.release()
cv2.destroyAllWindows()

Video

Functions

Explain Program

Import OpenCV Library

import cv2

VideoCapture

This function is used to open a video by specifying a video path.

  • If you pass 0 as the argument, it opens the webcam instead.

isOpened

This function returns a boolean value to check if the video or resource is opened properly.

Use while to start a loop. with the condition isOpened().

read

This function reads a video frame by frame.

  • It returns two values:
    1. Boolean: True if the frame is read successfully.
    2. Frame Data: The actual video frame.

Use if(isTrue) to check if the data is properly read, then show the video.

  • Resize the video resolution using resize function.
  • Show the video using imshow.
  • Exit video on keypress if(cv2.waitKey(24)&0xFF == ord('q')).
    • Press β€˜qβ€˜ to break the video play loop.
Why Use &0xFF ?
  • This ensures the if condition runs correctly.
  • waitKey returns a key value, then performs an AND operation with 0xFF (which is 255 in hexadecimal).
  • If any number is used in an AND operation with 0xFF, it returns the same number.
    Example: 113 & 0xFF = 113 (same value as the first operand).

ord

The ord function returns the ASCII value of a character.

  • Example: ord('q') returns 113.

Finally, the if condition is validated.
If true, break the video play. Otherwise, continue playing.

release

This function releases the used resources.

destroyAllWindows() closes all windows and cleans up used memory.

Additional Link

github code

open-cv open image

What is OpenCV

OpenCV stands for Open Source Computer Vision. It is a library used for computer vision and machine learning tasks. It provides many functions to process images and videos.

Computer Vision

Computer vision is the process of extracting information from images or videos. For example, it can be used for object detection, face recognition, and more.

Source Code

import cv2

image = cv2.imread("./data/openCV_logo.jpg",cv2.IMREAD_COLOR)
image = cv2.resize(image,(600,600))
cv2.imshow("window title",image)

cv2.waitKey(0)

cv2.destroyAllWindows()

Image

OpenCV Functions

imread

This function is used to read an image and returns it as a NumPy array. It requires two arguments:

  1. Image path: The location of the image file.
  2. Read flag: Specifies the mode in which the image should be read. Common flags are:
    • Color Image
    • Grayscale Image
    • Image with Alpha Channel

resize

This function resizes an image. It requires two arguments:

  1. Image array: The NumPy array of the image.
  2. Resolution: A tuple specifying the new width and height.

imshow

This function displays an image. It takes two arguments:

  1. Window name: A string representing the window title.
  2. Image array: The image to be displayed.

waitKey

This function adds a delay to the program and listens for keypress events.

  • If the value is 0, the program waits indefinitely until a key is pressed.
  • If a key is pressed, it releases the program and returns the ASCII value of the pressed key.
  • Example: Pressing q returns 113.

destroyAllWindows

This function closes all open image windows and properly cleans up used resources.

Additional Link

Github code

❌