首页 > 解决方案 > 使用 scikit-image 进行点跟踪未按预期工作

问题描述

我正在尝试使用 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

我究竟做错了什么?

标签: pythonpython-3.xscikit-imageimage-stabilization

解决方案


推荐阅读