最近由于数电项目需要OpenCV便在Window上装OpenCV,网上多是VS来编译,但是我试过VS2015和2010效果,,,贼烂而且还没成功,,,只成功了几个模块。
后来改用CMake和MinGW(QT带的)就行,而且相对来说又快又好,这搭配简直perfect。
由于时间有限排版很渣,有空再整理一下。进入正题:
系统环境:Windows10 64bit
首先准备好软件:
QT5.8(GCC5.3)
http://mirrors.ustc.edu.cn/qtproject/archive/qt/5.8/5.8.0/qt-opensource-windows-x86-mingw530-5.8.0.exe
把安装目录下的Tools/mingw*/bin添加到path中
或者MinGW5.4(GCC6.3)
http://kent.dl.sourceforge.net/project/mingw-w64/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/6.3.0/threads-win32/seh/x86_64-6.3.0-release-win32-seh-rt_v5-rev1.7z
解压把里面的bin添加到系统path中
OpenCV3.2
https://nchc.dl.sourceforge.net/project/opencvlibrary/opencv-win/3.2.0/opencv-3.2.0-vc14.exe
CMake
https://cmake.org/files/v3.8/cmake-3.8.0-rc1-win64-x64.msi
安装CMake的的时候,注意安装路径不能有中文和空格, 在安装过程中,勾选加入所有环境变量(),把安装目录下的bin文件夹路径添加到path中
接下来编译OpenCV
1.生成MakeFile
打开cmake-gui,设置源码和编译文件夹
opencv解压文件夹里就有这两个文件夹,不用再乱搞,build文件夹还有文件的,千万不要自己建一个
接着点击Configure
如上图然后点Next设置编译器路径
分别在QT_INSTALL_DIR/Tools/mingw8/bin下的gcc和g++,最后finish
可能会出现失败,是因为没有设置make程序的路径,直接搜索设置值
重新点Configure,完了后取消勾选WITH_IPP,直接搜索取消掉就行
然后Generate就好了,不过期间会下载ffmpeg和ippicv又大又慢,,,所以我把路径抠出来下载好直接放进去就行,已经上传
ffmpeg
http://download.csdn.net/detail/desirev/9762198
ippicv
http://download.csdn.net/detail/desirev/9762350
CMake Generate时显示缺少组件的话
libwinpthread-1.dll
http://99idc.jb51.net:81/dll/2014/libwinpthread-1(jb51.net).rar
复制到C:/Windows/system32和SysWOW64文件夹中,把下面粘贴到一个文本中把后缀名改为.bat,右键以管理员运行即可
@echo 开始注册
regsvr32 %windir%\SysWOW64\libwinpthread-1.dll /s
regsvr32 %windir%\system32\libwinpthread-1.dll /s
@echo libwinpthread-1.dll注册成功
@pause
准备好以后进入build目录,shift+右键以管理员打开cmd窗口,运行mingw-make
千万不要-j多任务编译,,,我就作死试了然后各种错。。。其实单任务编译也没多久,半小时以内解决一般。
如果出错了,进入source\opencv-3.0.0-beta\sources\cmake 下找到 OpenCVCompilerOptions.cmake
搜索
add_extra_compiler_option(-Werror=non-virtual-dtor)
注释掉
#add_extra_compiler_option(-Werror=non-virtual-dtor)
make完后mingw-make install即可,安装目录默认在build目录下install
最后将build/bin的绝对路径添加到Path中,至此OpenCV安装完毕。
之前在Linux下用过CLion,非常好用,故想在Windoows上装CLion,而且CLion完美支持各种代码,Java本命语言支持最好,,,Python也很棒。
CLion
https://download.jetbrains.8686c.com/cpp/CLion-2016.3.3.exe
先説説QT项目配置OpenCV
在pro文件中写入
INCLUDEPATH+=D:/Win10Soft/opencv/build/include
LIBS += -L D:/Win10Soft/opencv/build/lib/libopencv_*.a
CMakeFiles\HW2.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/nkdzt/CLionProjects/HW2/main.cpp:19: undefined reference to `cvLoadImage'
C:/Users/nkdzt/CLionProjects/HW2/main.cpp:21: undefined reference to `cvNamedWindow'
C:/Users/nkdzt/CLionProjects/HW2/main.cpp:23: undefined reference to `cvShowImage'
C:/Users/nkdzt/CLionProjects/HW2/main.cpp:25: undefined reference to `cvWaitKey'
C:/Users/nkdzt/CLionProjects/HW2/main.cpp:27: undefined reference to `cvDestroyWindow'
C:/Users/nkdzt/CLionProjects/HW2/main.cpp:29: undefined reference to `cvReleaseImage'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\HW2.dir\build.make:96: recipe for target 'HW2.exe' failed
mingw32-make.exe[3]: *** [HW2.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/HW2.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/HW2.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/HW2.dir/rule] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/HW2.dir/rule' failed
Makefile:117: recipe for target 'HW2' failed
mingw32-make.exe: *** [HW2] Error 2
cmake_minimum_required(VERSION 3.6)
project(HW2)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Where to find CMake modules and OpenCV
set(OpenCV_DIR "D:\\Win10Soft\\opencv\\build\\install")
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}"\\cmake")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(HW2 main.cpp)
# add libs you need
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
# linking
target_link_libraries(HW2 ${OpenCV_LIBS})
其中的路径根据需要自己修改
测试代码如下:main.cpp
#include "opencv/cv.h"
#include "opencv/highgui.h"
int main(int argc, char *argv[])
{
//声明IplImage指针
IplImage *pImg;
//载入图片
pImg = cvLoadImage("C:\\Users\\nkdzt\\Pictures\\Camera Roll\\79647.jpg", 1);
//创建窗口
cvNamedWindow("Image",1);
//显示图像
cvShowImage("Image",pImg);
//等待按键
cvWaitKey(0);
//销毁窗口
cvDestroyWindow("Image");
//释放图像
cvReleaseImage(&pImg);
return 0;
}