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.
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 **
ʻImport cv2.cv cannot be used (?), So it fits when replacing the part of the net sample source using the
cvpackage, such as
cv2.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