作者:王先荣
前言
近期需要做一些图像处理方面的学习和研究,首要任务就是选择一套合适的图像处理类库。目前较知名且功能完善的图像处理类库有OpenCv、EmguCv、AForge.net等等。本文将从许可协议、下载、安装、文档资料、易用性、性能等方面对这些类库进行比较,然后给出选择建议,当然也包括我自己的选择。
许可协议
类库 | 许可协议 | 许可协议网址 | 大致介绍 |
OpenCv | BSD | www.opensource.org/licenses/bsd-license.html | 在保留原来BSD协议声明的前提下,随便怎么用都行 |
EmguCv | GPL v3 | http://www.gnu.org/licenses/gpl-3.0.txt | 你的产品必须也使用GPL协议,开源且免费 |
商业授权 | http://www.emgu.com/wiki/files/CommercialLicense.txt | 给钱之后可以用于闭源的商业产品 | |
AForge.net | LGPL v3 | http://www.gnu.org/licenses/lgpl.html | 如果不修改类库源代码,引用该类库的产品可以闭源和(或)收费 |
以上三种类库都可以用于开发商业产品,但是EmguCv需要付费;因为我只是用来学习和研究,所以这些许可协议对我无所谓。不过鉴于我们身在中国,如果脸皮厚点,去他丫的许可协议。
下载
可以很方便的下载到这些类库,下载地址分别为:
类库 |
下载地址 |
OpenCv |
http://sourceforge.net/projects/opencvlibrary/files/ |
EmguCv |
http://www.emgu.com/wiki/index.php/Download_And_Installation |
AForge.net |
http://www.aforgenet.com/framework/downloads.html |
安装
这些类库的安装都比较简单,直接运行安装程序,并点“下一步”即可完成。但是OpenCv在安装完之后还需要一些额外的处理才能在VS2008里面使用,在http://www.opencv.org.cn有一篇名为《VC2008 Express下安装OpenCv 2.0》的文章专门介绍了如何安装OpenCv。
类库 |
安装难易度 |
备注 |
OpenCv |
比较容易 |
VC下使用需要重新编译 |
EmguCv |
容易 |
|
AForge.net |
容易 |
|
相信看这篇文章的人都不会被安装困扰。
文档资料
类库 |
总体评价 |
书籍 |
网站 |
文档 |
示例 |
社区 |
备注 |
OpenCv |
中等 |
中英文 |
中英文 |
中英文 |
较多 |
中文论坛 |
有中文资料但不完整 |
EmguCv |
少 |
无 |
英文 |
英文 |
少 |
英文论坛 |
论坛人气很差 |
AForge.net |
少 |
无 |
英文 |
英文 |
少 |
英文论坛 |
论坛人气很差 |
OpenCv有一些中文资料,另外两种的资料全是英文的;不过EmguCv建立在OpenCv的基础上,大部分OpenCv的资料可以用于EmguCv;而AForge.net是原生的.net类库,对GDI+有很多扩展,一些MSDN的资料可以借鉴。如果在查词典的基础上还看不懂英文文档,基本上可以放弃使用这些类库了。
易用性
易用性这玩意,主观意志和个人能力对它影响很大,下面是我的看法:
类库 |
易用性 |
备注 |
OpenCv |
比较差 |
OpenCv大多数功能都以C风格函数形式提供,少部分功能以C++类提供。注意:2.0版将更多的功能封装成类了。 |
EmguCv |
比较好 |
将OpenCv的绝大部分功能都包装成了.net类、结构或者枚举。不过文档不全,还是得对照OpenCv的文档去看才行。 |
AForge.net |
好 |
纯.net类库,用起来很方便。 |
最近几年一直用的是C# ,把C和C++忘记得差不多了,况且本来C/C++我就不太熟,所以对OpenCv的看法恐怕有偏见。
性能
这些类库能做的事情很多,我选了最基础的部分来进行性能测试,那就是将一幅彩色图像转换成灰度图,然后再将灰度图转换成二值图像。因为图像处理大部分时间都用于内存读写及运算(特别是矩阵运算),所以这两种操作有一定的代表性。
我分别用以下方式实现了图像的灰度化及二值化:(1)C语言调用OpenCv库;(2)C#调用AForge.net库;(3)C#调用EmguCv库;(4)C#中用P/INVOKE的形式调用OpenCv函数;(5)C#调用自己写的灰度和二值化方法。
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include "winbase.h" int _tmain(int argc, _TCHAR* argv[])
{
//初始化图像
IplImage * pIplSource=cvLoadImage("E:\\xrwang\\ImageProcessLearn\\Debug\\wky_tms_2272x1704.jpg");
IplImage * pIplGrayscale=cvCreateImage(cvSize(pIplSource->width,pIplSource->height),IPL_DEPTH_8U,1);
IplImage * pIplThreshold=cvCreateImage(cvSize(pIplSource->width,pIplSource->height),IPL_DEPTH_8U,1);
//执行灰度化和二值化,并输出所用时间
LARGE_INTEGER frequency,count1,count2,count3;
double time1,time2;
QueryPerformanceFrequency(&frequency);
for(int i=0;i<10;i++)
{
QueryPerformanceCounter(&count1);
cvCvtColor(pIplSource,pIplGrayscale,CV_BGR2GRAY);
QueryPerformanceCounter(&count2);
cvThreshold(pIplGrayscale,pIplThreshold,128,255,CV_THRESH_BINARY);
QueryPerformanceCounter(&count3);
time1=(double)1000.0*(count2.QuadPart-count1.QuadPart)/frequency.QuadPart;
time2=(double)1000.0*(count3.QuadPart-count2.QuadPart)/frequency.QuadPart;
printf("灰度:%g毫秒,二值化:%g毫秒\r\n",time1,time2);
}
//显示图像
cvNamedWindow("grayscale",0);
cvNamedWindow("threshold",0);
cvResizeWindow("grayscale",600,480);
cvResizeWindow("threshold",600,480);
cvShowImage("grayscale",pIplGrayscale);
cvShowImage("threshold",pIplThreshold);
cvWaitKey(0);
//销毁对象
cvDestroyAllWindows();
cvReleaseImage(&pIplThreshold);
cvReleaseImage(&pIplGrayscale);
cvReleaseImage(&pIplSource);
return 0;
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using AForge.Imaging.Filters;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum; namespace ImageProcessLearn
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
//窗体加载时
private void FormMain_Load(object sender, EventArgs e)
{
//显示原始图像
pbSource.Image = Image.FromFile("wky_tms_2272x1704.jpg");
}
//使用选定的类库处理图像
private void btnProcess_Click(object sender, EventArgs e)
{
if (rbAForge.Checked)
{
ProcessImageWithAforge();
}
else if (rbEmgucv.Checked)
{
ProcessImageWithEmgucv();
}
else if (rbOpencv.Checked)
{
ProcessImageWithOpencv();
}
else if (rbOwnMethod.Checked)
ProcessImageWithOwnMethod();
}
/// <summary>
/// 使用AForge.net处理图像
/// </summary>
private void ProcessImageWithAforge()
{
Stopwatch sw = new Stopwatch(); //计时器
//灰度
sw.Start();
Grayscale grayscaleFilter = new Grayscale(0.299, 0.587, 0.114);
Bitmap bitmapGrayscale = grayscaleFilter.Apply((Bitmap)pbSource.Image);
sw.Stop();
double timeGrayscale = sw.Elapsed.TotalMilliseconds;
if (pbGrayscale.Image != null)
{
pbGrayscale.Image.Dispose();
pbGrayscale.Image = null;
}
pbGrayscale.Image = bitmapGrayscale;
//二值化
sw.Reset();
sw.Start();
Threshold thresholdFilter = new Threshold(128);
Bitmap bitmapThreshold = thresholdFilter.Apply(bitmapGrayscale);
sw.Stop();
double timeThreshold = sw.Elapsed.TotalMilliseconds;
if (pbThreshold.Image != null)
{
pbThreshold.Image.Dispose();
pbThreshold.Image = null;
}
pbThreshold.Image = bitmapThreshold;
//输出所用时间
txtResult.Text += string.Format("类库:AForge.net,灰度:{0:F05}毫秒,二值化:{1:F05}毫秒\r\n", timeGrayscale, timeThreshold);
}
/// <summary>
/// 使用EmguCv处理图像
/// </summary>
private void ProcessImageWithEmgucv()
{
Stopwatch sw = new Stopwatch(); //计时器
//灰度
Image<Bgr, Byte> imageSource = new Image<Bgr, byte>((Bitmap)pbSource.Image);
sw.Start();
Image<Gray, Byte> imageGrayscale = imageSource.Convert<Gray, Byte>();
sw.Stop();
double timeGrayscale = sw.Elapsed.TotalMilliseconds;
if (pbGrayscale.Image != null)
{
pbGrayscale.Image.Dispose();
pbGrayscale.Image = null;
}
pbGrayscale.Image = imageGrayscale.ToBitmap();
//二值化
sw.Reset();
sw.Start();
Image<Gray, Byte> imageThreshold = imageGrayscale.ThresholdBinary(new Gray(128), new Gray(255));
sw.Stop();
double timeThreshold = sw.Elapsed.TotalMilliseconds;
if (pbThreshold.Image != null)
{
pbThreshold.Image.Dispose();
pbThreshold.Image = null;
}
pbThreshold.Image = imageThreshold.ToBitmap();
//输出所用时间
txtResult.Text += string.Format("类库:EmguCv,灰度:{0:F05}毫秒,二值化:{1:F05}毫秒\r\n", timeGrayscale, timeThreshold);
}
/// <summary>
/// 使用Open Cv P/Invoke处理图像
/// </summary>
unsafe private void ProcessImageWithOpencv()
{
Stopwatch sw = new Stopwatch(); //计时器
//灰度
Image<Bgr, Byte> imageSource = new Image<Bgr, byte>((Bitmap)pbSource.Image);
IntPtr ptrSource = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MIplImage)));
Marshal.StructureToPtr(imageSource.MIplImage, ptrSource, true);
sw.Start();
IntPtr ptrGrayscale = CvInvoke.cvCreateImage(imageSource.Size, IPL_DEPTH.IPL_DEPTH_8U, 1);
CvInvoke.cvCvtColor(ptrSource, ptrGrayscale, COLOR_CONVERSION.CV_BGR2GRAY);
sw.Stop();
double timeGrayscale = sw.Elapsed.TotalMilliseconds;
if (pbGrayscale.Image != null)
{
pbGrayscale.Image.Dispose();
pbGrayscale.Image = null;
}
pbGrayscale.Image = ImageConverter.IplImagePointerToBitmap(ptrGrayscale);
//二值化
sw.Reset();
sw.Start();
IntPtr ptrThreshold = CvInvoke.cvCreateImage(imageSource.Size, IPL_DEPTH.IPL_DEPTH_8U, 1);
CvInvoke.cvThreshold(ptrGrayscale, ptrThreshold, 128d, 255d, THRESH.CV_THRESH_BINARY);
sw.Stop();
double timeThreshold = sw.Elapsed.TotalMilliseconds;
if (pbThreshold.Image != null)
{
pbThreshold.Image.Dispose();
pbThreshold.Image = null;
}
pbThreshold.Image = ImageConverter.IplImagePointerToBitmap(ptrThreshold);
//释放资源
//CvInvoke.cvReleaseImage(ref ptrThreshold);
//CvInvoke.cvReleaseImage(ref ptrGrayscale);
Marshal.FreeHGlobal(ptrSource);
//输出所用时间
txtResult.Text += string.Format("类库:OpenCv P/Invoke,灰度:{0:F05}毫秒,二值化:{1:F05}毫秒\r\n", timeGrayscale, timeThreshold);
}
/// <summary>
/// 使用自定义的方法处理图像
/// </summary>
private void ProcessImageWithOwnMethod()
{
Stopwatch sw = new Stopwatch(); //计时器
//灰度
sw.Start();
Bitmap bitmapGrayscale = Grayscale((Bitmap)pbSource.Image);
sw.Stop();
double timeGrayscale = sw.Elapsed.TotalMilliseconds;
if (pbGrayscale.Image != null)
{
pbGrayscale.Image.Dispose();
pbGrayscale.Image = null;
}
pbGrayscale.Image = bitmapGrayscale;
//二值化
sw.Reset();
sw.Start();
Bitmap bitmapThreshold = Threshold(bitmapGrayscale, 128);
sw.Stop();
double timeThreshold = sw.Elapsed.TotalMilliseconds;
if (pbThreshold.Image != null)
{
pbThreshold.Image.Dispose();
pbThreshold.Image = null;
}
pbThreshold.Image = bitmapThreshold;
//输出所用时间
txtResult.Text +=
【下篇】Visual C# 2010新特性之命名和可选参数与类型等价支持