Reputation: 11721
Emgu.CV (Nuget package 2.4.2) doesn't as far as I can tell implement the gpu::alphaComp method available in OpenCV.
As such when trying to implement this specific type of composite it is incredible slow in C#, such that it takes up some 80% of the total cpu usage of my app.
This was my original solution that performs really badly.
static public Image<Bgra, Byte> Overlay( Image<Bgra, Byte> image1, Image<Bgra, Byte> image2 )
{
Image<Bgra, Byte> result = image1.Copy();
Image<Bgra, Byte> src = image2;
Image<Bgra, Byte> dst = image1;
int rows = result.Rows;
int cols = result.Cols;
for (int y = 0; y < rows; ++y)
{
for (int x = 0; x < cols; ++x)
{
// http://en.wikipedia.org/wiki/Alpha_compositing
double srcA = 1.0/255 * src.Data[y, x, 3];
double dstA = 1.0/255 * dst.Data[y, x, 3];
double outA = (srcA + (dstA - dstA * srcA));
result.Data[y, x, 0] = (Byte)(((src.Data[y, x, 0] * srcA) + (dst.Data[y, x, 0] * (1 - srcA))) / outA); // Blue
result.Data[y, x, 1] = (Byte)(((src.Data[y, x, 1] * srcA) + (dst.Data[y, x, 1] * (1 - srcA))) / outA); // Green
result.Data[y, x, 2] = (Byte)(((src.Data[y, x, 2] * srcA) + (dst.Data[y, x, 2] * (1 - srcA))) / outA); // Red
result.Data[y, x, 3] = (Byte)(outA*255);
}
}
return result;
}
Is there a way to optimise the above in C#?
I've additionally looked at using OpencvSharp, but that doesn't appear to provide access to gpu::alphaComp neither.
Is there any OpenCV C# wrapper library that can do alpha Compositing?
AddWeighted does not do what I need it to do.
Whilst similar, this question doesn't provide an answer
Upvotes: 1
Views: 2403
Reputation: 11721
So so simple.
public static Image<Bgra, Byte> Overlay(Image<Bgra, Byte> target, Image<Bgra, Byte> overlay)
{
Bitmap bmp = target.Bitmap;
Graphics gra = Graphics.FromImage(bmp);
gra.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
gra.DrawImage(overlay.Bitmap, new Point(0, 0));
return target;
}
Upvotes: 5