我正在尝试使用python和通过skimage进行模板匹配来进行视频稳定。该代码应该在整个视频中跟踪单个点,但是跟踪非常不精确,我怀疑它甚至无法正常工作
这是 track_point 函数,应该将视频作为输入和点的一些坐标,然后返回跟踪点的数组每帧
from skimage.feature import match_template
from skimage.color import rgb2gray
def track_point(video,x,y,patch_size = 4,search_size = 40):
length,height,width,_ = video.shape
frame = rgb2gray(np.squeeze(video[1,:,:])) # convert image to grayscale
x1 = int(max(1,x - patch_size / 2))
y1 = int(max(1,y - patch_size / 2))
x2 = int(min(width,x + patch_size / 2 - 1))
y2 = int(min(height,y + patch_size / 2 - 1))
template = frame[y1:y2,x1:x2] # cut the reference patch (template) from the first frame
track_x = [x]
track_y = [y]
#plt.imshow(template)
half = int(search_size/2)
for i in range(1,length):
prev_x = int(track_x[i-1])
prev_y = int(track_y[i-1])
frame = rgb2gray(np.squeeze(video[i,:])) # Extract current frame and convert it grayscale
image = frame[prev_x-half:prev_x+half,prev_y-half:prev_y+half] # Cut-out a region of search_size x search_size from 'frame' with the center in the point's previous position (i-1)
result = match_template(image,template,pad_input=False,mode='constant',constant_values=0) # Compare the region to template using match_template
ij = np.unravel_index(np.argmax(result),result.shape) # Select best match (maximum) and determine its position. Update x and y and append new x,y values to track_x,track_y
x,y = ij[::-1]
x += x1
y += y1
track_x.append(x)
track_y.append(y)
return track_x,track_y
这是该功能的实现
points = track_point(video,point[0],point[1])
# Draw trajectory on top of the first frame from video
image = np.squeeze(video[1,:])
figure = plt.figure()
plt.gca().imshow(image)
plt.gca().plot(points[0],points[1])
我希望剧情能够以某种方式正常播放,因为视频并不那么摇摇欲坠,但事实并非如此。
由于某种原因,图形绘制了搜索模板的几乎所有坐标。
编辑:这是视频的链接:https://upload-video.net/a11073n9Y11-noau
我在做什么错了?