After many attempts to browse the blogs of many bigwigs, I finally successfully ran the OpenCV plug-in in UE4.27 and obtained the local camera screen. Thank you very much for your help.
1. Create UE4.27 project
Select C++ project when creating.
After the creation is complete, close the project and automatically open VS.
2. Download OpenCV
OpenCV Baidu network disk link:
Link: https://pan.baidu.com/s/1HkrmQHhaP__CMpBoYFZnsA?pwd=vzmm
Extraction code:
After decompressing vzmm, put the three folders into the root directory of the created project.
Right-click the uproject file to generate a VS project file.
3. Modify the OpenCV code
After opening the sln file, modify the content of ProjectName.Build.cs.
Add lines 2, 6-9, 25-27.
using UnrealBuildTool;
using System.IO;
public class TestWebcam : ModuleRules
{
private string ThirdPartyPath
{
get{
return Path.GetFullPath(Path.Combine(ModuleDirectory,"../../ThirdParty"));}
}
public TestWebcam(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] {
"Core", "CoreUObject", "Engine", "InputCore","OpenCV"});
PrivateDependencyModuleNames.AddRange(new string[] {
});
// 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
string OpenCVPath = Path.Combine(ThirdPartyPath, "OpenCV/");
PublicIncludePaths.AddRange(new string[] {
Path.Combine(OpenCVPath, "Includes") });
PublicLibraryPaths.Add(Path.Combine(OpenCVPath, "Libraries/Win64"));
}
}
After modification, right-click the project project and click "Generate", and the compilation is successful.
4. Enable the plugin in the project
5. Create M_WebCam material.
After opening the material, right click to create Param2D.
Change the name to Video Texture. Note that this name is used later in the blueprint. The previous bloggers may have made a mistake here, which is inconsistent with the one used in the blueprint. I have wondered for a long time how a variable connects RGB to the self-illuminating color
.
Set the material as shown below
6. Create BP_Webcam blueprint
Create a blueprint based on WebcamReader.
After opening the blueprint, write it as shown below.
Pay attention to add the plane component Plane, add the Dynamic Material variable, and the variable is the dynamic instance of the material.
Note that the Video Texture here is the name customized in the previous section.
7. Drag the blueprint into the map to run
I have also studied the screenshots for a long time. Currently, I use a camera in front of the plane to cover the entire screen and use the "automatic screenshot of the game process" to capture the full screen. I will write it again when I have time.