CV2.MorphologyEx()
参数 | 描述 |
---|---|
InputArray src | 源图像 |
OutputArray dst | 输出图像 |
MorphTypes op | 形态操作类型 |
InputArray element | 结构数组 |
Point? anchor = null | 锚点(中心点) |
int iterations = 1 | 应用腐蚀和膨胀的次数。[默认情况下这是1] |
BorderTypes borderType = BorderTypes.Constant | 边缘处理方法 |
Scalar? borderValue = null | 在边界为常数的情况下的边界值。默认值具有特殊意义。[默认情况下这是cvcp . morphologydefaultbordervalue ()] |
开操作:open: 先腐蚀后膨胀,可以去掉小的对象。
d s t = o p e n ( s r c , e l e m e n t ) = d i l a t e ( e r o d e ( s r c , e l e m e n t ) ) dst=open(src,element)=dilate(erode(src,element)) dst=open(src,element)=dilate(erode(src,element))
代码:
///
/// 形态学开操作:先腐蚀后膨胀(可以去掉图像中小的对象)
///
private static void MorphologyOpen(string path)
{
using (Mat src = new Mat(path, ImreadModes.AnyColor | ImreadModes.AnyDepth))
using (Mat dst = new Mat())
{
//:定义结构元素
InputArray kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5), new Point(-1, -1));
//开操作,也可省落变量前缀名
Cv2.MorphologyEx(src: src, dst: dst, op: MorphTypes.Open, element: kernel, anchor: new Point(-1, -1));
using (new Window("SRC", WindowMode.Normal, src))
using (new Window("DST", WindowMode.Normal, dst))
{
Cv2.WaitKey(0);
}
}
}
闭操作 :close:先膨胀后腐蚀,可以填充图像的噪点。
d s t = c o l s e ( s r c , e l e m e n t ) = e r o d e ( d i l a t e ( s r c , e l e m e n t ) ) dst=colse(src,element)=erode(dilate(src,element)) dst=colse(src,element)=erode(dilate(src,element))
代码:
///
/// 形态学闭操作 :先膨胀后腐蚀(可以填充图像小的‘洞’,纯色如果包含小的污渍可以去掉污渍)
///
private static void MorphologyClose(string path)
{
using (Mat src = new Mat(path, ImreadModes.AnyColor | ImreadModes.AnyDepth))
using (Mat dst = new Mat())
{
//:定义结构元素
InputArray kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5,5), new Point(-1, -1));
//闭操作,也可省落变量前缀名
Cv2.MorphologyEx(src: src, dst: dst, op: MorphTypes.Close, element: kernel, anchor: new Point(-1, -1));
using (new Window("SRC", WindowMode.Normal, src))
using (new Window("DST", WindowMode.Normal, dst))
{
Cv2.WaitKey(0);
}
}
}
小黑点没了!
形态学梯度:Morphological Gradient : 膨胀减去腐蚀,又称基本梯度(还包括:内部梯度,方向梯度)
d s t = M o r p h g r a d ( s r c , e l e m e n t ) = d i l a t e ( s e c , e l e m e n t ) − e r o d e ( s r c , e l e m e n t ) dst=Morph_{grad}(src,element)=dilate(sec,element)-erode(src,element) dst=Morphgrad(src,element)=dilate(sec,element)−erode(src,element)
代码:
///
/// 形态学梯度- Morphological Gradient
/// 1;膨胀减去腐蚀
/// 2;又称为基本梯度(其它还包括-内部梯度、方向梯度)
///
private static void MorphologyGradient(string path)
{
using (Mat src = new Mat(path, ImreadModes.AnyColor | ImreadModes.AnyDepth))
using (Mat dst = new Mat())
{
//:定义结构元素
InputArray kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15), new Point(-1, -1));
//形态学梯度操作,也可省落变量前缀名
Cv2.MorphologyEx(src: src, dst: dst, op: MorphTypes.Gradient, element: kernel, anchor: new Point(-1, -1));
using (new Window("SRC", WindowMode.Normal, src))
using (new Window("DST", WindowMode.Normal, dst))
{
Cv2.WaitKey(0);
}
}
}
**顶帽 top hat:**是原图像与开操作之间的差值操作。
代码:
///
/// 形态学顶帽:是原图像与开操作之间的差值图像
///
private static void MorphologyTopHAT(string path)
{
using (Mat src = new Mat(path, ImreadModes.AnyColor | ImreadModes.AnyDepth))
using (Mat dst = new Mat())
{
//:定义结构元素
InputArray kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(25, 25), new Point(-1, -1));
//顶帽操作,也可省落变量前缀名
Cv2.MorphologyEx(src: src, dst: dst, op: MorphTypes.TopHat, element: kernel, anchor: new Point(-1, -1));
using (new Window("SRC", WindowMode.Normal, src))
using (new Window("DST", WindowMode.Normal, dst))
{
Cv2.WaitKey(0);
}
}
}
**黑帽:black hat:**闭操作图像与原图像的差值图像
代码:
///
/// 形态学黑帽:闭操作图像与源图像的差值图像
///
private static void MorphologyBlackHAT(string path)
{
using (Mat src = new Mat(path, ImreadModes.AnyColor | ImreadModes.AnyDepth))
using (Mat dst = new Mat())
{
//:定义结构元素
InputArray kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15), new Point(-1, -1));
//顶帽操作,也可省落变量前缀名
Cv2.MorphologyEx(src: src, dst: dst, op: MorphTypes.BlackHat, element: kernel, anchor: new Point(-1, -1));
using (new Window("SRC", WindowMode.Normal, src))
using (new Window("DST", WindowMode.Normal, dst))
{
Cv2.WaitKey(0);
}
}
}