- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试从相机和相关运动数据中捕捉帧。对于同步,我使用时间戳。视频和 Action 被写入文件,然后进行处理。在那个过程中,我可以计算每个视频的运动帧偏移量。
事实证明,相同时间戳的运动数据和视频数据彼此偏移了不同的时间,从 0.2 秒到 0.3 秒不等。此偏移量对于一个视频是恒定的,但因视频而异。如果每次都具有相同的偏移量,我将能够减去一些校准值,但事实并非如此。
有同步时间戳的好方法吗?也许我没有正确记录它们?有没有更好的方法让它们处于同一参照系?
CoreMotion 返回相对于系统正常运行时间的时间戳,因此我添加偏移量以获得 unix 时间:
uptimeOffset = [[NSDate date] timeIntervalSince1970] -
[NSProcessInfo processInfo].systemUptime;
CMDeviceMotionHandler blk =
^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error){
if(!error){
motionTimestamp = motion.timestamp + uptimeOffset;
...
}
};
[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical
toQueue:[NSOperationQueue currentQueue]
withHandler:blk];
为了获得高精度的帧时间戳,我使用了 AVCaptureVideoDataOutputSampleBufferDelegate。它也偏移了 unix 时间:
-(void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
CMTime frameTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
if(firstFrame)
{
firstFrameTime = CMTimeMake(frameTime.value, frameTime.timescale);
startOfRecording = [[NSDate date] timeIntervalSince1970];
}
CMTime presentationTime = CMTimeSubtract(frameTime, firstFrameTime);
float seconds = CMTimeGetSeconds(presentationTime);
frameTimestamp = seconds + startOfRecording;
...
}
最佳答案
关联这些时间戳实际上非常简单 - 虽然没有明确记录,但相机帧和运动数据时间戳均基于 mach_absolute_time()
时基。
这是一个在启动时重置的单调计时器,但重要的是,当设备处于 sleep 状态时也会停止计数。因此,没有简单的方法可以将其转换为标准的“挂钟”时间。
谢天谢地,您不需要这样做,因为时间戳可以直接比较 - motion.timestamp 以秒为单位,您可以在回调中注销 mach_absolute_time()
以查看它是否是相同的时基。我的快速测试显示,运动时间戳通常比处理程序中的 mach_absolute_time
早大约 2 毫秒,这对于将数据报告给应用程序可能需要多长时间似乎是正确的。
注意 mach_absolute_time()
是以滴答为单位的,需要转换为纳秒;在 iOS 10 及更高版本上,您可以使用等效的 clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
does the same thing .
[_motionManager
startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical
toQueue:[NSOperationQueue currentQueue]
withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
// motion.timestamp is in seconds; convert to nanoseconds
uint64_t motionTimestampNs = (uint64_t)(motion.timestamp * 1e9);
// Get conversion factors from ticks to nanoseconds
struct mach_timebase_info timebase;
mach_timebase_info(&timebase);
// mach_absolute_time in nanoseconds
uint64_t ticks = mach_absolute_time();
uint64_t machTimeNs = (ticks * timebase.numer) / timebase.denom;
int64_t difference = machTimeNs - motionTimestampNs;
NSLog(@"Motion timestamp: %llu, machTime: %llu, difference %lli", motionTimestampNs, machTimeNs, difference);
}];
对于相机,时基也是一样的:
// In practice gives the same value as the CMSampleBufferGetOutputPresentationTimeStamp
// but this is the media's "source" timestamp which feels more correct
CMTime frameTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
uint64_t frameTimestampNs = (uint64_t)(CMTimeGetSeconds(frameTime) * 1e9);
这里时间戳和调用处理程序之间的延迟有点大,通常在 10 毫秒内。
我们现在需要考虑相机帧上的时间戳的实际含义 - 这里有两个问题;有限的曝光时间和滚动快门。
滚动快门意味着并非图像的所有扫描线实际上都同时被捕获 - 首先捕获顶行,最后捕获底行。数据的这种滚动读出分布在整个帧时间内,因此在 30 FPS 相机模式下,最终扫描线的曝光开始/结束时间几乎恰好在第一条扫描线的相应开始/结束时间之后的 1/30 秒。
我的测试表明 AVFoundation 帧中的呈现时间戳是帧读出的开始 - 即第一条扫描线曝光的结束。因此,最后一条扫描线的曝光结束是在此之后的 frameDuration
秒,而第一条扫描线的曝光开始是在此之前的 exposureTime
秒。因此,帧曝光中心(图像中间扫描线曝光的中点)的时间戳可以计算为:
const double frameDuration = 1.0/30; // rolling shutter effect, depends on camera mode
const double exposure = avCaptureDevice.exposureDuration;
CMTime frameTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
double midFrameTime = CMTimeGetSeconds(frameTime) - exposure * 0.5 + frameDuration * 0.5;
在室内设置中,曝光通常以全帧时间结束,因此上面的 midFrameTime
最终与 frameTime
相同。对于您通常从明亮的室外场景中获得的短曝光,差异很明显(在极快的运动下)。
我认为您偏移的主要原因是您假设第一帧的时间戳是处理程序运行的时间 - 即它不考虑捕获数据和将其传送到您的应用程序之间的任何延迟。特别是如果您正在为这些处理程序使用主队列,我可以想象第一帧的回调会被您提到的 0.2-0.3 秒延迟。
关于iOS:同步来自相机和运动数据的帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42116314/
这是否可以检测到手机何时像图片上那样移动?这个在android中叫什么名字? 是否有处理此类事情的事件?一年前,我看到一个带指南针的应用程序,它可以实时运行。 谢谢! 最佳答案 我希望这段代码能有所帮
我正在为这个而撕扯我的头发。出于某种奇怪的原因,我找不到/想不出如何在 SFML 和/或 SDL 中移动 Sprite 。我看过的这两个库的教程对此一无所知;所以我认为它更像是 C++ 的东西而不是库
所以我最近一直在研究 DirectX11,但我对它还是很陌生。我现在正在尝试通过翻译来移动一些东西,这就是我所拥有的。我一直在阅读 Frank D Luna 关于 DirectX11 的书,他提供了一
我一直在尝试为绘图元素制作动画,但没有成功。我可以对导入的图像进行动画处理,但是当我尝试对 pygame 生成的绘图进行动画处理时,它们仍然是静态的。 编辑:“动画”是指“移动”。就像使圆在 x 和
好吧,我已经尝试 Java 几个星期了,遵循类和在线教程。我做了一个简单的游戏,其中方 block 落向屏幕底部,而玩家控制一个小球,仅在 x 轴上移动并尝试避开它们。 我遇到的问题是方 block
我的 python 代码遇到一些问题,我正在制作蛇的一个版本,我的问题涉及蛇本身的运动。我已经得到了工作正常的方向,我只需要做到这一点,以便蛇继续沿着通过按键告诉它的方向移动,我还需要使它成为一个 b
那是我的代码。 -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { [[NSNotificationC
我正在构建这个用于慢跑的 Android 应用程序,它应该测量用户从某个时间点开始的距离。 我对使用 GPS 选项(经度、纬度)不感兴趣,所以如果没有 GPS 选项并且只使用 android 传感器,
什么是自律?网上有这样一个回答:“最健康的自律是早睡,最实用的自律是运动,最丰盈内心的自律是读书。”早睡养神,运动养身,读书养脑。坚持把这三件事做好,你就已经超越了很多人。 1 每天早点睡 你有没有过
考虑下面这行 Lisp 代码: (some-function 7 8 | 9) ;; some comment. note the extra indentation 该点位于“8”和
在 Vim 中,如何移动到 xml 文件中的父/表亲标签?我正在寻找类似的东西: vatat " create a selection for second parent tag with all c
用 Dart 做这样的 Canvas 运动的最佳方法是什么? http://jsfiddle.net/loktar/dMYvG/ 我正在尝试使 Canvas 运动平稳,并想看看Dart可以做什么。 还
我试图让一个物体在固定的时间段内沿着圆形路径移动。 这个应用程序是一个“平滑运动”时钟。 因此,我不想每次 .getSeconds() 更新时将位置移动到固定坐标,而是想使用 ( .getSecond
我正在尝试创建一个简单的动画,其中一系列气泡围绕中心点旋转。我有一个动画版本,其中气泡在开始旋转之前从中心点扩散,效果很好,但是一旦我单击其中一个图像(引发动画),屏幕就会卡住一会儿,然后气泡出现在他
不久前我开始学习java作为一种爱好,因为我想制作一个小游戏。我学习了 Java 基础知识,并决定尝试解决游戏开发问题。我的 JFrame 和一切都很好,从技术上讲我没有错误,但我的小矩形家伙不会在屏
我在制作台球游戏时遇到问题,当我模拟击球时,我需要球使用react,程序是这样工作的,您单击击球的方向和力量,然后单击开始, go按钮位于创建标签的GUI类中,该按钮调用我的主类中的一个方法来接收参数
我以前在 2d 项目中使用过类似的东西来移动并且它总是有效。我现在正在使用它,它为我提供了一些输出的正确角度和错误的角度。我认为我的触发器中有一些错误,我弄错了。不过,我已经检查了大约一百万次。 Pr
我的 OpenGL 应用程序有问题,您可以在 this gif 中清楚地看到.基本上我想朝光标指向的方向移动,但这并没有发生,而是“前进”方向保持不变。 例如,如果我转身 180° 并按“w”向前走,
因此,我再次开始使用 C++ 编程并尝试使用 OpenGL。目前我有一个基本的应用程序,我只想用键移动相机。我读了很多关于这个的文章,但我仍然对运动有问题,我猜是因为即使只有一点点,但它们与我的程序不
我在 Android OpenGL 中有一个 Sprite 。这个 Sprite (一只小甲虫)总是向前移动,我使用: sprite.setPosition(posX, posY); 现在我有一个旋转
我是一名优秀的程序员,十分优秀!