Python code for video characterization

Python20 lines of code for video characterization
We often see some character ghost and animal videos on station B, mainly to convert a video into characters. It seems to be very high-end, but the actual implementation is indeed very simple. We only need to touch the opencv module to quickly realize video characterization. But before that, let's take a look at the effect we achieved:

 

The above is a part of the effect picture taken, and the following begins to enter our theme.

1. OpenCV installation and image reading
In Python, we only need to use pip to install, we execute the following statement in the console:

pip install opencv-python
1
can be used after installation is complete. We first read an image:

import cv2
im = cv2.imread ('jljt') # Read picture
cv2.imshow ('im', im) # Show picture
cv2.waitKey (0) # Wait for keyboard input
cv2.destroyAllWindows () # Destroy memory
 
first we use The cv2.imread method reads the picture, which returns an ndarray object. Then call the imshow method to display the image, a window will appear after the call, because this window will only appear for a moment, so we call waitKey to wait for input, incoming 0 means infinite wait. Because opencv is written in C ++, we need to destroy the memory.

Second, some basic operations in OpenCV
The idea of ​​characterizing our video is to convert the video into a frame-by-frame image, then characterize the image, and finally show the effect of character video. Before we generate character drawings, we also have to look at some OpenCV operations.

(1) Grayscale conversion
Grayscale processing is a very common operation. Our original picture has three layers of BGR (in OpenCV, the image is read in the form of BGR). Our gray-scale processing intuitively looks like turning the picture into black and white, but essentially it turns the three layers of the picture into one layer through calculation. And this calculation does not require us to do, we only need to call the function in OpenCV:

import cv2 
# Read the picture 
im = cv2.imread ( ' jljt.jpg ' ) 
# Grayscale conversion 
grey = cv2.cvtColor (frame, cv2.COLOR_RGB2GRAY)

 

 

 


————————————————
Copyright Statement: This article is an original article of CSDN blogger "ZackSock", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprint .
Original link: https://blog.csdn.net/ZackSock/article/details/105453311

Guess you like

Origin www.cnblogs.com/LQZ888/p/12691820.html