OpenCVSharp-⽤MorphologyEx函数做形态学变换OpenCVSharp 跑⼀遍OpenCV官⽅教程(全为⼿敲代码,如有雷同都是我的错)
⽤MorphologyEx函数做形态学变换
⼀系列变换包括:
开运算
闭运算
形态学梯度
顶帽
⿊帽
核⼼函数:
MorphologyEx()
GetStructuringElement()
效果图
using OpenCvSharp;
using System;
namespace ConsoleApp1
{
class tutorial5 : ITutorial
{
Mat src, dst;
int morph_elem = 0;
int morph_size = 0;
int morph_operator = 0;
int max_operator = 4;
int max_elem = 2;
int max_kernel_size = 21;
string window_name = "Morphology Transformations Demo";
public void Run()
{
src = Cv2.ImRead("baboon.jpg", ImreadModes.Color);
if (src.Empty())
{
Console.WriteLine("Could not open or find the image!\n");
return;
}
dst = new Mat();
Cv2.NamedWindow(window_name, WindowFlags.AutoSize); // Create window
Cv2.CreateTrackbar("Operator", window_name, ref morph_operator, max_operator, new TrackbarCallbackNative(Morphology_Operations));
Cv2.CreateTrackbar("Element:\n 0: Rect - 1: Cross - 2: Ellipse", window_name,
ref morph_elem, max_elem,
Morphology_Operations);
Cv2.CreateTrackbar("Kernel size:\n 2n +1", window_name,
ref morph_size, max_kernel_size,
Morphology_Operations);
Morphology_Operations(0, IntPtr.Zero);
Cv2.WaitKey(0);
return;
}
void Morphology_Operations(int pos, IntPtr usedata)
{
// Since MORPH_X : 2,3,4,5 and 6
MorphTypes operation = (MorphTypes)(morph_operator + 2);
Mat element = Cv2.GetStructuringElement((MorphShapes)morph_elem, new Size(2 * morph_size + 1, 2 * morph_size + 1), new Point(morph_size, morph            Cv2.MorphologyEx(src, dst, operation, element);
Cv2.ImShow(window_name, dst);writeline函数
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。