这里就不写怎么配置EmguCV了,毕竟百度一下还是可以找到的(没错,我就是怕麻烦......),这里主要是记录一下EmguCv的学习笔记,网上能查阅的资料太少了,不过OPenCv的资料还是蛮多的,而且根据EmguCv的版本不同,相应的函数也不一样。楼主是啃着《Emgu CV Essential》这本书一边百度翻译,一边敲代码学习的,英语不好的我想哭QAQ。
好的,不跑题了,楼主主要用的是3.0版本+vs2010,其实《Emgu CV Essential》是不适应的,不过也有可能是楼主下的这本版本比较老,但没办法啊,资料太少,哭着也要啃下去,谁叫当初嘴贱,提什么做人脸识别,好想打死当时那个自己。咦,我好想又跑题了......
这里先贴一下书中的内容:
//Go to the code view of form1.cs. The most important thing is to add the following namespaces: using Emgu.CV; using Emgu.CV.CvEnum; using Emgu.CV.Structure; //Hello World //Next, create a member variable in the class Form1. //Set the name of pop-up window String winname = "First Window"; //Now let's come to the coding part of the Hello World button. private void button1_Click(object sender, EventArgs e) { //Create a window with the specific name CvInvoke.cvNamedWindow(winname); //Create an image of 480x200 with color yellow using (Image<Bgr, Byte> img1 = new Image<Bgr, byte>(480, 200, new Bgr(0, 255, 255))) { //Create a font MCvFont font=new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0); //Draw "Hello, world" on the yellow image; Start point is (25, 100) with color blue img1.Draw("Hello, world", ref font, new Point(25, 100), new Bgr(255, 0, 0)); //Show the img1 in the window CvInvoke.cvShowImage(winname, img1.Ptr); //A key pressing event CvInvoke.cvWaitKey(0); //Destroy the window CvInvoke.cvDestroyWindow(winname); } }
在写这个代码之前内,先要将几个dll加入引用中,如图所示:
首先建立窗体项目,然后复制粘贴代码,当然你也可以选择手打,如果你和楼主是一样的版本,你会发现出现了许多错误,如图所示:
此时楼主的内心是崩溃的,不知道你们是不是一样,然后楼主发挥了一下探索精神,首先是没有定义的CvInvoke.cvNameWindow,楼主发现有个CvInvoke.NamedWindow,它的解释是这样的:
CvInvoke.NamedWindow(string name, [NamedWindowType flags = NamedWindowType.AutoSize])
Creates a window which can be use as aplaceholder for images and trackbars.Created window are reffered by their names,If the window with such a name already exists,the function does nothing.
参数是string类型,解释也差不多,于是抱着试一试的想法,就用了,发现,嘿,成了!
下面的各种错误都是通过查看函数释义来解决的,解决后的代码如下所示:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Emgu.CV; using Emgu.CV.CvEnum; using Emgu.CV.Structure; namespace HelloEmguCv { public partial class Form1 : Form { string winName = "First Window"; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { CvInvoke.NamedWindow(winName); using (Image<Bgr, Byte> img1 = new Image<Bgr, byte>(480, 200, new Bgr(0, 255, 255))) { Bgr fontColor=new Bgr(0,0,255); //设置字体的颜色 FontFace fontFace=new FontFace(); //字体的外观 double fontSize=1; //设置字体的大小 img1.Draw("Hello,EmguCV", new Point(125, 100), fontFace,fontSize , fontColor); //img1.Draw(字符串,字符串的位置,字体,字体的大小,Brg类的颜色) CvInvoke.Imshow(winName, img1); //在弹出的窗体上显示图片 CvInvoke.WaitKey(0); CvInvoke.DestroyWindow(winName); } } } }
然后代码运行的效果是这样的:
楼主也在学习当中,所以进度还是蛮慢的,so,在此大家一起学习咯.