我有两个位图图像。 其中包含一张用 USB 相机拍摄的照片。 另一个将包含一个形状,例如矩形,但它也可以是梯形,或者可以说是只有一种颜色的随机形状。 图像的其余部分现在是白色的。
两个图像的大小不同,但缩放算法并不是这里最困难的部分,因此我们假设它们的尺寸完全相同。
我想在 USB 相机图片上显示我的形状。 出于组合目的,白色部分将被视为透明。 现在我正在考虑按像素编辑图像,但我正在寻找一个可以为我完成此操作的 API。
因此,如果我拍摄一张中间有一座房子的照片并覆盖一个红色矩形,则生成的图像将具有原始图片,房子周围有一个红色矩形。
如果这有帮助的话,我正在使用 .NET。 如果 win32 API 包含一些有用的功能,我也可以使用它。
编辑 :
我接受了这个答案,因为它让我走上了正轨。 这实际上非常容易做到。
Bitmap^ overlay_image = gcnew Bitmap("overlay.bmp");
Bitmap^ original_image = gcnew Bitmap("original.bmp");
overlay_image->MakeTransparent(Color::White);
Graphics^ g_original = Graphics::FromImage(original_image);
g_original->DrawImage(overlay_image, 0, 0);
瞧,original_image
现在上面有一个红色矩形。 实际上对于我的 30fps USB 相机来说它已经足够快了,所以我可以实时获取它。
目前还没有进行缩放。 另外,它假设覆盖图像背景为白色,这将变得透明。
I have two bitmap images. One contains a picture taken with a usb camera. The other will contain a shape, like a rectagle, but it can also be a trapezoid, or lets say, a random shape with only one color in it. The rest of the image is white right now.
The two images aren't of the same size but scaling algorithms aren't the most difficult part here, so lets assume they are of the exact same dimensions.
I want to show my shape on the usb camera picture. The white part will be considered as transparent for the combinaison purpose. Right now I'm thinking of editing the image pixels by pixels, but I'm searching for an API which will do it for me.
So if I take a picture with a house in the middle of it and overlay a red rectagle, the resulting image will have the original picture with a red rectangle around the house.
I'm using .NET if this can help. I could also use win32 API if it contains some useful functions.
Edit :
I accepted the answer since it put me on the right track. This is actually super easy to do.
Bitmap^ overlay_image = gcnew Bitmap("overlay.bmp");
Bitmap^ original_image = gcnew Bitmap("original.bmp");
overlay_image->MakeTransparent(Color::White);
Graphics^ g_original = Graphics::FromImage(original_image);
g_original->DrawImage(overlay_image, 0, 0);
Voilà, original_image
now has a red rectangle over it. It is actually fast enough for my 30fps usb camera so I can get it in real time.
There is no scaling done right now. Also, it assumes that the overlay image background is white, which will be made transparent.
发布评论
评论(2)
如果您使用完整的 .NET 平台,System.Drawing.Imaging 具有用于 Alpha 通道和遮罩的函数:
http://www.codeproject.com/KB/GDI-plus/alphafx.aspx
If you are using the full .NET platform, System.Drawing.Imaging has functions for alpha channels and masking:
http://www.codeproject.com/KB/GDI-plus/alphafx.aspx
一个非常强大的库值得一看,它是 ImageMagick。 他们甚至还有该库的 .NET 端口。 它可以做与透明度、形状和叠加相关的事情。
我可能会首先尝试使用 命令行版本 (
convert 或
composite
命令)看看是否可以让它们执行您想要的操作。 如果可行,那么您应该能够使用他们的库实现相同的功能。One quite robust library worth looking at is ImageMagick. They even have a .NET port of the library. It can do things related to transparency, shapes, and superimposing.
I might try using the command-line versions first (the
convert
orcomposite
command, for example) to see if you can make them do what you want. If that works, then you ought to be able to implement that same functionality using their libraries.