如何在随后的两个视频帧中跟踪车轮轮辐?

2024-05-20 21:38:17 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑视频的两个后续帧,如下所示: fig1

fig2

跟踪其中一根辐条运动的可能方法是什么?你知道吗

我问这个是因为我没有任何视频处理的经验。所以任何建议都是有用的!我在追踪什么?从我所读到的,通常我必须首先检测到我想要跟踪的对象。我使用了一个角点检测算法,比如goodfeaturestotrack。但如何确保检测到正确的轮辐等?你知道吗

一些附加信息:视频的帧速率是30fps。轮子只能顺时针转动。当我一帧一帧地点击视频时,很明显一个轮辐的移动不会超过两个轮辐之间角度的一半(从一帧到另一帧)。 另外:车轮半径为5厘米。你知道吗

我现在已经试过马克的答案了。我将Tmax和帧的时间戳记录在一个txt文件中,然后编写以下代码来计算相应的速度:

ListOfVelocities = []
for idx in range(1,len(ListOfAngles)):
    if ListOfAngles[idx] < ListOfAngles[idx-1]:
        rotation = (360-ListOfAngles[idx]) + ListOfAngles[idx-1]
    else: 
        rotation = ListOfAngles[idx] - ListOfAngles[idx-1]
    timePassed = VideoTimestamp[idx]-VideoTimestamp[idx-1]
    velocity = 2*np.pi/360 * rotation * RADIUS * timePassed
    ListOfVelocities.append(velocity)

Tags: 对象方法算法视频经验建议idxrotation
3条回答

我并不认为这是一个跟踪问题,因为轮子是受约束的,所以它不能在整个帧中移动,它只能改变它的角度位置,所以你只需要知道它的某个部分在一帧中的位置,以及它在下一帧中旋转了多少。然后,如你所知的帧速率,即帧间时间,你可以计算速度。你知道吗

所以,问题是如何判断哪个是你在前一帧中测量的同一辐条。由于辐条后面的区域是黑暗的,你会想要一个光辐条对比度好,所以你可以很容易地找到它。所以,我会把四个辐条涂成黑色,然后你只需要在深色背景上找一个浅色的辐条。我也会考虑把车轮的中心漆成红色(或其他饱和的颜色),这样你就可以很容易地找到中间。你知道吗

现在,在开始处理时,通过寻找红色找到轮子的中心,并在图像中得到它的x,y坐标。现在以像素为单位选择一个半径,你可以稍后更改,然后计算出一个x,y坐标的列表,比如说在以红点为圆心的圆的周长上360个点(1/degree)。这些点和所有的正弦/余弦在整个处理过程中都不会改变,所以在主视频处理循环之外这样做。你知道吗

现在,在每一帧中,使用索引来获取360个点中每个点的亮度,并且至少在最初,将最亮的一个点作为轮辐。你知道吗

所以,我在你的图像上粗略地画了一下,中心是红色的,只有一个轮辐是白色的:

enter image description here

现在代码如下所示:

#!/usr/bin/env python3

import math
import numpy as np
from PIL import Image

# Open image and make Numpy version of it too
im = Image.open('wheel.png')
imnp = np.array(im)

# Find centre by looking for red pixels
# See https://stackoverflow.com/a/52183666/2836621
x, y = 193, 168

# Set up list of 360 points on a circle centred on red dot outside main processing loop
radius = 60
# List of X values and Y values on circumference
Xs = []
Ys = []
for theta in range(360):
    thetaRad = math.radians(theta)
    dx = int(radius * math.sin(thetaRad))
    dy = int(radius * math.cos(thetaRad))
    Xs.append(x+dx)
    Ys.append(y+dy)

# Your main loop processing frames starts here

# Make greyscale Numpy version of image
grey = np.array(im.convert('L'))

sum  = 0
Bmax = 0
Tmax = 0
for theta in range(360):
    brightness=grey[Ys[theta],Xs[theta]]
    sum += brightness
    if brightness > Bmax:
        Bmax = brightness
        Tmax = theta
    print(f"theta: {theta}: brightness={brightness}")

# Calculate mean
Mgrey = sum/len(Xs)
print(f"Mean brightness on circumf: {Mgrey}")

# Print peak brightness and matching theta
print(f"Peak brightness: {Bmax} at theta: {Tmax}")

输出如下:

theta: 0: brightness=38
theta: 5: brightness=38
theta: 10: brightness=38
theta: 15: brightness=38
theta: 20: brightness=38
theta: 25: brightness=38
theta: 30: brightness=38
theta: 35: brightness=45
theta: 40: brightness=38
theta: 45: brightness=33
theta: 50: brightness=30
theta: 55: brightness=28
theta: 60: brightness=28
theta: 65: brightness=31
theta: 70: brightness=70
theta: 75: brightness=111
theta: 80: brightness=130
theta: 85: brightness=136
theta: 90: brightness=139    < - peak brightness at 90 degrees to vertical as per picture - thankfully!
theta: 95: brightness=122
theta: 100: brightness=82
theta: 105: brightness=56
theta: 110: brightness=54
theta: 115: brightness=49
theta: 120: brightness=43
theta: 125: brightness=38
theta: 130: brightness=38
theta: 135: brightness=38
theta: 140: brightness=38
theta: 145: brightness=38
theta: 150: brightness=38
theta: 155: brightness=38
theta: 160: brightness=38
theta: 165: brightness=38
theta: 170: brightness=38
theta: 175: brightness=38
theta: 180: brightness=31
theta: 185: brightness=33
theta: 190: brightness=38
theta: 195: brightness=48
theta: 200: brightness=57
theta: 205: brightness=38
theta: 210: brightness=38
theta: 215: brightness=38
theta: 220: brightness=38
theta: 225: brightness=38
theta: 230: brightness=38
theta: 235: brightness=38
theta: 240: brightness=38
theta: 245: brightness=38
theta: 250: brightness=52
theta: 255: brightness=47
theta: 260: brightness=36
theta: 265: brightness=35
theta: 270: brightness=32
theta: 275: brightness=32
theta: 280: brightness=29
theta: 285: brightness=38
theta: 290: brightness=38
theta: 295: brightness=38
theta: 300: brightness=38
theta: 305: brightness=38
theta: 310: brightness=38
theta: 315: brightness=38
theta: 320: brightness=39
theta: 325: brightness=40
theta: 330: brightness=42
theta: 335: brightness=42
theta: 340: brightness=40
theta: 345: brightness=36
theta: 350: brightness=35
theta: 355: brightness=38
Mean brightness on circumf: 45.87222222222222
Peak brightness: 142 at theta: 89

如果在下一帧中,峰值亮度现在与垂直方向成100度角,则您知道轮子在1/(每秒帧数)内旋转了10度。你知道吗

你可能需要改变半径以获得最佳效果-实验!图像上显示的白色半径与代码中的60像素半径相对应。你知道吗

与其求峰值亮度,不如求圆周上360像素亮度的平均值和标准偏差,然后取角度作为亮度大于平均值以上标准偏差数的角度的平均值。这取决于你需要的分辨率/精确度。你知道吗

您还可以将θ索引的圆周围的所有亮度收集到单个360元素数组中,如下所示:

brightnessByTheta = grey[Ys[:],Xs[:]]

你会得到:

array([ 38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,
        38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,
        38,  38,  38,  38,  38,  43,  49,  47,  46,  45,  44,  43,  43,
        40,  38,  36,  34,  33,  33,  33,  32,  31,  31,  29,  30,  28,
        29,  29,  29,  28,  28,  27,  29,  28,  28,  27,  28,  28,  29,
        31,  36,  42,  51,  60,  70,  81,  89,  98, 105, 111, 117, 122,
       126, 128, 130, 131, 132, 133, 135, 136, 138, 139, 141, 142, 139,
       136, 133, 129, 124, 122, 119, 113, 104,  93,  82,  72,  65,  60,
        59,  56,  56,  55,  55,  54,  54,  53,  52,  52,  50,  49,  47,
        46,  45,  44,  43,  42,  40,  39,  38,  38,  37,  38,  38,  37,
        38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,
        38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,
        38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,
        38,  38,  38,  38,  38,  38,  38,  38,  34,  31,  31,  31,  31,
        31,  31,  32,  33,  34,  35,  36,  37,  38,  42,  43,  44,  45,
        48,  49,  50,  51,  55,  57,  60,  64,  65,  38,  38,  38,  38,
        38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,
        38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,
        38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,
        38,  38,  38,  52,  56,  46,  46,  47,  47,  38,  39,  40,  40,
        36,  36,  36,  36,  36,  35,  35,  34,  34,  34,  32,  33,  33,
        33,  33,  32,  32,  31,  30,  29,  29,  28,  38,  38,  38,  38,
        38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,
        38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,
        38,  38,  38,  38,  38,  38,  40,  40,  39,  38,  39,  39,  39,
        40,  40,  41,  41,  42,  42,  42,  41,  41,  42,  42,  41,  40,
        39,  40,  40,  38,  39,  38,  37,  36,  36,  35,  34,  33,  35,
        38,  38,  38,  38,  38,  38,  38,  38,  38], dtype=uint8)

卢克,马苏德解释的是你在轮子上做了标记。这可能是一个小的白色贴纸或一滴油漆。如果你是转换到灰度然后白色将是最好的选择,我。如果是我,我会找到车轮使用霍夫圆和阈值。一旦你有了轮子,创建一个遮罩来移除背景。只剩下轮子了。一旦你让轮子找到最亮的点(应该是白色的贴纸,油漆滴或任何使用的东西),记录它的位置,最好是中心和每个帧做同样的过程,测量位置的变化,并用它来计算角动量。你知道吗

道格

对于提供的帧,不可能跟踪单个辐条,因为所有辐条都具有相同的形状和颜色。跟踪的实际方法是通过物理标记辐条。然后,只要相机有移动,就需要image registration来对齐帧。事后追踪轮辐并不困难。你知道吗

编辑:物理标记可以是轮辐上的色点(为简单起见,请使用图像中唯一的颜色)。然后使用thresholding技术来挑选颜色。你可能需要一些增强来消除噪音。你知道吗

相关问题 更多 >