OpenCV Plugin - VideoCapture with MJPG Format outputs only noise

When using the OpenCV plugin for Unreal Engine and setting the video codec for the webcam to MJPG, the image recieved from opencv looks like gibberish:

When doing the same thing with just c++, outside of unreal engine, with the locally installed OpenCV, it works fine. Also when not setting the fourcc code, which defaults to YUYV, it also works fine in unreal engine.

Includes in the header file:

#include "PreOpenCVHeaders.h"
#include <ThirdParty/OpenCV/include/opencv2/core.hpp>
#include <ThirdParty/OpenCV/include/opencv2/videoio.hpp>
#include <ThirdParty/OpenCV/include/opencv2/imgproc.hpp>
#include "PostOpenCVHeaders.h"

VideoCapture is initialized in the BeginPlay function:

void ACamera::BeginPlay()
{
	Super::BeginPlay();

	cap.open(0);

	cap.set(cv::CAP_PROP_FRAME_WIDTH, 1280);
	cap.set(cv::CAP_PROP_FRAME_HEIGHT, 720);
	cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M','J','P','G'));

	size = cv::Size(cap.get(cv::CAP_PROP_FRAME_WIDTH), cap.get(cv::CAP_PROP_FRAME_HEIGHT));
	
	texture = UTexture2D::CreateTransient(size.width, size.height, PF_B8G8R8A8);
	texture->MipGenSettings = TMGS_NoMipmaps;
}

The frame is getted from the webcam, converted to bgra and then written into the Unreal Engine texture.

void ACamera::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	cv::Mat frame;
	cap >> frame;

	cvtColor(frame, frame, cv::COLOR_BGR2BGRA);

	if (frame.size().area() > 0)
	{
		void* texture_data = texture->GetPlatformData()->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
		memcpy(texture_data, frame.data, frame.elemSize() * frame.size().area());
		texture->GetPlatformData()->Mips[0].BulkData.Unlock();
		texture->UpdateResource();
	}
}
1 Like

Hi did this work out for you eventually ?

Thanks

1 Like