# Texture2D to cv::Mat conversion

**URL:** <https://forums.unrealengine.com/t/texture2d-to-cv-mat-conversion/1577954>\
**Category:** Programming & Scripting\
**Tags:** opencv, unreal-engine, crash, question, CPP\
**Created:** [January 1, 2024, 1:13am UTC](https://forums.unrealengine.com/t/texture2d-to-cv-mat-conversion/1577954 "2024-01-01T01:13:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![supermtf20](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@supermtf20](https://forums.unrealengine.com/u/supermtf20)\
**Post date:** [January 1, 2024, 1:13am UTC](https://forums.unrealengine.com/t/texture2d-to-cv-mat-conversion/1577954/1 "2024-01-01T01:13:32Z")

</div>

Hi everybody,

I had some C++ code that integrated OpenCV and it worked great in UE4. I tried to port that to UE5, but it keeps crashing whenever I try to initialize an OpenCV CV::Mat. Here is a tiny snippet that I use for testing and keeps crashing on me:

> FTexture2DMipMap\* MyMipMap = &MyTexture2D-\>PlatformData-\>Mips[0];  
> FByteBulkData\* RawImageData = &MyMipMap-\>BulkData;
> 
> uint8\* Pixels = static\_cast\<uint8\*\>(RawImageData-\>Lock(LOCK\_READ\_ONLY));
> 
> int32 SizeX = MyTexture2D-\>GetSizeX();  
> int32 SizeY = MyTexture2D-\>GetSizeY();  
> cv::Vec3b\* newColor = new cv::Vec3b();
> 
> cv::Mat\* imageMat = new cv::Mat(SizeY, SizeX, CV\_8UC4, Pixels);//CV\_64FC3);  
> RawImageData-\>Unlock();  
> return;

Any help is much appreciated !!

---

<div class="post-metadata">

**Author:** ![supermtf20](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@supermtf20](https://forums.unrealengine.com/u/supermtf20)\
**Post date:** [February 11, 2024, 7:05am UTC](https://forums.unrealengine.com/t/texture2d-to-cv-mat-conversion/1577954/2 "2024-02-11T07:05:29Z")

</div>

Issue solved by using the OpenCV version that came with the engine rather than downloading separately

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [March 12, 2024, 7:06am UTC](https://forums.unrealengine.com/t/texture2d-to-cv-mat-conversion/1577954/3 "2024-03-12T07:06:27Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
