# How to use Custom Stencil Mask in C++ code for 2D bounding box?

**URL:** <https://forums.unrealengine.com/t/how-to-use-custom-stencil-mask-in-c-code-for-2d-bounding-box/570564>\
**Category:** C++\
**Tags:** unreal-engine, UE5-0, CPP, question\
**Created:** [May 31, 2022, 8:02am UTC](https://forums.unrealengine.com/t/how-to-use-custom-stencil-mask-in-c-code-for-2d-bounding-box/570564 "2022-05-31T08:02:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gigoubigou](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Gigoubigou](https://forums.unrealengine.com/u/Gigoubigou)\
**Post date:** [May 31, 2022, 8:02am UTC](https://forums.unrealengine.com/t/how-to-use-custom-stencil-mask-in-c-code-for-2d-bounding-box/570564/1 "2022-05-31T08:02:34Z")

</div>

Hello Everyone,

I am currently trying to create a synthetic dataset for a Image Recognition project. I am trying to create precise Bounding Box around a drone for my dataset labels.

I was able to create a mask of my drone by following this tutorial : [How to Create Masks With the Custom Stencil Buffer | Tips and Tricks | Unreal Engine | Talks and demos](https://dev.epicgames.com/community/learning/talks-and-demos/JM/how-to-create-masks-with-the-custom-stencil-buffer-tips-and-tricks-unreal-engine)

Here is the result I obtained:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/2/3/523dbf07b74f7d5d6ffe217ac79858c5b8a7fe21.jpeg)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/c/0/1c09b9663533191e1c21da8260dc1dd2fc740add.png)

Now, I would like to create a 2D bounding box, probably by following this piece of code:

```auto
FBox2D UTryPixelAccess::calcBoundingFromBinary(UTextureRenderTarget2D * RenderTexture)
{
	TArray<FLinearColor> ImageData;
	FRenderTarget *RenderTarget = RenderTexture->GameThread_GetRenderTargetResource();
	RenderTarget->ReadLinearColorPixels(ImageData);

	FVector2D maxPoint(0, 0);
	FVector2D minPoint(RenderTexture->SizeX, RenderTexture->SizeY);

	for (int x = 0; x < RenderTexture->SizeX; x++) {
		for (int y = 0; y < RenderTexture->SizeY; y++) {
			int i = x + y * RenderTexture->SizeX;
			if (ImageData[i].R > 0.1) {
				// we a have white pixel
				if (x <= minPoint.X) {
					minPoint.X = x;
				}
				if(y <= minPoint.Y) {
					minPoint.Y = y;
				}
				if (x >= maxPoint.X) {
					maxPoint.X = x;
				}
				if (y >= maxPoint.Y) {
					maxPoint.Y = y;
				}
			}
		}
	}

	return FBox2D(minPoint, maxPoint);
}

```

from this question: [Bounding Box from specific View (SceneCapture)](https://forums.unrealengine.com/t/bounding-box-from-specific-view-scenecapture/434522/6)

However :

- I don’t know how to convert my Custom Stencil Buffer to a _UTextureRenderTarget2D_
- I would like to render my images using the Movie Render Queue and I don’t know how to render at the same time the lit scene and get the Custom Stencil Buffer to calculate by BB.

I don’t care about performances as I will do a render afterward.

Thanks for your help.  
Louis
