UE5_OpenCV库的加载方式

UE5使用opencv库要在系统中添加opencv的环境变量

UE5_OpenCV库的加载方式_第1张图片

在项目文件夹下新建ThirdParty,将OpenCV中的bin、opencv文件夹copy到ThirdParty中

UE5_OpenCV库的加载方式_第2张图片

打开项目,找到source目录下的build.cs文件

UE5_OpenCV库的加载方式_第3张图片

修改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的方法

// Fill out your copyright notice in the Description page of Project Settings.


#include "userwidgetClass.h"

#include 
#include 
#include 
#include 

#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);
}

你可能感兴趣的:(ue5,opencv,人工智能)