Background
I needed to annotate to create machine learning teacher data.
For that reason, a large amount of still images were needed, so I decided to cut out still images from the video.
At first, I used to do it manually using a tool that extracts still images from videos. The task was tedious, so I decided to automate it.
Procedure
install openCV
Loading video
Cut out a still image for each unit frame
Readjust the still image size
install openCV This time I did it with windows + anaconda, so at the anaconda prompt
Users\user\conda install -c conda-forge opencv
I was able to install it with the command.
Basically, it can be done fully automatically, but it took about 2 hours.
A little Python description is required to actually make it work.
<br> Code <br>
A Laplacian filter is a spatial filter that extracts image contours using quadratic differentiation.
laplacian.var looks at the variance of the Laplace distribution.
if ret and laplacian.var() >= laplacian_thr:
laplacian_thr is a threshold for judging as a blurred image
I decided to use this value by acquiring the parameters of the image that I recognized as being out of focus.
This time, I decided to judge the image with the value of laplaciaan.var () of 800 or less as a blurred image.
```python
"""
@author: S.Nakamura 2020/10/18
A program that reads a video and divides it into images (jpg)
input_movie: Movie file name to read
cpf: How many frames to save
image_dir: Image output destination directory (create if not available)
・ Loading video
・ How many frames to read
-Specify the directory to output the image
・ Remove the out-of-focus image
・ Resize
・ Determine a name and output
"""
import cv2
import numpy as np
import os
i=0
count = 0
cpf = 10 #How many frames to cut out
#Image size
image_width =Width of image to resize
image_heigh =The height of the image to be resized
#Laplacian for judging blurred images.var
laplacian_thr = 800 #Threshold when making a blurred image judgment
#Loading video
cap = cv2.VideoCapture('Specify the video file to read')
while(cap.isOpened()):
ret, frame = cap.read() #Load video
#assert frame, "Failed to open" #For debugging
if ret == False:
print('Finished') #When the video cutout is finished
break
if count%cpf == 0: #How many frames to cut out once
#Reduce the size
resize_frame = cv2.resize(frame,(image_width,image_heigh))
#Check if the image is blurred
laplacian = cv2.Laplacian(resize_frame, cv2.CV_64F)
if ret and laplacian.var() >= laplacian_thr: #Output only those whose defocus judgment is above the threshold
#File name of the first argument image, second argument The image you want to save
write = cv2.imwrite('Name and extension of the image to save') #Display the cropped image
assert write, "Failed to save"
print('Save',Name and extension of the image to save) #Confirmation display
i += 1
count = count + 1
cap.release()
I haven't verified it in detail, but I was able to achieve the purpose of cutting out the image and resizing it. I am impressed to be able to cut out a still image from a video with such a simple description.
Reference information
Blur detection with OpenCV https://www.pyimagesearch.com/2015/09/07/blur-detection-with-opencv/
note.nkmk.me https://note.nkmk.me/python-opencv-video-to-still-image/
A program that cuts out images (jpg) from videos (mp4) https://www.souichi.club/deep-learning/spliter/
Recommended Posts