文章目录

  • Windows
  • win10 + vs2019 的opencv配置
  • 环境测试
  • vs的部分优化设置
  • 代码部分
  • 1. 双边滤波
  • 2. 色彩空间转化
  • 3. 颜色通道分离与混合
  • nvida测试安装
  • Linux
  • 背景
  • 安装
  • 1. 下载源码
  • 2.安装依赖项
  • 3. 编译
  • 4. 安装
  • 可能出现的报错
  • 测试是否安装成功
  • 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. 重启,开启一轮cv测试

环境测试

test code:

#include<iostream>
#include<stdio.h>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main() {
	Mat image_in = imread("1.png");
	namedWindow("show_imput_image", 1);
	imshow("show_input_image", image_in);
	waitKey(0);
	printf("aaaaaa");
	return 0;
}

result:

opencv 创建蒙板 opencv搭建环境_opencv

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,
								OutputArray 	dst,
								int 	d,
								double 	sigmaColor,
								double 	sigmaSpace,
								int 	borderType = BORDER_DEFAULT 
)
  • python API
dst	=	cv.bilateralFilter(	src, d, sigmaColor, sigmaSpace[, dst[, borderType]]	)
  • 参数解释
Parameters
			src	Source 8-bit or floating-point, 1-channel or 3-channel image.
			dst	Destination image of the same size and type as src .
			d	Diameter of each pixel neighborhood that is used during filtering. If it is non-positive, it is computed from sigmaSpace.
			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.
			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.
			borderType	border mode used to extrapolate pixels outside of the image, see BorderTypes
  • 实例代码
// 双边滤波
	Mat bilateral_image_out;
	bilateralFilter(image_in, bilateral_image_out, 25, 25 * 2, 25 / 2);

	
	namedWindow("bilateral_image", 1);
	imshow("bilateral_image", bilateral_image_out);

2. 色彩空间转化

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

void cv::cvtColor	(	InputArray 	src,
                        OutputArray 	dst,
                        int 	code,
                        int 	dstCn = 0 
)		
Python:
dst	=	cv.cvtColor(	src, code[, dst[, dstCn]]	)

3. 颜色通道分离与混合

分离&&合并

// 定义一个容器
	vector<Mat> channels_lab;
	// 分离通道
	split(image_lab, channels_lab);
	// 提取单个通道
	Mat l_LAB = channels_lab.at(0);
	Mat a_LAB = channels_lab.at(1);
	Mat b_LAB = channels_lab.at(2);
	imshow("l", l_LAB);
	imshow("a", a_LAB);
	imshow("b", b_LAB);
	// 调整通道

	// 合并通道
	Mat mergeImage;
	merge(channels_lab, mergeImage);
	imshow("merge", mergeImage);

还可以通过这个函数

Mat lab,L
    // L = lab.at(0)
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.安装依赖项

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

命令行跑下面代码即可:

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

3. 编译

opencv 创建蒙板 opencv搭建环境_c++_02

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

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

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

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

cmake -D CMAKE_BUILD_TYPE=Release
 -D CMAKE_INSTALL_PREFIX=/usr/local
  ..

参数解释

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

4. 安装

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

  1. 先执行

编译工程比较浩大,编译速度取决与CPU计算能力,我一般要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,我们可以拿来测试下。按顺序执行:

cmake .
make
./opencv_example

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

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

opencv 创建蒙板 opencv搭建环境_c++_03

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

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
cds@cds-TN15S:~/Downloads/Compressed/opencv-4.2.0/samples/cpp/example_cmake$ cmake .
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local (found version "4.2.0") 
-- OpenCV library status:
--     config: /usr/local/lib/cmake/opencv4
--     version: 4.2.0
--     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
--     include path: /usr/local/include/opencv4
-- Configuring done
-- Generating done
-- Build files have been written to: /home/cds/Downloads/Compressed/opencv-4.2.0/samples/cpp/example_cmake
cds@cds-TN15S:~/Downloads/Compressed/opencv-4.2.0/samples/cpp/example_cmake$ make
Scanning dependencies of target opencv_example
[ 50%] Building CXX object CMakeFiles/opencv_example.dir/example.cpp.o
[100%] Linking CXX executable opencv_example
[100%] Built target opencv_example
cds@cds-TN15S:~/Downloads/Compressed/opencv-4.2.0/samples/cpp/example_cmake$ ./opencv_example 
Built with OpenCV 4.2.0
Capture is opened
^C

python如何安装

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