EMGU.CV入门(九、形态学操作)

22 篇文章 29 订阅

一、常见形态学操作

在这里插入图片描述

二、代码

 // 1. 加载原图
 var image1 = new Image<Bgr, byte>("test.png");
 var image0 = image1.Clone();
 PreviewImage1 = new WriteableBitmap(Bitmap2BitmapImage(Text(image1.Bitmap, "原图")));
 
 // 2. 膨胀 将黑色变小,白色变大
 var image2 = new Image<Bgr, byte>(image1.Size);
 Mat element = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(3, 3), new Point(-1, -1));
 CvInvoke.Dilate(image0.Clone(), image2,element,new Point(-1,-1),2,BorderType.Default,new MCvScalar());
 PreviewImage2 = new WriteableBitmap(Bitmap2BitmapImage(Text(image2.Bitmap, "膨胀")));

 // 3. 腐蚀 将白色变小,黑色变大
 var image3 = new Image<Bgr, byte>(image1.Size);
 CvInvoke.Erode(image0.Clone(), image3, element, new Point(-1, -1), 1, BorderType.Default, new MCvScalar());
 PreviewImage3 = new WriteableBitmap(Bitmap2BitmapImage(Text(image3.Bitmap, "腐蚀")));

 // 4. 开运算(先腐蚀后膨胀)消除小物体,平滑
 var image4 = new Image<Bgr, byte>(image1.Size);
 CvInvoke.MorphologyEx(image0.Clone(),image4,MorphOp.Open,element,new Point(-1,-1),1,BorderType.Default,new MCvScalar());
 PreviewImage4 = new WriteableBitmap(Bitmap2BitmapImage(Text(image4.Bitmap, "开运算")));

 // 5. 闭运算(先膨胀后腐蚀)消除小型黑洞
 var image5 = new Image<Bgr, byte>(image1.Size);
 CvInvoke.MorphologyEx(image0.Clone(), image5, MorphOp.Close, element, new Point(-1, -1), 1, BorderType.Default, new MCvScalar());
 PreviewImage5 = new WriteableBitmap(Bitmap2BitmapImage(Text(image5.Bitmap, "闭运算")));

 // 6. 形态学梯度(膨胀图和腐蚀图之差) 保留边缘
 var image6 = new Image<Bgr, byte>(image1.Size);
 CvInvoke.MorphologyEx(image0.Clone(), image6, MorphOp.Gradient, element, new Point(-1, -1), 1, BorderType.Default, new MCvScalar());
 PreviewImage6 = new WriteableBitmap(Bitmap2BitmapImage(Text(image6.Bitmap, "形态学梯度")));

 // 7. 顶帽 (原图和开运算作差) 分离比 邻近点 亮的点
 var image7 = new Image<Bgr, byte>(image1.Size);
 CvInvoke.MorphologyEx(image0.Clone(), image7, MorphOp.Tophat, element, new Point(-1, -1), 1, BorderType.Default, new MCvScalar());
 PreviewImage7 = new WriteableBitmap(Bitmap2BitmapImage(Text(image7.Bitmap, "顶帽")));

 // 8. 黑帽 (闭运算和原图作差) 分离比 邻近点 暗的点
 var image8 = new Image<Bgr, byte>(image1.Size);
 CvInvoke.MorphologyEx(image0.Clone(), image8, MorphOp.Blackhat, element, new Point(-1, -1), 1, BorderType.Default, new MCvScalar());
 PreviewImage8 = new WriteableBitmap(Bitmap2BitmapImage(Text(image8.Bitmap, "黑帽")));

 // 9. 一个没什么意义的转换
 var image9 = new Image<Bgr, byte>(image1.Size);
 var image10 = new Image<Bgr, byte>(image1.Size);
 CvInvoke.MorphologyEx(image0.Clone(), image9, MorphOp.Tophat, element, new Point(-1, -1), 1, BorderType.Default, new MCvScalar());
 CvInvoke.BitwiseNot(image9,image9);
 CvInvoke.BitwiseXor(image9,image0.Clone(),image10);
 var bm = image10.Bitmap;
 Graphics g = Graphics.FromImage(bm);
 g.DrawString("顶帽+取反+异或", new Font("Verdana", 20), new SolidBrush(Color.Black), new PointF(20, 0));
 PreviewImage9 = new WriteableBitmap(Bitmap2BitmapImage(bm));

三、说明

//
// 摘要:
//     Performs advanced morphological transformations.
//
// 参数:
//   src:
//     Source image.
//
//   dst:
//     Destination image.
//
//   kernel:
//     Structuring element.
//
//   operation:
//     Type of morphological operation.
//
//   iterations:
//     Number of times erosion and dilation are applied.
//
//   borderType:
//     Pixel extrapolation method.
//
//   anchor:
//     Anchor position with the kernel. Negative values mean that the anchor is at the
//     kernel center.
//
//   borderValue:
//     Border value in case of a constant border.
public static void MorphologyEx(IInputArray src, IOutputArray dst, MorphOp operation, IInputArray kernel, Point anchor, int iterations, BorderType borderType, MCvScalar borderValue)

//
// 摘要:
//     Returns a structuring element of the specified size and shape for morphological
//     operations.
//
// 参数:
//   shape:
//     Element shape
//
//   ksize:
//     Size of the structuring element.
//
//   anchor:
//     Anchor position within the element. The value (-1, -1) means that the anchor
//     is at the center. Note that only the shape of a cross-shaped element depends
//     on the anchor position. In other cases the anchor just regulates how much the
//     result of the morphological operation is shifted.
//
// 返回结果:
//     The structuring element
public static Mat GetStructuringElement(ElementShape shape, Size ksize, Point anchor)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值