本人的系统是Win10 64位,VS版本为2017,OpenCV版本2.49。
手贱把Visual Studio 2012给卸载了,换成了VS2017,但是即便是OpenCV3.20,目前也只自带VC14(VS2015),所以要想用VS2017配置OpenCV的话,就需要自己编译配置。
首先我们先下载OpenCV2.49,下载并解压,比如我解压到了C:\opencv249\opencv,然后下载Cmake Binary Distribution,打开...\cmake-3.8.0-rc3-win64-x64\bin\cmake-gui.exe,对OpenCV249重新进行编译。
(Where to build the binaries)那里选择自己想保存编译文件的位置,我选择的是C:/opencv249/build/vc15
然后点Configure,如果出现如图的红色,就把BUILD_TBB勾选上,再次Configure。
Done之后选Generate即可。成功后应该是界面都是白色的,没有红色的错误提示。然后选择Open Project。
Cmake会自动打开VS2017,运行Project。
项目打开有,选择CMakeTargets下面的INSTALL,右键点击,选择生成(分别在Debug和Release下面生成两次),生成之后就意味着你的OpenCV已经编译成功了。
首先,在系统变量PATH中添加你编译的OpenCV路径,如果按照我的路径的话,是把C:\opencv249\build\vc15\install\lib;加入PATH变量中。
然后把C:\opencv249\build\vc15\install\bin下的所有DLL文件Copy到C:\Windows\SysWOW64以及C:\Windows\System32下面。
接下来我们新建一个Windows控制台程序,选择空项目。
随便Copy一个OpenCV的代码,图片在OpenCV\Sorce\Samples\cpp里面有
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include
using namespace cv;
using namespace std;
static void help()
{
cout << "\nThis program demonstrates circle finding with the Hough transform.\n"
"Usage:\n"
"./houghcircles , Default is pic1.png\n" << endl;
}
int main(int argc, char** argv)
{
const char* filename = argc >= 2 ? argv[1] : "board.jpg";
Mat img = imread(filename, 0);
if(img.empty())
{
help();
cout << "can not open " << filename << endl;
return -1;
}
Mat cimg;
medianBlur(img, img, 5);
cvtColor(img, cimg, COLOR_GRAY2BGR);
vector circles;
HoughCircles(img, circles, CV_HOUGH_GRADIENT, 1, 10,
100, 30, 1, 30 // change the last two parameters
// (min_radius & max_radius) to detect larger circles
);
for( size_t i = 0; i < circles.size(); i++ )
{
Vec3i c = circles[i];
circle( cimg, Point(c[0], c[1]), c[2], Scalar(0,0,255), 3, CV_AA);
circle( cimg, Point(c[0], c[1]), 2, Scalar(0,255,0), 3, CV_AA);
}
imshow("detected circles", cimg);
waitKey();
return 0;
}
点击VS2017菜单栏的项目-xxxxx属性。
在所有配置中配置VC++目录的包含目录和库目录。
其中包含目录添加几个路径:(...\install\include; ...\install\include\opencv; ...\install\include\opencv2)
库目录为C:\opencv249\build\vc15\install\lib
然后左上角选择Debug,配置链接器的附加依赖项。
添加:
opencv_ml249d.lib
opencv_calib3d249d.lib
opencv_contrib249d.lib
opencv_core249d.lib
opencv_features2d249d.lib
opencv_flann249d.lib
opencv_gpu249d.lib
opencv_highgui249d.lib
opencv_imgproc249d.lib
opencv_legacy249d.lib
opencv_objdetect249d.lib
opencv_ts249d.lib
opencv_video249d.lib
opencv_nonfree249d.lib
opencv_ocl249d.lib
opencv_photo249d.lib
opencv_stitching249d.lib
opencv_superres249d.lib
opencv_videostab249d.lib
再选择Release:
添加:
opencv_ml249.lib
opencv_calib3d249.lib
opencv_contrib249.lib
opencv_core249.lib
opencv_features2d249.lib
opencv_flann249.lib
opencv_gpu249.lib
opencv_highgui249.lib
opencv_imgproc249.lib
opencv_legacy249.lib
opencv_objdetect249.lib
opencv_ts249.lib
opencv_video249.lib
opencv_nonfree249.lib
opencv_ocl249.lib
opencv_photo249.lib
opencv_stitching249.lib
opencv_superres249.lib
opencv_videostab249.lib
保存属性配置,在x64平台下编译运行即可。
配置成功,可以使用Visual Studio 2017在x64下运行OpenCV2.49。(其他版本的OpenCV也类似的,利用Cmake编译,配置属性)