CARLA
 
载入中...
搜索中...
未找到
OpticalFlowCamera.cpp
浏览该文件的文档.
1#include "Carla.h"
3#include "HAL/IConsoleManager.h"
4
6
8
9// 获取光流相机的传感器定义
11{
12 // 使用蓝图函数库创建光流相机的传感器定义
14}
15
16// 构造函数,初始化光流相机
17AOpticalFlowCamera::AOpticalFlowCamera(const FObjectInitializer &ObjectInitializer)
18 : Super(ObjectInitializer)
19{
20 // 启用 16 位图像格式
22
23 // 添加用于物理镜头畸变校正的后处理材质
25 TEXT("Material'/Carla/PostProcessingMaterials/PhysicLensDistortion.PhysicLensDistortion'"));
26
27 // 添加用于计算光流的后处理材质
29 TEXT("Material'/Carla/PostProcessingMaterials/VelocityMaterial.VelocityMaterial'"));
30}
31
32// 在物理帧结束时处理光流数据
33void AOpticalFlowCamera::PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds)
34{
35 // 获取控制光流输出的控制台变量
36 auto CVarForceOutputsVelocity = IConsoleManager::Get().FindConsoleVariable(TEXT("r.BasePassForceOutputsVelocity"));
37
38 // 保存控制台变量的原始值
39 int32 OldValue = CVarForceOutputsVelocity->GetInt();
40
41 // 设置控制台变量以强制输出速度(光流)信息
42 CVarForceOutputsVelocity->Set(1);
43
44 // 定义一个转换函数,将图像数据转换为浮点光流值
45 std::function<TArray<float>(void *, uint32)> Conversor = [](void *Data, uint32 Size)
46 {
47 TArray<float> IntermediateBuffer;
48 // 计算数据中的像素数量
49 int32 Count = Size / sizeof(FFloat16Color);
50 DEBUG_ASSERT(Count * sizeof(FFloat16Color) == Size);
51
52 // 将数据解释为 FFloat16Color 类型的数组
53 FFloat16Color *Buf = reinterpret_cast<FFloat16Color *>(Data);
54
55 // 预分配缓冲区大小(每个像素有两个值:x 和 y)
56 IntermediateBuffer.Reserve(Count * 2);
57
58 // 遍历像素数据,计算光流的 x 和 y 分量
59 for (int i = 0; i < Count; ++i)
60 {
61 float x = (Buf->R.GetFloat() - 0.5f) * 4.f; // 光流 x 分量
62 float y = (Buf->G.GetFloat() - 0.5f) * 4.f; // 光流 y 分量
63 IntermediateBuffer.Add(x);
64 IntermediateBuffer.Add(y);
65 ++Buf; // 移动到下一个像素
66 }
67 return IntermediateBuffer;
68 };
69
70 // 使用像素读取器将处理后的光流数据发送到渲染线程
71 FPixelReader::SendPixelsInRenderThread<AOpticalFlowCamera, float>(*this, true, Conversor);
72
73 // 恢复控制台变量的原始值
74 CVarForceOutputsVelocity->Set(OldValue);
75}
TSharedPtr< const FActorInfo > carla::rpc::ActorState UWorld * World
#define DEBUG_ASSERT(predicate)
Definition Debug.h:68
static FActorDefinition GetSensorDefinition()
AOpticalFlowCamera(const FObjectInitializer &ObjectInitializer)
void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds) override
void Enable16BitFormat(bool Enable=false)
bool AddPostProcessingMaterial(const FString &Path)
Load the UMaterialInstanceDynamic at the given Path and append it to the list of shaders with Weight.
static FActorDefinition MakeCameraDefinition(const FString &Id, bool bEnableModifyingPostProcessEffects=false)
创建一个相机参与者定义。