打开terminal
第1步 - 更新Ubuntu
$
sudo
apt
-
get
update
$
sudo
apt
-
get
upgrade
第2步 - 安装依赖关系
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install python3.5-dev python3-numpy libtbb2 libtbb-dev
$ sudo apt-get install libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev
第3步 - 获取OpenCV
$ sudo su
$ cd /opt
/opt$ git clone https://github.com/Itseez/opencv.git
/opt$ git clone https://github.com/Itseez/opencv_contrib.git
第4步 - 构建和安装OpenCV
/opt$ cd opencv
/opt/opencv$ mkdir release
/opt/opencv$ cd release
/opt/opencv/release$ cmake -D BUILD_TIFF=ON -D WITH_CUDA=OFF -D ENABLE_AVX=OFF -D WITH_OPENGL=OFF -D WITH_OPENCL=OFF -D WITH_IPP=OFF -D WITH_TBB=ON -D BUILD_TBB=ON -D WITH_EIGEN=OFF -D WITH_V4L=OFF -D WITH_VTK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib/modules /opt/opencv/
/opt/opencv$ make -j4
/opt/opencv$ make install
/opt/opencv$ ldconfig
/opt/opencv$ exit
/opt/opencv$ cd ~
现在要检查一台机器上是否安装了OpenCV,请运行以下命令
$
$
pkg
-
config
--
modversion
opencv
3.&3.2.0
|
建立文件夹
mkdir cpp_test
cd cpp_test
ls
touch main.cpp
getdit main.cpp
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat src = imread("1.jpg");
Mat src1 = src.clone();
imshow("canny",src);
Mat dst, edge, gray;
dst.create(src1.size(),src1.type());//创建与src同类型大小的矩阵
cvtColor(src1,gray,CV_BGR2GRAY);
blur(gray,edge,Size(3,3));//使用3*3内核降噪
Canny(edge,edge,3,9,3);
dst = Scalar::all(0); //将dst内的所有元素设置为0
imshow("边缘检测",edge);
waitKey(0);
return 0;
}
打开cpp_test文件夹,放入一张1.jpg的图像
用下面的命令编译你的代码