opencv安装教程

下载opencv

下载链接:
https://opencv.org/releases/
如: opencv-4.3.0-vc14_vc15.exe

运行exe

运行exe(其实是解压),将压缩包解压到相应目录,如:
D:\
解压后会生成opencv文件夹。
其中build是OpenCV使用时要用到的一些库文件,而sources中则是OpenCV官方为我们提供的一些demo示例源码

Win+VS

添加环境变量

根据安装目录新建环境变量:

此电脑 - 属性 - 高级系统设置 - 高级 - 环境变量 - 系统变量(s) - path - 双击 - 新建 - D:\opencv\build\x64\vc15\bin

测试opencv

新建vs2019工程

如 :Test。

添加静态库和动态库

解决方案管理器 - Test - 右键 - 属性 - 配置属性 - VC++目录 - 平台(x64)

  • 包含目录(Include Directories) - D:\opencv\build\includeD:\opencv\build\include\opencv2
  • 库目录(Library Directories) - D:\opencv\build\x64\vc15\lib

添加动态连接器

打开D:\opencv\build\x64\vc15\lib,会看到里边有连接器opencv_world430d.lib,然后,

解决方案管理器 - Test - 右键 - 属性 - 配置属性 - 链接器 - 输入 - 平台(x64) - 附加依赖项 - opencv_world430d.lib
这一步和include是等价的。

确定。

样例

vs2019 Debug x64 环境

#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main()
{
  // 读入一张图片(游戏原画)
  Mat img = imread("C://Users//Administrator//Desktop//test.jpg");
  if (img.empty())
  {
    std::cout << "No image! Maybe the image path is wrong!" << std::endl;
    int i;
    std::cin >> i;
    return 0;
  }
  // 创建一个名为 "游戏原画"窗口
  namedWindow("游戏原画");
  // 在窗口中显示游戏原画
  imshow("游戏原画", img);
  // 等待6000 ms后窗口自动关闭
  waitKey(0);
}

弹出图片就对了!

Win+MinGW

如果QT用的编译器是MinGW,那么则无法编译.lib后缀的库文件,所以需要准备.a后缀的opencv库文件。opencv244版本带有mingw的库文件,但opencv2410以后则没有了。

安装QT

QT需要的两个模块:
Qt-Qt5.9-MingGW 5.3.0 32 bit
Qt-Tools-MinGW 5.3.0

安装cmkake

安装opencv

步骤如开始部分。

添加环境变量

D:\Qt\Qt5.14.2\Tools\mingw730_64\bin
D:\Qt\Qt5.14.2\5.14.2\mingw73_64\bin

编译opencv

打开cmake

Where is the source code: D:\opencv\sources
where to build the binaries: D:\opencv-build

点击 Configure,Yes,选择下列设定:

Specify the generator for this project: MinGW Makefiles
Specify native compilers, next
Compilers C: D:/Qt/Qt5.14.2/Tools/mingw730_64/bin/gcc.exe
Compilers C++: D:/Qt/Qt5.14.2/Tools/mingw730_64/bin/g++.exe
Finish
set Qt5_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5
Check the box [X]WITH_QT
Check the box [X]WITH_OPENGL

点config

Set QT_MAKE_EXECUTABLE to D:\Qt\5.9\mingw53_32\bin\qmake.exe
Set Qt5Concurrent_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Concurrent
Set Qt5Core_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Core
Set Qt5Gui_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Gui
Set Qt5Test_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Test
Set Qt5Widgets_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Widgets
Set Qt5OpenGL_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5OpenGL
Set CMAKE_BUILD_TYPE to Release or RelWithDebInfo
Set OPENCV_VS_VERSIONINFO_SKIP=1
Uncheck the box []ENABLE_PRECOMPILED_HEADERS

点config

点generate

然后编译:

d:
cd d:\
cd opencv-build  
mingw32-make -j 8
mingw32-make install

mingw32-make -j 8时如果编译出错:

gcc: error: long: No such file or directory
C:\mingw64\bin\windres.exe: preprocessing failed.
mingw32-make[2]: *** [modules\core\CMakeFiles\opencv_core.dir\build.make:1506: modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1
mingw32-make[2]: *** Waiting for unfinished jobs....
mingw32-make[1]: *** [CMakeFiles\Makefile2:2237: modules/core/CMakeFiles/opencv_core.dir/all] Error 2
mingw32-make: *** [Makefile:179: all] Error 2

解决方法,在cmake选项中取消该选项:

Uncheck the box []OPENCV_ENABLE_ALLOCATOR_STATS

添加Opencv编译库到环境变量

D:\opencv-build\install\x86\mingw\bin

测试

.pro文件中添加,注意修改文件路径和库名:

INCLUDEPATH += D:\opencv\build\include
LIBS += D:\opencv-build\bin\libopencv_*.dll



#LIBS += D:\opencv-build\bin\libopencv_core320.dll
#LIBS += D:\opencv-build\bin\libopencv_highgui320.dll
#LIBS += D:\opencv-build\bin\libopencv_imgcodecs320.dll
#LIBS += D:\opencv-build\bin\libopencv_imgproc320.dll
#LIBS += D:\opencv-build\bin\libopencv_features2d320.dll
#LIBS += D:\opencv-build\bin\libopencv_calib3d320.dll

# more correct variant, how set includepath and libs for mingw
# add system variable: OPENCV_SDK_DIR=D:/opencv/opencv-build/install
# read http://doc.qt.io/qt-5/qmake-variable-reference.html#libs

#INCLUDEPATH += $$(OPENCV_SDK_DIR)/include

#LIBS += -L$$(OPENCV_SDK_DIR)/x86/mingw/lib \
#        -lopencv_core320        \
#        -lopencv_highgui320     \
#        -lopencv_imgcodecs320   \
#        -lopencv_imgproc320     \
#        -lopencv_features2d320  \
#        -lopencv_calib3d320

main.cpp

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

int main()
{
    // read an image
    cv::Mat image = cv::imread("C://Users//Administrator//Desktop//timg.jpg", 1);
    // create image window named "My Image"
    cv::namedWindow("My Image");
    // show the image on window
    cv::imshow("My Image", image);
    cv::waitKey(0);
    return 0;
}

MAC+Qt

Mac下只能使用Xcode进行构建。

可以使用brew install opencv进行安装opencv,但是github总是被墙,因此使用码云上的opencv源码来构建opencv。

下载opencv源码,打开cmake:

Where is the source code: /Users/cdq/thirdLibrary/opencv
where to build the binaries: /Users/cdq/thirdLibrary/opencv-build

点击config

再点击config

点generate

cd /Users/cdq/thirdLibrary/opencv
make -j 8
sudo make install

安装后的位置是: /usr/local/include,/usr/local/lib,/usr/local/share和/usr/local/bin

.pro文件

INCLUDEPATH +=/usr/local/include/opencv4/
LIBS += /usr/local/lib/libopencv_*.dylib

#LIBS += /usr/local/lib/libopencv_core.dylib
#LIBS += /usr/local/lib/libopencv_highgui.dylib
#LIBS += /usr/local/lib/libopencv_imgcodecs.dylib
#LIBS += /usr/local/lib/libopencv_imgproc.dylib

main.cpp文件

#include <QApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;  //包含cv命名空间

int main( )
{
 Mat srcImage = imread("/Users/cdq/Desktop/124.jpg"); //载入图像
 imshow("【原始图】",srcImage);//显示图像
 waitKey(0);//按任意键退出
 return 0;
}

需要注意的是:运行成功后,图片总是以最小化显示,而不是直接弹出,导致我一度以为没有正常运行!

参考链接:
https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows
https://blog.csdn.net/duwangthefirst/article/details/79452314
https://blog.csdn.net/maizousidemao/article/details/81474834
https://blog.csdn.net/u012435142/article/details/82952302

posted @ 2020-05-11 23:21  多弗朗强哥  阅读(4163)  评论(1编辑  收藏  举报