OpenCV in Unreal Engine 5

Hi,
I have been using OpenCV within Unreal Engine C++ scripts following this integration method. Today I downloaded UE5 and sadly it is not working.

I used following C++ actor for testing:
TestActor.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "opencv2/core.hpp"
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include "Log.h"
#include "Misc/Paths.h"
#include "TestActor.generated.h"

UCLASS()
class ATestActor : public AActor
{
	GENERATED_BODY()

public:
	void TestOpenCV();

public:
	ATestActor();
	virtual void BeginPlay() override;

};

TestActor.cpp

#include "TestActor.h"

ATestActor::ATestActor(){

	//TestOpenCV();

}

void ATestActor::TestOpenCV(){
	FString RelativePath = FPaths::ProjectDir();
	FString FullPath = IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*RelativePath);
	std::string path(TCHAR_TO_UTF8(*FullPath));
	UE_LOG(LogEndoVRCore, Log, TEXT("Testing OpenCV..."));
	cv::Mat img = cv::imread(path+"ThirdParty/Data/Lenna.png", cv::IMREAD_COLOR);
	cv::imshow("Display window", img);
	cv::waitKey(0); // Wait for a keystroke in the window
}

void ATestActor::BeginPlay(){
	Super::BeginPlay();
	TestOpenCV();
}

UE5 is running UE_LOG(LogEndoVRCore, Log, TEXT(“Testing OpenCV…”)); but then it is stuck.

There is already on OpenCV plugin distributed with the engine (plus one linked to the Composer). Did you check it out? Regarding your integration, I would start with placing a breakpoint after the UE_LOG and then stepping in this see what happens. Is path properly formed? Does it return a valid img but gets stuck afterwards?

Hi, Can you share this any link regarding this opencv plugin ?

Late response here

Yes, on your Build.cs you need to include: OpenCV, OpenCVHelper, then to include the headers you need to use this:

#if WITH_OPENCV
#include "PreOpenCVHeaders.h"

#include "opencv2/calib3d.hpp"
#include "opencv2/imgproc.hpp"

#include "PostOpenCVHeaders.h"
#endif

Finally make sure the OpenCV plugin is enabled, either on your project or add the dependency on the .uplugin file

3 Likes

Don’t think is late, thanks :wink:

1 Like

My guy, where is documentation on ANY of this?

That is a funny one, historically UE does not have real documentation since UE1, the documentation is reading the source code. on visual studio try to look for key words and start reading, I strongly suggest to use raider, since its search engine is far powerful than visual studio

1 Like

Yeah, I guess the question was rhetorical. Was just hoping for a miracle. Chat GPT probably knows.

1 Like