一、常见形态学操作
二、代码
// 1. 加载原图
var image1 = new Image("test.png");
var image0 = image1.Clone();
PreviewImage1 = new WriteableBitmap(Bitmap2BitmapImage(Text(image1.Bitmap, "原图")));
// 2. 膨胀 将黑色变小,白色变大
var image2 = new Image(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(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(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(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(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(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(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(image1.Size);
var image10 = new Image(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)