【UE4】UE4.25+OpenCV环境配置

UE4.25+OpenCV环境配置


一、前言

最近实验室项目需要将外置摄像头接入UE4,因此需要将OpenCV第三方库配置到UE4内,在此做个记录,经验证在4.25和4.26上均可配置成功。


二、下载OpenCV

UE4中的OpenCV插件已经在Github上了,因此只需将其下载解压即可。

Github:https://github.com/Brandon-Wilson/OpenCV-Plugin

下载后解压目录:


三、将OpenCV库复制进项目

将解压后的Binaries、Plugins、ThirdParty文件夹直接复制到项目根目录

  


四、在VS内修改配置

1. 打开并修改项目的Build.cs文件

打开\Source\ProjectName\ProjectName.Build.cs

在using UnrealBuildTool;后添加

using System.IO;

构造函数前添加如下ThirdPartyPath函数

private string ThirdPartyPath
	{
		get { return Path.GetFullPath(Path.Combine(ModuleDirectory, "../../ThirdParty/")); }
	}

构造函数中添加

string OpenCVPath = Path.Combine(ThirdPartyPath, "OpenCV/");
PublicIncludePaths.AddRange(new string[] { Path.Combine(OpenCVPath, "Includes") });
PublicLibraryPaths.Add(Path.Combine(OpenCVPath, "Libraries/Win64"));

整体build.cs文件如下所示

// Copyright Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;
using System.IO;

public class Test : ModuleRules
{
	private string ThirdPartyPath
	{
		get { return Path.GetFullPath(Path.Combine(ModuleDirectory, "../../ThirdParty/")); }
	}

	public Test(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
	
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });

		PrivateDependencyModuleNames.AddRange(new string[] {  });

		string OpenCVPath = Path.Combine(ThirdPartyPath, "OpenCV/");
		PublicIncludePaths.AddRange(new string[] { Path.Combine(OpenCVPath, "Includes") });
		PublicLibraryPaths.Add(Path.Combine(OpenCVPath, "Libraries/Win64"));

		// Uncomment if you are using Slate UI
		// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

		// Uncomment if you are using online features
		// PrivateDependencyModuleNames.Add("OnlineSubsystem");

		// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
	}
}

2.  打开并修改OpenCV代码

打开/Plugins/OpenCV/Source/OpenCV/Private/OpenCV.cpp

#include "OpenCVPrivatePCH.h"

替换为

#include "../Public/OpenCV.h"

打开/Plugins/OpenCV/Source/OpenCV/Public/OpenCV.h

#include "ModuleManager.h" 

替换为

#include "Modules/ModuleManager.h"

打开/Plugins/OpenCV/Source/OpenCV/OpenCV.Build.cs

将第13行

public OpenCV(TargetInfo Target)

换为

public OpenCV(ReadOnlyTargetRules Target) : base(Target)

将第27行

bool isdebug = Target.Configuration == UnrealTargetConfiguration.Debug && BuildConfiguration.bDebugBuildsActuallyUseDebugCRT;

换为

bool isdebug = Target.Configuration == UnrealTargetConfiguration.Debug && Target.bDebugBuildsActuallyUseDebugCRT;

打开/Plugins/OpenCV/Source/OpenCV/Private/WebcamReader.cpp

将#include "WebcamReader.h"放到Include的第一个,否则会报错

#include "WebcamReader.h"
#include "OpenCVPrivatePCH.h"
#include "OpenCV.h"
#include "string.h"

3. 在VS项目设置中添加引用路径

右键项目->属性->VC++目录 在Include目录中添加路径:

../../ThirdParty/OpenCV/Includes

五、在UE4中打开插件

在UE4编辑器的Plugin中打开OpenCV。

打开后重启项目即可。


六、验证OpenCV配置是否成功

1. 验证WebcamReader

新建蓝图类,搜索WebcamReader,如果能成功搜索即配置成功。

2. 验证OpenCV C++

在cpp文件中创建某个函数调用OpenCV自带的Mat类,如果UE4能编译成功,则表示配置成功。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;

void SomeClass::SomeFunction()
{
	Mat mat;
}

注意!!

需要在.h头文件中添加如下代码才能编译成功

#ifdef _DEBUG  
#pragma comment(lib,"opencv_world320d.lib") 
#else
#pragma comment(lib,"opencv_world320.lib") 
#endif

七、总结

按照以上步骤进行配置后应该就可以在UE4中使用OpenCV了,可以实现读取摄像头并进行处理等多种功能。

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值