VS2017 + cmake 3.7 + opencv 3.2 編譯
由於各種原因使用了VS2010不支援的功能,需要在VS2017上使用opencv,且opencv release 沒有 vc15(VS2017需要的),所以需要編譯opencv 固有此文
本文參考:
http://blog.csdn.net/maize1111/article/details/68923677
http://blog.csdn.net/jarvischu/article/details/8468894
電腦配置: Win10 64bit
1. 在opencv官網上下載 open3.2 的source檔案, copy到D盤 解壓縮 D:\opencv-3.2.0,在 D:\opencv-3.2.0 資料夾新建 build/vc15
2. 開啟Cmake (電腦上沒安裝的去官網下載安裝)
在source code 和 build 分別browse到路徑,其中source code是含有Cmakelist文字檔案的那個資料夾,build是我們剛剛建好的空資料夾
輸好路徑之後,點configure,選Visual Studio 15,確認,等待幾分鐘,如果沒有錯誤則點generate,再沒有錯誤就 open project。
我一開始用Visual Studio 15 + opencv2.4做的時候最後報錯版本不匹配,後來換成3.2版本就沒有問題了
3. openproject 開啟VS2017後,在左欄專案檔案下面 選擇CMakeTargets下面的INSTALL,右鍵點選,選擇build生成(分別在Debug和Release下面生成兩次),生成之後就意味著你的OpenCV已經編譯成功了
首先,在系統變數PATH中新增你編譯的OpenCV路徑,D:\opencv-3.2.0\opencv-3.2.0\build\vc15\install\x86\vc14\lib 加入PATH變數中。
然後把D:\opencv-3.2.0\opencv-3.2.0\build\vc15\install\x86\vc14\bin下的所有DLL檔案Copy到C:\Windows\SysWOW64以及C:\Windows\System32下面。
4. 之後開始配置 VS 專案屬性
點project - 選最後一個 '屬性',分別在debug 和 release 選項下配置,debug和release 的配置過程基本相同,唯一的不同點是新增附加依賴項的時候檔名相差一個字母
4.1 配置包含檔案
D:\opencv-3.2.0\opencv-3.2.0\build\vc15\install\include
D:\opencv-3.2.0\opencv-3.2.0\build\vc15\install\include\opencv
D:\opencv-3.2.0\opencv-3.2.0\build\vc15\install\include\opencv2
4.2 配置庫檔案
D:\opencv-3.2.0\opencv-3.2.0\build\vc15\install\x86\vc14\lib
4.3 配置附加依賴項
4.3.1 Debug下面
複製 下面的內容, 此處應注意,確認如下檔名是否可以在 D:\opencv-3.2.0\opencv-3.2.0\build\vc15\install\x86\vc14\lib 資料夾下被找到,因為opencv版本不同,檔名也不一樣,填寫內容應與lib資料夾下檔名匹配
opencv_ml320d.lib
opencv_calib3d320d.lib
opencv_core320d.lib
opencv_features2d320d.lib
opencv_flann320d.lib
opencv_highgui320d.lib
opencv_imgcodecs320d.lib
opencv_imgproc320d.lib
opencv_objdetect320d.lib
opencv_video320d.lib
opencv_photo320d.lib
opencv_shape320d.lib
opencv_stitching320d.lib
opencv_superres320d.lib
opencv_videostab320d.lib
opencv_videoio320d.lib
4.3.2 Release下面
同理 複製
opencv_ml320.lib
opencv_calib3d320.lib
opencv_core320.lib
opencv_features2d320.lib
opencv_flann320.lib
opencv_highgui320.lib
opencv_imgcodecs320.lib
opencv_imgproc320.lib
opencv_objdetect320.lib
opencv_video320.lib
opencv_photo320.lib
opencv_shape320.lib
opencv_stitching320.lib
opencv_superres320.lib
opencv_videostab320.lib
opencv_videoio320.lib
確認 所有對話方塊
5. 測試
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
static void help()
{
cout << "\nThis program demonstrates circle finding with the Hough transform.\n"
"Usage:\n"
"./houghcircles <image_name>, 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<Vec3f> 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;
}
注意,要保證專案資料夾下已經拷貝名為board.jpg
的圖片,否則執行結果顯示找不到圖片
成功!