用起了ue4.22 却发现所有教程都是4.20以下的,内心无比崩溃, opencv连官方都是4.17的文章 , plugin也是4.20以下的,按照教程完全没法编译成功!爬文n篇终于有所斩获,希望能让喜欢下载新版本的人不再走弯路.
1 .下载win64版本opencv3.4.0 如果不需要cuda 和一些类库是不需要编译的.编译的目的是安装更多插件.
2 . 在ue4里面 编辑/插件/新插件 名字就叫OpenCV,为啥起这个名字?方便抄别人代码的原则起的
创建完成后如下图*(其实不截图也行,截图显得高大上一些 ,而且我猜你手机一定看不全^-^)
3 . 创建目录
在你的插件里面创建目录 你的项目目录
/plugins/OpenCV/ThirdParty/OpenCV/Win64/Lib
/plugins/OpenCV/ThirdParty/OpenCV/Win64/Include
/plugins/OpenCV/ThirdParty/OpenCV/Win64/Bin
创建好后大约是这个样子
需要win32版本吧win64复制一分,现在这年月需要linux或者安卓的几率更大一些^-^
4.开始复制文件,从你下载解压的opencv3.4文件夹
down-opencv\build\x64\vc14 ( vs2017对应的版本,高版本要复制vc15下的)
down-opencv\build\x64\vc14\bin--->上图Bin目录
down-opencv\build\x64\vc14\lib--->上图Lib目录
down-opencv\build\include--->上图Include目录
down-opencv\build\x64\vc14\lib + bin--->项目目录\Plugins\OpenCV\Binaries\Win64
5 好了,复制完毕,得吧include加到编译路径里面,要不然找不到.
6 好了,改代码 plugins/OpenCV/source/OpenCV/OpenCV.Build.cs加几行代码.最后是这样子的:
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.using UnrealBuildTool;using System.IO;public class OpenCV : ModuleRules{ private string ThirdPartyPath { get { return Path.GetFullPath(Path.Combine(ModuleDirectory, "../../ThirdParty")); } } public OpenCV(ReadOnlyTargetRules Target) : base(Target) { PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; PublicIncludePaths.AddRange( new string[] { // ... add public include paths required here ... } ); PrivateIncludePaths.AddRange( new string[] { // ... add other private include paths required here ... } ); PublicDependencyModuleNames.AddRange( new string[] { "Core", // ... add other public dependencies that you statically link with here ... } ); PrivateDependencyModuleNames.AddRange( new string[] { "CoreUObject", "Engine", "Slate", "SlateCore", // ... add private dependencies that you statically link with here ... } ); DynamicallyLoadedModuleNames.AddRange( new string[] { // ... add any modules that your module loads dynamically here ... } ); string OpenCVPath = Path.Combine(ThirdPartyPath, "OpenCV"); if (Target.Platform == UnrealTargetPlatform.Win64)//64位平台 { string OpenCVIncludePath = Path.Combine(OpenCVPath, "Win64", "Include"); string OpenCVLibPath = Path.Combine(OpenCVPath, "Win64", "Lib"); PublicIncludePaths.Add(OpenCVIncludePath); PublicLibraryPaths.Add(OpenCVLibPath); PublicAdditionalLibraries.Add("opencv_world340.lib"); PublicAdditionalLibraries.Add("opencv_world340d.lib"); } else if (Target.Platform == UnrealTargetPlatform.Win32)//32位平台 { //32位的OpenCV库,仿照上面Win64的自己填写 } //else if (Target.Platform == UnrealTargetPlatform.Mac) //{ // //Mac //} }}
简书没法粘贴代码?如果你在用chrome 右键就能复制带格式的了
7 OpenCV.uplugin文件的"Type": "Runtime",看下是不是默认的runtime,是的话不用管了
8 改下 你的项目名.Build.cs 第11行,加上 OpenCV
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" ,"OpenCV"});
9 马上最后一个地方了,
'[你的项目目录]\ThirdParty\OpenCV\Includes\opencv2\core\ utility.hpp
~51 to ~55 和 ~852 行改成下面的样子(这是apple平台的)
# warning报错的,注释掉或者改下#pragma message("WARNING: You need to implement DEPRECATED for this compiler")
//#if defined(check)
//# warning Detected Apple 'check' macro definition, it can cause build conflicts. Please, include this header before any Apple headers.
//#endif...
//bool check() const;
好了,创建一个AActor的继承类,.h加几行试试
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
没报错的话就可以 愉快的cv::Mat了,