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
Click here for processing video files Try converting videos in real time with OpenCV
This time, I will try to process videos that have input in real time, such as webcams and video cameras.
Smoothing is a filter used to determine the area of characters and the area of moving objects to be tracked. As an example, smoothing is performed in the following flow to determine the area.
OpenCV supports the following smoothing:
--Gaussian smoothing: cv2.GaussianBlur --Smoothing with averaging filter: cv2.blur --Smoothing using median: cv2.medianBlur --Smoothing using bilateral filter: cv2.bilateralFilter
This time, I will create a program that performs Gaussian smoothing on video input in real time, displays windows, and saves files.
sebcam.py
import cv2
# 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 __name__ == '__main__':
#Constant definition
ESC_KEY = 27 #Esc key
INTERVAL= 33 #Waiting time
FRAME_RATE = 30 # fps
ORG_WINDOW_NAME = "org"
GAUSSIAN_WINDOW_NAME = "gaussian"
GAUSSIAN_FILE_NAME = "gaussian.avi"
DEVICE_ID = 0
#Camera image acquisition
cap = cv2.VideoCapture(DEVICE_ID)
#Preparing the saved video file
end_flag, c_frame = cap.read()
height, width, channels = c_frame.shape
rec = cv2.VideoWriter(GAUSSIAN_FILE_NAME, \
cv_fourcc('X', 'V', 'I', 'D'), \
FRAME_RATE, \
(width, height), \
True)
#Window preparation
cv2.namedWindow(ORG_WINDOW_NAME)
cv2.namedWindow(GAUSSIAN_WINDOW_NAME)
#Conversion processing loop
while end_flag == True:
#Gaussian smoothing
g_frame = cv2.GaussianBlur(c_frame, (15, 15), 10)
#Frame display
cv2.imshow(ORG_WINDOW_NAME, c_frame)
cv2.imshow(GAUSSIAN_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 = cap.read()
#End processing
cv2.destroyAllWindows()
cap.release()
rec.release()
The operation of the script was confirmed in the following environment.
Video files and camera input can be treated in much the same way in a program. When handling input from the camera, it is OK if you specify the device ID in the place of the file name.
cv2.VideoCapture(DEVICE_ID)
If you have only one camera, specify DEVICE_ID = 0
.
Execution environment --Laptop I bought 5 years ago
Shooting target --When you roll the pumpkin on the floor
Shooting method I took a picture twice --Normal shooting (without filtering) --Recording while smoothing Gaussian with OpenCV
** No filtering **
** With Gaussian smoothing processing **
Click here to watch the video.
--No filtering Simply record with the software attached to the PC (link) --Gaussian smoothing process Process and save with OpenCV while shooting with camera (link)
"It's an old laptop, can Python withstand OpenCV in terms of performance?" I tried to run it with half confidence, but it worked without problems (^^) v
Recommended Posts