1- CV- OpenCV环境配置
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-llfF0OBv-1595731104649)(https://opencv.org/wp-content/uploads/2020/07/cropped-OpenCV_logo-1.png)]
Windows 环境配置
Step.1 下载source
- 官网 https://github.com/opencv/opencv/archive/4.4.0.zip
sourceforge.net
github
Step.2 查看和配置
-
查看目录结构
-
查看
x64
版本对应Visual Studio
的版本Visual Studio版本号 vc版本号 Visual Studio 6
vc6
Visual Studio 2003
vc7
Visual Studio 2005
vc8
Visual Studio 2008
vc9
Visual Studio 2010
vc10
Visual Studio 2012
vc11
Visual Studio 2013
vc12
Visual Studio 2015
vc14
Visual Studio 2017
vc15
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-W1qKdW4C-1595731104652)(http://free-cn-01.cdn.bilnn.com/ddimg/jfs/t1/116977/18/13006/68427/5f184bbdE25c82368/f0b15f854439211d.png)]
Step.3 在环境变量路径添加
-
打开环境变量设置,界面如下:
-
将
cv
路径添加到系统变量 -
在某些情况需要将缺少的
dll
拷贝至路径C:\Windows\System32
-
由于找不到opencv_world410d.dll,无法执行代码,重新安装程序可能会解决此问题
-
配置VS项目
-
VS
中VC++
目录Include Directory
和Library Directory
对其进行配置Include Directory
build\include
build\include\opencv
build\include\opencv2
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Bkf5VZHG-1595731104657)(http://free-cn-01.cdn.bilnn.com/ddimg/jfs/t1/129540/27/7666/76130/5f185818E92bf9a34/c436d989704c6252.png)]
Library Directory
C:\opencv3410\opencv\build\x64\vc15\lib
\
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-c1XOnNmC-1595731104658)(http://free-cn-01.cdn.bilnn.com/ddimg/jfs/t1/119908/36/7771/76352/5f185818Eddc470a5/23ff68c84e609258.png)]
-
【通用属性】 - > 【链接器】-> 【输入】-> 【附加依赖项】
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d15TmGV4-1595731104659)(http://free-cn-01.cdn.bilnn.com/ddimg/jfs/t1/128536/9/7736/63704/5f185818E4b806b48/08121c3f8b002b83.png)]
-
测试代码
#include <opencv2/opencv.hpp>
#include <iostream>
void read_image(const char* path);
int main(int argv, char* args[])
{
cv::Mat image;
read_image("E:\\vs_series\\normal\\cv_test\\data\\sample\\ballon.jpg");
return 0;
}
void read_image(const std::string path)
{
using namespace cv;
Mat image;
image = imread(path);
if (!image.data)
{
std::cout << "image created failed." << std::endl;
return;
}
std::string win_name = "Original Image";
namedWindow(win_name,2);
imshow(win_name,image);
waitKey(0);
}
Linux下环境配置
如果需要卸载之前版本
make uninstall
cd ..
rm -rf build
rm -rf /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
解压下载的安装包
unzip opencv-3.4.0.zip
安装所需要的依赖
$ sudo apt-get install cmake libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff5-dev libdc1394-22-dev # 处理图像所需的包
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
$ sudo apt-get install libxvidcore-dev libx264-dev # 处理视频所需的包
$ sudo apt-get install libatlas-base-dev gfortran # 优化opencv功能
$ sudo apt-get install ffmpeg
编译
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make
安装
make install
step3 配置OpenCV环境
还需要为程序指定openvc的头文件位置,这里使用pkg-config命令来完成。
1、进入/etc/profile配置文件,在文件末尾追加添加以下命令:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
2.添加完后更新配置文件
source profile
3.查看版本号,验证是否安装成功
pkg-config --modversion opencv
编写测试
编写cpp文件
#include <iostream>
#include<opencv2/opencv.hpp>
void read_image(std::string path);
void write_image(std::string path,int type);
int main(int argv,char* args[])
{
using namespace cv;
Mat src;
std::string path = "ubuntu_test.jpg";
src=imread("ubuntu_test.jpg");
imshow("image test",src);
read_image(path);
write_image(path,-1);
waitKey(0);
return 0;
}
void read_image(std::string path)
{
using namespace cv;
Mat image;
std::cout << "size." << image.size().height << ","<< image.size().width <<"."<<std::endl;
image = imread(path);
if(!image.data)
{
// 圖像未被創建
std::cout << "image not existed."<< std::endl;
return;
}
namedWindow("Original Image");
imshow("Original Image",image);
}
void write_image(std::string path,int type)
{
using namespace cv;
Mat image;
std::cout << "size." << image.size().height << ","<< image.size().width <<"."<<std::endl;
image = imread(path);
if(!image.data)
{
// 圖像未被創建
std::cout << "image not existed."<< std::endl;
return;
}
Mat result,cpy_image;
image.copyTo(cpy_image);
switch (type)
{
case -1:
flip(cpy_image,result,-1);
break;
case 0:
flip(cpy_image,result,0);
break;
case 1:
flip(cpy_image,result,1);
break;
default:
flip(cpy_image,result,-1);
break;
}
std::string name = "Output_Image_"+std::to_string(type);
namedWindow(name);
imshow(name,result);
imwrite(name+".jpg",result);
}
编写cmakelists.txt文件
CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
PROJECT ( demo )
set(CMAKE_CXX_STANDARD 11)
FIND_PACKAGE(OpenCV REQUIRED)
ADD_EXECUTABLE(demo test.cpp)
TARGET_LINK_LIBRARIES(demo ${OpenCV_LIBS})
编译、查看结果
cmake .
make