UE4.27 uses OpenCV plug-in to get local camera content

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. insert image description here
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.
insert image description here
Right-click the uproject file to generate a VS project file.
insert image description here

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.
insert image description here

4. Enable the plugin in the project

insert image description here

5. Create M_WebCam material.

After opening the material, right click to create Param2D.
insert image description here
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
insert image description here
.
Set the material as shown below
insert image description here

6. Create BP_Webcam blueprint

Create a blueprint based on WebcamReader.
insert image description here
After opening the blueprint, write it as shown below.
insert image description here
Pay attention to add the plane component Plane, add the Dynamic Material variable, and the variable is the dynamic instance of the material.
insert image description here
Note that the Video Texture here is the name customized in the previous section.
insert image description here

7. Drag the blueprint into the map to run

insert image description here
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.

Guess you like

Origin blog.csdn.net/Accepted_10086/article/details/126785890