本文介绍了如何在另一个较大的图像内部绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
如何使用OpenCV在另一个较大的图像内绘制图像?
谢谢
Hi
How to plot an image in an inside another larger image using OpenCV?
Thank you
推荐答案
// This is for Format8bppIndexed image. You may write your own if you are using BGR or other formats
// based on this code example. Good luck!
private static Bitmap PlotSmallImageOnBigImage(Bitmap bigImage, Bitmap smallImage)
{
// Error
Rectangle r1 = new Rectangle(0, 0, bigImage.Width, bigImage.Height);
Rectangle r2 = new Rectangle(0, 0, smallImage.Width, smallImage.Height);
if (r1.Contains(r2) == false)
throw new Exception("ERROR: small image must be inside the big image.");
int offsetWidth = (bigImage.Width-smallImage.Width)/2;
int offsetHeight = (bigImage.Height-smallImage.Height)/2;
Rectangle rect = new Rectangle(offsetWidth, offsetHeight, smallImage.Width, smallImage.Height);
BitmapData bigData = bigImage.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
BitmapData smallData=smallImage.LockBits(new Rectangle(0,0, smallImage.Width, smallImage.Height), ImageLockMode.ReadOnly,
PixelFormat.Format8bppIndexed);
unsafe
{
byte* ptrB = (byte*)(void*)bigData.Scan0;
int offsetB = bigData.Stride - rect.Width * 1; // see "1", this is for Format8bppIndexed. If BGR it is 3
byte* ptrS = (byte*)(void*)smallData.Scan0;
int offsetS = smallData.Stride - smallData.Width * 1;
for (int y = 0; y < rect.Height; y++)
{
for (int x = 0; x < rect.Width; x++)
{
ptrB[0] = ptrS[0]; // in BGR you may add transparency options here.
//ptrB[0] = (byte)((ptrB[0] + ptrS[0])/2);
ptrB++;
ptrS++;
}
ptrB += offsetB; // if any
ptrS += offsetS;
}
}
bigImage.UnlockBits(bigData);
smallImage.UnlockBits(smallData);
return bigImage;
}
这篇关于如何在另一个较大的图像内部绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!