/// <summary> /// 图像逻辑运算 /// </summary> /// <param name="bgImage">二值背景</param> /// <param name="fgImage">二值前景</param> /// <param name="logicMethod">逻辑运算方法</param> /// <returns></returns> public Bitmap LogicOperate(Bitmap bgImage, Bitmap fgImage, LogicMethod logicMethod) { Bitmap dstImage = (Bitmap)bgImage.Clone(); Graphics g = System.Drawing.Graphics.FromImage(dstImage); // 计算有效区域 Region validRegion = bgRegion; validRegion.Intersect(fgRegion); RectangleF validRect = validRegion.GetBounds(g); RectangleF fgRect = fgRegion.GetBounds(g); RegionClip bgRegionClip = new RegionClip(validRegion); Bitmap background = bgRegionClip.Hold((Bitmap)bgImage.Clone()); RegionClip fgRegionClip = new RegionClip(validRegion); validRegion.Translate(-fgRect.X, -fgRect.Y); Bitmap foreground = fgRegionClip.Hold((Bitmap)fgImage.Clone()); validRegion.Translate(fgRect.X, fgRect.Y); // 先将原始二值图转化为二维数组 byte[,] bgGray = Image2Array(background); byte[,] fgGray = Image2Array(foreground); // 进行逻辑运算处理后的灰度二维数组 byte[,] dstGray = null; // 进行逻辑运算 switch (logicMethod) { case LogicMethod.And: dstGray = LogicAnd(bgGray, fgGray); break; case LogicMethod.Or: dstGray = LogicOr(bgGray, fgGray); break; case LogicMethod.Xor: dstGray = LogicXor(bgGray, fgGray); break; } // 将二值数组转化为灰度图 Bitmap validImage = Array2Image(dstGray); g.DrawImage(validImage, validRect, new Rectangle(0, 0, (int)validRect.Width, (int)validRect.Height), GraphicsUnit.Pixel); bgImage.Dispose(); fgImage.Dispose(); return(dstImage); } // end of LogicOperate
/// <summary> /// 图像代数运算 /// </summary> /// <param name="bgImage">背景</param> /// <param name="fgImage">前景</param> /// <param name="algebraMethod">代数运算方法</param> /// <returns></returns> public Bitmap AlgebraOperate(Bitmap bgImage, Bitmap fgImage, AlgebraMethod algebraMethod) { // 计算有效区域 Region validRegion = bgRegion; validRegion.Intersect(fgRegion); RegionClip rc = new RegionClip(validRegion); Bitmap background = rc.Hold((Bitmap)bgImage.Clone()); Bitmap dstImage = rc.Remove((Bitmap)bgImage.Clone()); Graphics g = System.Drawing.Graphics.FromImage(dstImage); RectangleF validRect = validRegion.GetBounds(g); RectangleF fgRect = fgRegion.GetBounds(g); validRegion.Translate(-fgRect.X, -fgRect.Y); Bitmap foreground = rc.Hold((Bitmap)fgImage.Clone()); validRegion.Translate(fgRect.X, fgRect.Y); Bitmap validImage = null; switch (algebraMethod) { case AlgebraMethod.Add: validImage = Add(background, foreground); break; case AlgebraMethod.Subtract: validImage = Subtract(background, foreground); break; case AlgebraMethod.Multiply: validImage = Multiply(background, foreground); break; case AlgebraMethod.Divide: validImage = Divide(background, foreground); break; case AlgebraMethod.Average: validImage = Average(background, foreground); break; case AlgebraMethod.Differ: validImage = Differ(background, foreground); break; case AlgebraMethod.Maximize: validImage = Maximize(background, foreground); break; case AlgebraMethod.Minimum: validImage = Minimum(background, foreground); break; } g.DrawImage(validImage, validRect, new Rectangle(0, 0, (int)validRect.Width, (int)validRect.Height), GraphicsUnit.Pixel); bgImage.Dispose(); fgImage.Dispose(); return(dstImage); } // end of AlgebraOperate