CvInvoke类包含了大多数的基本图像处理功能,像滤波、直方图操作、形态学处理等等。下面写一些示例。
首先,在VS2012中新建一个C#控制台应用程序,并按上一篇———所说的配置方式进行配置。
示例一:
加载图像、转换为灰度图像、获取图像边缘。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
//简单图像处理
Mat img = CvInvoke.Imread("E:\\image\\image\\xyjy.jpg", LoadImageType.Unchanged);
if (img.IsEmpty)
{
Console.WriteLine("can not load the image \n");
}
CvInvoke.Imshow("Image", img);
Mat grayImg = new Mat();
//转换为灰度图像
CvInvoke.CvtColor(img, grayImg, ColorConversion.Rgb2Gray);
CvInvoke.Imshow("Gray Image", grayImg);
//sobel
Mat sobelImg = new Mat();
CvInvoke.Sobel(grayImg, sobelImg, grayImg.Depth, 1, 0);
//使用canny算子查找边缘
Mat cannyImg = new Mat();
CvInvoke.Canny(grayImg, cannyImg, 20, 40);
CvInvoke.Imshow("Canny Image", cannyImg);
CvInvoke.WaitKey(0);
}
}
}
示例二:
直方图均匀化、高斯滤波、均值滤波、二值化、腐蚀膨胀、闭操作等。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
//简单图像处理
Mat src = CvInvoke.Imread("E:\\image\\image\\image.jpg", LoadImageType.Unchanged);
if (src.IsEmpty)
{
Console.WriteLine("can not load the image \n");
}
CvInvoke.Imshow("Image", src);
CvInvoke.CvtColor(src, src, ColorConversion.Rgb2Gray);
//直方图均匀化
//Mat dst = new Mat(src.Size, DepthType.Cv8U, 1);
//CvInvoke.EqualizeHist(src, dst);
//CvInvoke.Imshow("Equalization", src);
//高斯滤波
CvInvoke.GaussianBlur(src, src, new Size(3, 3), 3);
CvInvoke.Imshow("GaussianBlur Image", src);
//均值滤波
CvInvoke.Blur(src, src, new Size(3, 3), new Point(-1, -1));
CvInvoke.Imshow("Blur Image", src);
//二值化
CvInvoke.Threshold(src, src, 70, 255, ThresholdType.BinaryInv);
CvInvoke.Imshow("Threshold Image", src);
//腐蚀、膨胀
//Mat struct_element1 = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(3, 3), new Point(-1, -1));
//CvInvoke.Dilate(src, src, struct_element1, new Point(-1, -1), 1, BorderType.Default, new MCvScalar(0, 0, 0));
//Mat struct_element2 = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(5, 5), new Point(-1, -1));
//CvInvoke.Erode(src, src, struct_element2, new Point(-1, -1), 1, BorderType.Default, new MCvScalar(0, 0, 0));
//闭操作
Mat struct_element = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(3, 3), new Point(-1, -1));
CvInvoke.MorphologyEx(src, src, MorphOp.Close, struct_element, new Point(-1, -1), 3, BorderType.Default, new MCvScalar(0, 0, 0));
CvInvoke.Imshow("Erode Image", src);
Mat dst_canny = new Mat();
CvInvoke.Canny(src, dst_canny, 20, 40);
CvInvoke.Imshow("Canny Image", dst_canny);
CvInvoke.WaitKey(0);
}
}
}
Emgu CV Library Documentation:CvInvoke Class