Make blur videos look like fixed-point cameras with Python and OpenCV

Former story

A story about camera shake video corrected like a fixed-point camera OpenCV | Digital Creative Super Software Tokyo

I found the above site when I searched because I wanted to correct the fixed point camera that was shaken by the wind. However, I couldn't find the sample code, so I wrote it in Python.

What I used

code

I don't handle any errors. Also, I think it's okay because it worked, so the usage of the essential function may be wrong.

findTransformECC_test.py


# -*- coding: utf-8 -*-
import cv2
import numpy as np

#FourCC designation
#fourcc = "DIB "
fourcc = "XVID"

#Conversion method specification (?)
#warp_type = cv2.MOTION_AFFINE
#warp_type = cv2.MOTION_EUCLIDEAN
#warp_type = cv2.MOTION_TRANSLATION
warp_type = cv2.MOTION_HOMOGRAPHY

#Output image vertically and horizontally 1/2 sizes (area 1)/4) set
scale = 2

#Set WarpMatrix and conversion function
if warp_type == cv2.MOTION_HOMOGRAPHY:
    warp = np.eye(3,3,dtype=np.float32)
    warpTransform = cv2.warpPerspective
else:
    warp = np.eye(2,3,dtype=np.float32)
    warpTransform = cv2.warpAffine

#Input video
cap = cv2.VideoCapture(r'./test.mp4')

#Get capture information and create output information
fps = cap.get(cv2.CAP_PROP_FPS)
size = cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
size = tuple(map(lambda x: int(x / scale), size))

#Get the reference image
ret, base = cap.read()
base = cv2.resize(base, size)
base = cv2.cvtColor(base, cv2.COLOR_BGR2GRAY)

#Output destination
video1 = cv2.VideoWriter("./out1.avi", cv2.VideoWriter_fourcc(*fourcc), fps, size)

while True:

    #Get frame
    ret, frame = cap.read()
    if not ret:
        break
    frame = cv2.resize(frame, size)
    tmp = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.findTransformECC(tmp, base, warp, warp_type)

    #The output is the original image (color image)
    out = warpTransform(frame, warp, size)

    #Display and output to AVI
    cv2.imshow("", out)
    video1.write(out)

    k = cv2.waitKey(1)
    if k in [27, ord('q')]:
        #Finish by pressing ESC or Q key
        break

#release
cap.release()
video1.release()
cv2.destroyAllWindows()

** Addition ** I changed wrap_type to warp_type and put it in the argument of findTransfomrECC. ** Addendum to here **

Where it fits

ʻImport cv2.cv cannot be used (?), So it fits when replacing the part of the net sample source using the cvpackage, such ascv2.cv.CreateMat` for warpMatrix. ..

warpMatrix Error code

warpMatrix must be single-channel floating-point matrix in function cv::findTransformECC


 It was refreshing (I always thought that I specified the float!), But it was easy to specify the `dtype`.

 FourCC
 If this is also the net, I could not use it only by the method specified by `cv2.cv.CV_FOURCC`.
 What if Vim's complementary plugin NeoComplete didn't tell me `VideoWriter_fourcc` ...
 I found [here](http://stackoverflow.com/questions/15584608/python-opencv2-cv2-cv-fourcc-not-working-with-videowriterb) later.

# What I'm still worried about
 ――I feel that the relationship between the first and second arguments of `findTransformECC` is the opposite.
 + But is it correct considering the argument of `warpAffine`?

# Additional ingenuity
 Although the above source is resized, it covers the entire image, so if a moving object appears large in the video, it may not be corrected properly.

 So, select the part of the background that hardly moves the image passed to the 1st and 2nd arguments of `findTransformECC`, and crop (trim) it like` base [: 300,:] `,` tmp [: 300,:] `. ?) Then, it improved dramatically if I used the original image only when converting with `warpAffine`,` warpPerspective`.

 If you use a cropped output video, you will not be able to see the missing parts on the outside, which will make you feel good.

 After that, if you leave it alone for a day, it may be a little more resistant to time changes if you base it on a converted version of the previous frame instead of the first frame.


Recommended Posts

Make blur videos look like fixed-point cameras with Python and OpenCV
Shining life with Python and OpenCV
Neural network with OpenCV 3 and Python 3
How to make a surveillance camera (Security Camera) with Opencv and Python
Make a simple OMR (mark sheet reader) with Python and OpenCV
Capturing images with Pupil, python and OpenCV
Fractal to make and play with Python
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
Hello World and face detection with OpenCV 4.3 + Python
Automatically search and download YouTube videos with Python
Make OpenCV3 available from python3 installed with pyenv
Feature matching with OpenCV 3 and Python 3 (A-KAZE, KNN)
[# 1] Make Minecraft with Python. ~ Preliminary research and design ~
Binarization with OpenCV / Python
[# 2] Make Minecraft with Python. ~ Model drawing and player implementation ~
Let's make a simple game with Python 3 and iPhone
Create miscellaneous Photoshop videos with Python + OpenCV ④ Deal with issues
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Create miscellaneous Photoshop videos with Python + OpenCV ② Create still image Photoshop
[Python] Webcam frame size and FPS settings with OpenCV
Make ordinary tweets fleet-like with AWS Lambda and Python
Reading, displaying and speeding up gifs with python [OpenCV]
Associate Python Enum with a function and make it Callable
Analyze stocks with python and look for favorable trading phases
Convert video to black and white with ffmpeg + python + opencv
I tried to make GUI tic-tac-toe with Python and Tkinter
Programming with Python and Tkinter
Encryption and decryption with Python
"Apple processing" with OpenCV3 + Python3
Python and hardware-Using RS232C with Python-
Image editing with python OpenCV
Camera capture with Python + OpenCV
[Python] Using OpenCV with Python (Basic)
Make a fortune with Python
Make Opencv available in Python
Face detection with Python + OpenCV
Using OpenCV with Python @Mac
Works with Python and R
Automatic image interpolation with OpenCV and Python (Fast Marching Method, Navier-Stokes)
Create a striped illusion with gamma correction for Python3 and openCV3
Make a cat detector with Google Colabratory (Part 2) [Python] ~ Use OpenCV ~
Draw a watercolor illusion with edge detection in Python3 and openCV3
I tried to make a periodical process with Selenium and Python
Make a scraping app with Python + Django + AWS and change jobs
Throw something to Kinesis with python and make sure it's in
[Python] Try to recognize characters from images with OpenCV and pyocr
I tried to make an image similarity function with Python + OpenCV
Communicate with FX-5204PS with Python and PyUSB
Make Echolalia LINEbot with Python + heroku
Install Python 2.7.9 and Python 3.4.x with pip.
[Python] Using OpenCV with Python (Image Filtering)
AM modulation and demodulation with python
Make apache log csv with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
[Python] Using OpenCV with Python (Image transformation)
Let's make a GUI with python.
[Python] Using OpenCV with Python (Edge Detection)
Scraping with Python and Beautiful Soup
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python