opencv新手入门之环境搭建

港控/mmm° 2023-06-30 09:28 127阅读 0赞

文章目录

  • Windows
  • win10 + vs2019 的opencv配置
    • 环境测试
    • vs的部分优化设置
  • 代码部分
      1. 双边滤波
      1. 色彩空间转化
      1. 颜色通道分离与混合
    • nvida测试安装
  • Linux
    • 背景
    • 安装
        1. 下载源码
      • 2.安装依赖项
        1. 编译
        1. 安装
    • 可能出现的报错
    • 测试是否安装成功
    • python如何安装

Windows

win10 + vs2019 的opencv配置

  1. 下载opencv
  2. 解压到某文件夹,比如:C:\Program Files\SoftwareDevelopment\OpenCV4.2.0
  3. C:\Program Files\SoftwareDevelopment\OpenCV4.2.0\opencv\build\x64\vc15\bin加到环境变量
  4. 开始vs的配置
  5. format_png
  6. format_png 1
  7. format_png 2
  8. 重启,开启一轮cv测试

环境测试

test code:

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<opencv2/opencv.hpp>
  4. using namespace std;
  5. using namespace cv;
  6. int main() {
  7. Mat image_in = imread("1.png");
  8. namedWindow("show_imput_image", 1);
  9. imshow("show_input_image", image_in);
  10. waitKey(0);
  11. printf("aaaaaa");
  12. return 0;
  13. }

result:
format_png 3

vs的部分优化设置

  • 字体调整 :设置在工具栏视窗下
  • 代码格式化 : ctrl k + d
  • Ctrl+K,C: 注释选定内容 (Comment)
  • Ctrl+K,U: 取消选定注释内容 (UnComment)
  • Ctrl+J /Ctrl+K,L: 智能提示 列出成员 (kernel核心内容 list列表 如果我们想查看一个对象具有的成员具体信息的时候试下这个快捷键吧)
  • Ctrl+K,P: 参数信息 (kernel核心内容 Parameters参数 如果我们想查看一个方法的具体参数的时候这个组合键可是挺有用的哦)
  • Ctrl+K,I: 快速查看信息(Infomation)

代码部分

1. 双边滤波

  • 功能
  • bilateralFilter can reduce unwanted noise very well while keeping edges fairly sharp. However, it is very slow compared to most filters.

  • Sigma values: For simplicity, you can set the 2 sigma values to be the same. If they are small (< 10), the filter will not have much effect, whereas if they are large (> 150), they will have a very strong effect, making the image look “cartoonish”.

  • Filter size: Large filters (d > 5) are very slow, so it is recommended to use d=5 for real-time applications, and perhaps d=9 for offline applications that need heavy noise filtering.

数学原理

  • cpp API

    void cv::bilateralFilter ( InputArray src,

    1. OutputArray dst,
    2. int d,
    3. double sigmaColor,
    4. double sigmaSpace,
    5. int borderType = BORDER_DEFAULT

    )

  • python API

    dst = cv.bilateralFilter( src, d, sigmaColor, sigmaSpace[, dst[, borderType]] )

  • 参数解释

    Parameters

    1. src Source 8-bit or floating-point, 1-channel or 3-channel image.
    2. dst Destination image of the same size and type as src .
    3. d Diameter of each pixel neighborhood that is used during filtering. If it is non-positive, it is computed from sigmaSpace.
    4. sigmaColor Filter sigma in the color space. A larger value of the parameter means that farther colors within the pixel neighborhood (see sigmaSpace) will be mixed together, resulting in larger areas of semi-equal color.
    5. sigmaSpace Filter sigma in the coordinate space. A larger value of the parameter means that farther pixels will influence each other as long as their colors are close enough (see sigmaColor ). When d>0, it specifies the neighborhood size regardless of sigmaSpace. Otherwise, d is proportional to sigmaSpace.
    6. borderType border mode used to extrapolate pixels outside of the image, see BorderTypes
  • 实例代码

    // 双边滤波

    1. Mat bilateral_image_out;
    2. bilateralFilter(image_in, bilateral_image_out, 25, 25 * 2, 25 / 2);
  1. namedWindow("bilateral_image", 1);
  2. imshow("bilateral_image", bilateral_image_out);

2. 色彩空间转化

RGB是最常见的颜色,因为我们的眼睛使用类似的颜色,但是请记住,OpenCV标准显示系统使用BGR颜色空间来组合颜色(红色和蓝色通道已互换位置)

  1. void cv::cvtColor ( InputArray src,
  2. OutputArray dst,
  3. int code,
  4. int dstCn = 0
  5. )
  6. Python:
  7. dst = cv.cvtColor( src, code[, dst[, dstCn]] )

3. 颜色通道分离与混合

分离&&合并

  1. // 定义一个容器
  2. vector<Mat> channels_lab;
  3. // 分离通道
  4. split(image_lab, channels_lab);
  5. // 提取单个通道
  6. Mat l_LAB = channels_lab.at(0);
  7. Mat a_LAB = channels_lab.at(1);
  8. Mat b_LAB = channels_lab.at(2);
  9. imshow("l", l_LAB);
  10. imshow("a", a_LAB);
  11. imshow("b", b_LAB);
  12. // 调整通道
  13. // 合并通道
  14. Mat mergeImage;
  15. merge(channels_lab, mergeImage);
  16. imshow("merge", mergeImage);

还可以通过这个函数

  1. Mat lab,L
  2. // L = lab.at(0)
  3. extractChannel(lab, L, 0);

nvida测试安装

  1. 连接电源
  2. 连接显示器
  3. 配上鼠标和键盘
  4. 按下S4(POWER BTN)开机键
  5. 账号密码:nvida

Linux

背景

我使用的是cmake编译,编辑器用的是轻量级的vscode,平台是在ubuntu18.04。当然我在为windows下也进行学习,不过因为毕业设计是基于linux平台的开发,所以我下来这个opencv系列的记录会比较着重讲解linux系统下的开发,不过opencv是跨平台的,各个平台只是编译方法略有差别,代码和思维都是一样的。

安装

大家英语好的话可以直接参考官网安装指南 opencv官网linux的c++版opencv安装指南


1. 下载源码

  1. 第一步;官网下载opencv的源码

这里需要说明下:
点击github会去opencv的github主页,在哪里不仅可以下载opencv的各个版本,各个平台的发行版,还可以下载opencv目前还没有纳入主版本的增强库

在这里插入图片描述
在这里插入图片描述

这里需要说明下就是我们国区访问github和opencv官网因为制度的原因都是非常慢的,如果遇到kb级别的下载速度别着急

    1. 科学xx解决
    2. 等待(比较漫长,中途还会失败需要手动重新开始)
    3. 下载别上下载好上传到百度云盘的包(上网搜索自己需要的版本)

2.安装依赖项

  1. [必要包]
  2. GCC 4.4.x or later
  3. CMake 2.8.7 or higher
  4. Git
  5. GTK+2.x or higher, including headers (libgtk2.0-dev)
  6. pkg-config
  7. Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
  8. ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
  9. [可选包] libtbb2 libtbb-dev
  10. [可选包] libdc1394 2.x
  11. [可选包] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
  12. [可选包] CUDA Toolkit 6.5 or higher

命令行跑下面代码即可:

  1. sudo apt-get install build-essential
  2. sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
  3. sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

3. 编译

在这里插入图片描述

  1. 首先解压后的安装包后,进入安装包,然后执行下面命令

我们在源码下新建一个build文件夹,然后在这个文件夹里面编译源码,这样编译过程中的中间文件就会生成在这个目录下,方便我们管理,项目结构更加清洁!

  1. cd ~/opencv
  2. mkdir build
  3. cd build
  1. 然后执行一下命令来进行开始构建项目

这一步会耗费很长的时间,中间需要下载一个CPU还是GPU的加速包源,可能会卡住,有科学上网加持会快很对,如果没有卡住就让他多等一会,或者我们也可也单独下载离线包,然后修改文件,这个可以在网上单独搜索以下,这里就不贴出来了

  1. cmake -D CMAKE_BUILD_TYPE=Release
  2. -D CMAKE_INSTALL_PREFIX=/usr/local
  3. ..

参数解释

  1. # 编译成发行版还是调试版,一般选择Release即可
  2. CMAKE_BUILD_TYPE=Release\Debug
  3. # 最后安装在/usr/local目录下
  4. -D CMAKE_INSTALL_PREFIX=/usr/local ..
  5. # 增强包的路径,如果有就填写,没有可以不写
  6. -D OPENCV_EXTRA_MODULES_PATH=/home/cds/Downloads/opencv41/opencv_contrib-4.1.2/modules

4. 安装

这个j后面的数字,说的是并发执行的线程数,看自己电脑是几核几线程,不知道就填4把、

  1. 先执行

编译工程比较浩大,编译速度取决与CPU计算能力,我一般要1个小时

  1. sudo make -j4
  1. 执行安装
    sudo make install

安装速度很快

如果在make -j4的过程中出现错误,不用担心,根据错误原因在控制台解决,再次编译,会随着上一次卡住的进度,继续编译,不会重新开始的!

可能出现的报错

采用的是源码编译的方式,所以可以查看 build 文件夹下的日志文件 CMakeDownloadLog.txt,在日志文件CMakeDownloadLog.txt中搜索 boostdesc_bgm.i 关键词 (不是在文件夹中搜索),
发现这个文件下载失败了。日志文件里就有它的下载地址,直接复制其下载地址到网页可以看该到文件的源码,直接拷贝源码并生存同名文件,放在 opencv_contrib/modules/xfeatures2d/src/ 路径下即可。

测试是否安装成功

找到 opencv/samples/cpp/example_cmake 目录下,官方已经给出了一个cmake的example,我们可以拿来测试下。按顺序执行:

  1. cmake .
  2. make
  3. ./opencv_example

即可看到打开了摄像头,在左上角有一个hello opencv ,即表示配置成功。

像这样——-原谅我的大红马赛克

\[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-f2Jv7hgP-1579233431004)(/home/cds/.config/Typora/typora-user-images/image-20200117115122549.png)\]

我在这里贴出操作的历史纪录供大家参考

  1. cds@cds-TN15S:~/Downloads/Compressed/opencv-4.2.0/samples/cpp/example_cmake$ pwd/home/cds/Downloads/Compressed/opencv-4.2.0/samples/cpp/example_cmake
  2. cds@cds-TN15S:~/Downloads/Compressed/opencv-4.2.0/samples/cpp/example_cmake$ cmake .
  3. -- The C compiler identification is GNU 7.5.0
  4. -- The CXX compiler identification is GNU 7.5.0
  5. -- Check for working C compiler: /usr/bin/cc
  6. -- Check for working C compiler: /usr/bin/cc -- works
  7. -- Detecting C compiler ABI info
  8. -- Detecting C compiler ABI info - done
  9. -- Detecting C compile features
  10. -- Detecting C compile features - done
  11. -- Check for working CXX compiler: /usr/bin/c++
  12. -- Check for working CXX compiler: /usr/bin/c++ -- works
  13. -- Detecting CXX compiler ABI info
  14. -- Detecting CXX compiler ABI info - done
  15. -- Detecting CXX compile features
  16. -- Detecting CXX compile features - done
  17. -- Found OpenCV: /usr/local (found version "4.2.0")
  18. -- OpenCV library status:
  19. -- config: /usr/local/lib/cmake/opencv4
  20. -- version: 4.2.0
  21. -- libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio
  22. -- include path: /usr/local/include/opencv4
  23. -- Configuring done
  24. -- Generating done
  25. -- Build files have been written to: /home/cds/Downloads/Compressed/opencv-4.2.0/samples/cpp/example_cmake
  26. cds@cds-TN15S:~/Downloads/Compressed/opencv-4.2.0/samples/cpp/example_cmake$ make
  27. Scanning dependencies of target opencv_example
  28. [ 50%] Building CXX object CMakeFiles/opencv_example.dir/example.cpp.o
  29. [100%] Linking CXX executable opencv_example
  30. [100%] Built target opencv_example
  31. cds@cds-TN15S:~/Downloads/Compressed/opencv-4.2.0/samples/cpp/example_cmake$ ./opencv_example
  32. Built with OpenCV 4.2.0
  33. Capture is opened
  34. ^C

python如何安装

如果使用的是anconda的话。直接执行conda install py-opencv即可,如果是装的python的话/执行pip install py-opencv
测试方法

发表评论

表情:
评论列表 (有 0 条评论,127人围观)

还没有评论,来说两句吧...

相关阅读