OpenCV (Open Source Computer Vision Library) is a collection of BSD-licensed video / image processing libraries. There are many algorithms for image filtering, template matching, object recognition, video analysis, machine learning, and more.
Example of motion tracking using OpenCV (OpenCV Google Summer of Code 2015) https://www.youtube.com/watch?v=OUbUFn71S4s
Click here for installation and easy usage http://qiita.com/olympic2020/items/d5d475a446ec9c73261e
Click here for still image filtering Try edge detection with OpenCV
This time, I will convert the video in real time.
The following is done in the loop of the while statement.
movie.py
# -*- coding: utf-8 -*-
import cv2
#Constant definition
ESC_KEY = 27 #Esc key
INTERVAL= 33 #Waiting time
FRAME_RATE = 30 # fps
ORG_WINDOW_NAME = "org"
GRAY_WINDOW_NAME = "gray"
ORG_FILE_NAME = "org_768x576.avi"
GRAY_FILE_NAME = "gray_768x576.avi"
#Read original video file
org = cv2.VideoCapture(ORG_FILE_NAME)
#Preparing the saved video file
end_flag, c_frame = org.read()
height, width, channels = c_frame.shape
rec = cv2.VideoWriter(GRAY_FILE_NAME, \
cv2.VideoWriter_fourcc(*'XVID'), \
FRAME_RATE, \
(width, height), \
False)
#Window preparation
cv2.namedWindow(ORG_WINDOW_NAME)
cv2.namedWindow(GRAY_WINDOW_NAME)
#Conversion processing loop
while end_flag == True:
#Grayscale conversion
g_frame = cv2.cvtColor(c_frame, cv2.COLOR_BGR2GRAY)
#Frame display
cv2.imshow(ORG_WINDOW_NAME, c_frame)
cv2.imshow(GRAY_WINDOW_NAME, g_frame)
#Frame writing
rec.write(g_frame)
#Exit with Esc key
key = cv2.waitKey(INTERVAL)
if key == ESC_KEY:
break
#Read next frame
end_flag, c_frame = org.read()
#End processing
cv2.destroyAllWindows()
org.release()
rec.release()
Click here for a sample video. (Download) Rename "768x576.avi" to "org_768x576.avi" and place it in the same directory as the script and execute it.
The color video and the grayscale video were displayed at the same time, and the grayscale video was also saved in the file.
OpenCV3.1 does not have cv2.cv.CV_FOURCC
, which existed until OpenCV2.4.
There is also a method to deal with it with your own method as shown below, so I will introduce it.
CV_FOURCC.py
# cv2.cv.CV_FOURCC
def cv_fourcc(c1, c2, c3, c4):
return (ord(c1) & 255) + ((ord(c2) & 255) << 8) + \
((ord(c3) & 255) << 16) + ((ord(c4) & 255) << 24)
If you only need to operate with the codec decided in the program, you can also define it like this in the constant definition.
opencv3.1.py
XVID = 0x44495658
Here is a summary of frequently used codecs.
Codec | String | constant |
---|---|---|
MPEG-1 | PIM1 | 0x314D4950 |
MPEG-4 | MPG4 | 0x3447504D |
MPEG-4.2 | MP42 | 0x3234504D |
MPEG-4.3 | DIV3 | 0x33564944 |
DivX | DIVX | 0x58564944 |
DivX 5.0 | DX50 | 0x30355844 |
Xvid | XVID | 0x44495658 |
H.263 | U263 | 0x33363255 |
H.263I | I263 | 0x33363249 |
H.264 | H264 | 0x34363248 |
FLV1 | FLV1 | 0x31564C46 |
Motion JPEG | MJPG | 0x47504A4D |
Uncompressed | zero(0) | 0x00000000 |
Click here for a list of codecs (link)
Next, let's convert webcam / camcorder videos in real time with OpenCV. Try converting webcam / camcorder videos in real time with OpenCV
Recommended Posts