UE5使用opencv库要在系统中添加opencv的环境变量
在项目文件夹下新建ThirdParty,将OpenCV中的bin、opencv文件夹copy到ThirdParty中
打开项目,找到source目录下的build.cs文件
修改build.cs内容,添加头文件路径,dll路径,lib路径
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
using System.IO;
public class newopencv : ModuleRules
{
private string ModulePath
{
get
{
return ModuleDirectory;
}
}
private string ThirPartyPath
{
get
{
return Path.GetFullPath(Path.Combine(ModulePath, "../../ThirdParty/"));
}
}
public bool loadopencv(ReadOnlyTargetRules Target)
{
string OpenCVPath = Path.Combine(ThirPartyPath, "OpenCV");
string libpath = "";
if (Target.Platform == UnrealTargetPlatform.Win64)
{
PublicIncludePaths.AddRange(new string[]{Path.Combine(OpenCVPath,"include")});
libpath = Path.Combine(OpenCVPath, "lib");
PublicSystemLibraryPaths.Add(libpath);
PublicAdditionalLibraries.Add("opencv_world480.lib");
RuntimeDependencies.Add(Path.Combine("$(BinaryOutputDir)","opencv_world320.dll"),Path.Combine(libpath,"opencv_videoio_ffmpeg480_64.dll"));
RuntimeDependencies.Add(Path.Combine("$(BinaryOutputDir)","opencv_ffmpeg320_64.dll"),Path.Combine(libpath,"opencv_world480.dll"));
return true;
}
return false;
}
public newopencv(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore","RenderCore","RHI" });
//PrivateDependencyModuleNames.AddRange(new string[] { "OpenVDB" });
//PublicDependencyModuleNames.Add("UMG");
loadopencv(Target);
// 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
}
}
OpenCV库加载完成,以下是opencv读取外部png设置给ui的方法文章来源:https://uudwc.com/A/Pm3gO
// Fill out your copyright notice in the Description page of Project Settings.
#include "userwidgetClass.h"
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core/mat.hpp>
#include "Windows/WindowsTextInputMethodSystem.h"
void UuserwidgetClass::NativeConstruct()
{
Super::NativeConstruct();
openCV_RW_png();
}
void UuserwidgetClass::openCV_RW_png()
{
cv::Mat img;
img = cv::imread("E:\\mlao.PNG");
// 确保图像是BGR格式
if (img.channels() == 3)
{
cv::cvtColor(img, img, cv::COLOR_BGR2RGBA);
}
// 确保图像是8位无符号整数类型
if (img.depth() != CV_8U)
{
img.convertTo(img, CV_8U);
}
UTexture2D* texture = UTexture2D::CreateTransient(img.cols,img.rows,PF_R8G8B8A8);
void* texturedata = texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(texturedata,img.data,img.total()*img.elemSize());
texture->PlatformData->Mips[0].BulkData.Unlock();
texture->UpdateResource();
FSlateBrush Brush;
Brush.SetResourceObject(texture);
Image_abc->SetBrush(Brush);
}
文章来源地址https://uudwc.com/A/Pm3gO