본문 바로가기
Lecture/OpenCV Master with Python (Beginner)

OpenCV + Python Processing images, videos, webcams, RTSP and RTMP streams

by codingwalks.en 2024. 10. 23.
728x90
반응형

Hello. This is codingwalks.

This article explains how to process images, videos, webcams, and streaming URLs such as RTSP/RTMP using OpenCV. It covers everything from basic code for reading image files and displaying them on the screen, to processing video files frame by frame, and displaying real-time video using a webcam. It also provides a table summarizing how to process real-time streaming using streaming protocols such as RTSP and RTMP, and how to control various properties of the cv2.VideoCapture object using the set(...) function. Through this, you can learn the basic techniques for processing image and video data with OpenCV.

 

1. Reading and Saving Images

First, I will explain how to read an image file, display it on the screen, and save it. You can read an image using the cv2.imread function of OpenCV, and read the image in Unchanged mode including Color (default), Grayscale, or alpha channel depending on the flag. You can display it on the screen using the cv2.imshow function, and you can save the image using the cv2.imwrite function.

import cv2

# load image
img = cv2.imread('resources/Lena.png', cv2.IMREAD_COLOR)

# show image
cv2.imshow('Output', img)

# save image
cv2.imwrite('output_image.png', img)

# keep screen on (0 means infinite wait)
cv2.waitKey(0)

# close the window
cv2.destroyAllWindows()
- imread: Loads an image from a file path.
- imshow: Displays an image with a window name.
- waitKey: Waits for a key input, 0 means infinite waiting.

 

2. Read and save video files

This is how to read a video file, output it one frame at a time, and save it. Use the cv2.VideoCapture object to load the video file, and use the while loop to process the frames. To save the video file, use the cv2.VideoWriter object, and you can specify the format and codec of the video file to be saved. The video file is saved frame by frame.

import cv2

# load video
cap = cv2.VideoCapture('resources/TownCentreXVID.mp4')

# video save settings
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output_video.avi', fourcc, 20.0, (640, 480))

while True:
    # reading the frame
    success, img = cap.read()
    
    # check if the frame was read successfully
    if not success:
        break

    # show frame
    cv2.imshow('Video', img)
    
    # save frame
    out.write(img)

    # end loop when 'q' key is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# release the video object and close the window
cap.release()
out.release()
cv2.destroyAllWindows()
- VideoCapture: An object that loads and reads video files.
- VideoWriter: An object that saves video files. (You can specify the file name, codec, FPS, and image size.)
- VideoWriter_fourcc: An object that sets the codec.
- read: Reads frames one by one from a video.
- write: Saves frames one by one to a video.
- waitKey(1): Displays the next frame after waiting 1ms.

 

3. Using and Saving a Webcam

The way to use a webcam is almost the same as how to handle video files. Instead of a file path, use cv2.VideoCapture by specifying the camera ID. Normally, the camera ID is 0, and you can also set the resolution.

import cv2

# load webcam (ID 0)
cap = cv2.VideoCapture(0)

# video save settings
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('webcam_output.avi', fourcc, 20.0, (640, 480))

# set resolution
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)  # width
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)  # height

while True:
    # read frame
    success, img = cap.read()

    # show frame
    cv2.imshow('Webcam', img)
    
    # save frame
    out.write(img)

	# end loop when 'q' key is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# disable webcam and video and close window
cap.release()
out.release()
cv2.destroyAllWindows()
- set(cv2.CAP_PROP_FRAME_WIDTH, 640) : Set the width to 640.
- set(cv2.CAP_PROP_FRAME_HEIGHT, 480) : Set the height to 480.

 

Brightness Control

To set the brightness of the webcam, use set(cv2.CAP_PROP_BRIGHTNESS, "value"). For example, to set the brightness to 100, you can write the code as follows:

cap.set(cv2.CAP_PROP_BRIGHTNESS, 100)  # Set brightness

 

4. Use RTSP/RTMP Streaming

RTSP and RTMP are protocols for real-time video streaming. To handle this streaming in OpenCV, you can pass the streaming URL to cv2.VideoCapture() to get the data in real time. RTSP is usually used in IP cameras, and RTMP is mainly used in streaming services such as live broadcasting. If you don't have an IP camera, you can check the link below to see how to enable RTSP transmission using a webcam.

 

How to broadcast RTSP with a webcam and configure a stream server with MediaMTX

Hello. This is codingwalks.When learning computer vision or image processing, there are times when real-time video streaming is required. In particular, RTSP (Real-Time Streaming Protocol) is used for various purposes such as surveillance cameras, real-tim

en.codingwalks.com

import cv2

# load RTSP streams
rtsp_url = 'rtsp://username:password@ip_address:port/stream'
cap = cv2.VideoCapture(rtsp_url)

# video save settings
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('rtsp_output.avi', fourcc, 20.0, (640, 480))

while True:
    success, frame = cap.read()
    if not success:
        break

    # streaming output
    cv2.imshow('Streaming', frame)

    # save frame
    out.write(frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# end stream save
cap.release()
out.release()
cv2.destroyAllWindows()
- rtsp: Video streaming protocol.
- username: User name of the RTSP camera.
- password: Password of the RTSP camera.
- ip_address: IP address of the RTSP camera.
- port: Port of the RTSP camera (usually 554).
- stream: Stream path of the RTSP camera.

 

In this article, we have looked at how to process and save images, video files, webcams, and RTSP/RTMP streams using OpenCV. We have explained how to read image and video files, display them on the screen, and save them as files if necessary, with simple code examples. We have also shown that the same method can be applied to process and save real-time webcam images, and we have also covered real-time video processing using RTSP and RTMP streaming URLs.

Finally, we have provided a table summarizing how to control various properties of the cv2.VideoCapture object using the cap.set() function, and we can apply the necessary settings to process images more efficiently. Through all of this, we can establish the basics of image processing using OpenCV, and based on this, we can apply it to various applications.

Real-time and accuracy are important in image processing. We hope that this article will help you freely process image data, save it if necessary, and further deepen image processing.

 

If you found this post useful, please like and subscribe below. ^^

Buy me a coffee

 

[Codingwalks]에게 송금하기 - AQR

[Codingwalks]에게 송금하기 - AQR

aq.gy

★ All contents are referenced from the link below. ★

 

OpenCV: OpenCV-Python Tutorials

Core Operations In this section you will learn basic operations on image like pixel editing, geometric transformations, code optimization, some mathematical tools etc.

docs.opencv.org

728x90
반응형