学习图像处理知识---Emgu3.4 CvInvoke Class类学习(二)
前面已经简单学习基本图片处理用法,很多功能同Emgu3.4 image类,相同部分就不学习了,只做了解,
其实Emgu3.4 image类底层代码就是用CvInvoke Class的。
图片进行二值化后找出轮廓:
public static void FindContours( IInputOutputArray image,//输入图片必须为灰图图像 IOutputArray contours, //输出轮廓 IOutputArray hierarchy,//输出相量 RetrType mode, //轮廓类型 ChainApproxMethod method, //点连接模式 Point offset = null )
在轮廓处理时,我们使用到VectorOfVectorOfPoint类用于存取点阵。先进性学习:
Namespace: Emgu.CV.Util,文件在in Emgu.CV.World.dll中调用
同时它具有很多接口,在CvInvoke Class中很多地方用得到。
public class VectorOfVectorOfPoint : UnmanagedObject, IInputOutputArray, IInputArrayOfArrays, IOutputArrayOfArrays, IOutputArray, IInputArray
构造函数:
public VectorOfVectorOfPoint() //建立空对象
public VectorOfVectorOfPoint( //建立对象点阵数组实例 Point[][] values )
public VectorOfVectorOfPoint( //建立创建一个特定大小的VectorOfPoint的标准向量 int size )
public VectorOfVectorOfPoint( //创建一个带有初始值的VectorOfPoint的标准向量 params VectorOfPoint[] values )
其中常用的函数:
public void Clear() //清理数组
Push(VectorOfPoint) 有三种
Push(VectorOfPoint[])
Push(VectorOfVectorOfPoint) //此种用得多。常见属性用得多为其
public int Size { get; } //多用于找到轮廓个数。如果没有的话为零。
public VectorOfPoint this[ int index ] { get; }
找到轮廓后需画出轮廓以便显示是否正常。
Emgu.CV.Image<Gray,byte> GAY1 = new Image<Gray, byte>((Bitmap)pictureBox3.Image);
// pictureBox5.Image = GAY1.ThresholdBinaryInv(new Gray((int)numericUpDown1.Value), new Gray(250)).Bitmap;
// pictureBox5.Image = GAY1.ThresholdTrunc(new Gray((int)numericUpDown1.Value)).Bitmap;
VectorOfVectorOfPoint cwaikuo = new VectorOfVectorOfPoint();
GAY1 = GAY1.ThresholdAdaptive(new Gray(120), Emgu.CV.CvEnum.AdaptiveThresholdType.MeanC, Emgu.CV.CvEnum.ThresholdType.Binary, 3, new Gray((int)numericUpDown1.Value));
GAY1= GAY1.Canny(100, 180);
pictureBox4.Image = GAY1.ToBitmap();
CvInvoke.FindContours(GAY1, cwaikuo, null, Emgu.CV.CvEnum.RetrType.List, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
Emgu.CV.Image<bgr, byte> ditu = new Image<Bgr, byte>(GAY1.Size);//注意这里一定为非灰图图片,否则无法绘制颜色。
CvInvoke.DrawContours(ditu, cwaikuo, -1, new MCvScalar(0, 0, 255));
// ditu.Draw(new Rectangle(new Point(20,20),new Size(100,150)),)
pictureBox5.Image = ditu.ToBitmap();