1.下载OpenCv2.3.1安装
superpack需要run as administor
设置Environment variables中的
System variables
Path=.....\opencv\build\x86\vc10\bin\
user variables
Path=.....\opencv\build\x86\vc10\bin\ (没有需要自己创建)
2.下载OpenCvSharp
解压安装
3.在vs2012中配置
因为要使用OpenCvSharp,所以Template 选择c#
创建WindowsForm Application
Project->add reference
Browse到OpenCvSharp的文件夹,添加其中的一系列OpenCvSharp的.dll文件(除了OpenCvSharpExtern.dll)
reference里面不需要添加opencv的dll,也添加不了,会提示Please make sure taht the file is accessbile and that it is a valid assembly or COM component
这是因为
“ the file is a native DLL which means you can't add it to a .NET project via Add Reference... you can use via DllImport (see msdn.microsoft.com/en-us/library/…)”
添加OpenCvSharp的dll之后就可以在Solution Explorer里面看到 reference里面有了OpenCvSharp。
4.写测试代码,参考opencvsharp官网上面的sample code,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
namespace LennaOpenCVSharp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
IplImage src = Cv.LoadImage("lenna.png", LoadMode.GrayScale);
IplImage dst = Cv.CreateImage(new CvSize(src.Width, src.Height), BitDepth.U8, 1);
Cv.Canny(src, dst, 50, 200);
Cv.NamedWindow("src image");
Cv.ShowImage("src image", src);
Cv.NamedWindow("dst image");
Cv.ShowImage("dst image", dst);
Cv.WaitKey();
Cv.DestroyAllWindows();
Cv.ReleaseImage(src);
Cv.ReleaseImage(dst);
}
}
}
4.调整solution platform位x86
(这步是为了防止出现Problem - BadImageFormatException was unhandled,
这个错误的原因是 this usually happens in 64 bits systems. The problem is that the compiler trys to output 64 bits compatible code, but because it doesn't have the necessary assemblies to do that, it fails with BadImageFormatException.
So, the solution is to tell the compiler to ouput code for x86 systems (because most assemblies/librarys usually only support x86 systems). After doing this, the problem should be gone (the x86 produced code should run on 64 bits systems).
参见opencvsharp的F&Qhttp://code.google.com/p/opencvsharp/wiki/FAQ和http://www.broculos.net/en/article/badimageformatexception-was-unhandled-exception)
右击solution,在configuration manager里面 active solution platform新添加:
type填: x86
copysettings from 选为: empty
create new project platform: 不选
选择了新的platform后,编译运行。会在...bin\ 下产生一个新的文件夹x86
此时还会报错,因为还没有在生成可执行文件夹的 路径下添加opencv的dll
5 . 在exe的文件夹中添加
1)把需要的opencv的dll放到exe生成的文件夹下,....bin\x86\Debug
此例只需要opencv_core231.dll和opencv_imgproc231.dll, 如果不知道需要哪个dll就把opencv文件夹中bin下面的dll都拷贝过来。
2)OpenCvSharp.dll确保已经在这个debug文件夹中
3)从opencv文件夹中的 .....\build\common\tbb\ia32\vc10 把tbb的一系列.dll复制过来到这个debug文件夹中
因为opencv_core.dll需要依赖tbb.dll 否则会出现DllNotFoundException
4)本例中,需要一个名为“lenna”的png图片
6. 若出现msvcr100d.dll is missing 的错误
参考http://tech.dir.groups.yahoo.com/group/OpenCV/message/78648
下载上面链接中的文件,放到opencv的bin目录下。
After you extract MSVCR100D_MSVCP100D.rar file,
you must copy MSVCP100D.dll and msvcr100d.dll to C:\OpenCV2.2\bin
then you will never found MSVCP100D.dll and msvcr100d.dll is missing.