OpenCV on November 20 announced the OpenCV-3.4.4 and OpenCV-4.0.0. These versions have a lot of bug fixes and other changes. Release Highlights are as follows:
- OpenCV is now C ++ 11 libraries need to meet the C ++ 11 standard compilers. The minimum required CMake version has been upgraded to 3.5.1.
- Many C API from OpenCV 1.x has been removed.
- Persistent (storage and loading structured data in XML, YAML or JSON) is the core module has been completely re-implemented in C ++, and also lost C API.
- Adding a new module G-API, it can be very effective as based on the image processing pipeline of the graphics engine.
- dnn module now includes experimental Vulkan backend and support network ONNX format.
- Kinect Fusion popular algorithms have been implemented and optimized (the OpenCL) for the CPU and the GPU
the QR code detector and decoder module has been added to objdetect. - DIS highly efficient and high quality dense optical flow algorithm proceeds to opencv_contrib from the video module.
In this article, we will provide a bash script, used on Ubuntu 18.04 installed OpenCV-4.0 (C ++ and Python 3.6). We will also briefly studied to understand the contents of the script. Note that this script will install OpenCV in the local directory, rather than the entire system.
1. Installation OpenCV 4.0
Step 0: Choose the version you want to install OpenCV
1
2
3
|
echo
"OpenCV installation by learnOpenCV.com"
# Define OpenCV Version to install
cvVersion=
"master"
|
We will also clean up the build
directory and create installation
a directory.
1
2
3
|
# Clean build directories
rm
-rf opencv
/build
rm
-rf opencv_contrib
/build
|
1
2
3
|
# Create directory for installation
mkdir
installation
mkdir
installation
/OpenCV-
"$cvVersion"
|
Finally, we will be the current working directory is stored in the cwd
variable. We will in this blog, this directory is referred OpenCV_Home_Dir .
1
2
|
# Save current working directory
cwd=$(
pwd
)
|
Step 1: Update package
1
2
|
sudo
apt -y update
sudo
apt -y upgrade
|
Step 2: Install the OS library
1
2
3
4
Fives
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
thirty
31
32
33
|
sudo
apt -y remove x264 libx264-dev
## Install dependencies
sudo
apt -y
install
build-essential checkinstall cmake pkg-config yasm
sudo
apt -y
install
git gfortran
sudo
apt -y
install
libjpeg8-dev libpng-dev
sudo
apt -y
install
software-properties-common
sudo
apt -y update
sudo
apt -y
install
libjasper1
sudo
apt -y
install
libtiff-dev
sudo
apt -y
install
libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo
apt -y
install
libxine2-dev libv4l-dev
cd
/usr/include/linux
sudo
ln
-s -f ..
/libv4l1-videodev
.h videodev.h
cd
"$cwd"
sudo
apt -y
install
libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo
apt -y
install
libgtk2.0-dev libtbb-dev qt5-default
sudo
apt -y
install
libatlas-base-dev
sudo
apt -y
install
libfaac-dev libmp3lame-dev libtheora-dev
sudo
apt -y
install
libvorbis-dev libxvidcore-dev
sudo
apt -y
install
libopencore-amrnb-dev libopencore-amrwb-dev
sudo
apt -y
install
libavresample-dev
sudo
apt -y
install
x264 v4l-utils
# Optional dependencies
sudo
apt -y
install
libprotobuf-dev protobuf-compiler
sudo
apt -y
install
libgoogle-glog-dev libgflags-dev
sudo
apt -y
install
libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
|
Step 3: Install Python library
1
2
3
|
sudo
apt -y
install
python3-dev python3-pip
sudo
-H pip3
install
-U pip numpy
sudo
apt -y
install
python3-testresources
|
We will also install virtualenv
and virtualenvwrapper
modules to create a virtual Python environment.
1
2
3
4
Fives
6
7
8
9
10
11
12
13
|
cd
$cwd
############ For Python 3 ############
# create virtual environment
python3 -m venv OpenCV-
"$cvVersion"
-py3
echo
"# Virtual Environment Wrapper"
>> ~/.bashrc
echo
"alias workoncv-$cvVersion=\"source $cwd/OpenCV-$cvVersion-py3/bin/activate\""
>> ~/.bashrc
source
"$cwd"
/OpenCV-
"$cvVersion"
-py3
/bin/activate
# now install python libraries within this virtual environment
pip
install
wheel numpy scipy matplotlib scikit-image scikit-learn ipython dlib
# quit virtual environment
deactivate
|
to ease this tutorial, please click on the button below to download and install script. free!
Step 4: Download and opencv_contrib opencv
1
2
3
4
Fives
6
7
8
9
|
git clone https:
//github
.com
/opencv/opencv
.git
cd
opencv
git checkout $cvVersion
cd
..
git clone https:
//github
.com
/opencv/opencv_contrib
.git
cd
opencv_contrib
git checkout $cvVersion
cd
..
|
Step 5: Using the Compiler contrib module and install OpenCV
First, we navigate to the build directory.
1
2
3
|
cd
opencv
mkdir
build
cd
build
|
Next, we began to compile and install process.
1
2
3
4
Fives
6
7
8
9
10
11
|
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=$cwd
/installation/OpenCV-
"$cvVersion"
\
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=$cwd
/OpenCV-
$cvVersion-py3
/lib/python3
.5
/site-packages
\
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=../..
/opencv_contrib/modules
\
-D BUILD_EXAMPLES=ON ..
|
1
2
|
make
-j4
make
install
|
2.如何在C ++中使用OpenCV
使用CMakeLists.txt
CMakeLists.txt的基本结构如下:
1
2
3
4
|
cmake_minimum_required(VERSION 3.1)
# Enable C++11
set
(CMAKE_CXX_STANDARD 11)
set
(CMAKE_CXX_STANDARD_REQUIRED TRUE)
|
您必须设置OpenCV_DIR,如下所示。
1
|
SET(OpenCV_DIR <OpenCV_Home_Dir>
/installation/OpenCV-master/lib/cmake/opencv4
)
|
确保使用正确的路径替换OpenCV_Home_Dir。例如,在我的情况下:
1
|
SET(OpenCV_DIR
/home/hp/OpenCV_installation/installation/OpenCV-master/lib/cmake/opencv4
)
|
完成CMakeLists.txt后,请按照以下步骤操作。
1
2
3
|
mkdir
build &&
cd
build
cmake ..
cmake --build . --config Release
|
这将在构建目录中生成可执行文件。
3.如何在Python中使用OpenCV
要使用使用Python脚本安装的OpenCV版本,首先要激活正确的Python虚拟环境。
对于OpenCV-4:Python 3
1
|
workon OpenCV-master-py3
|
激活虚拟环境后,即可进入Python shell并测试OpenCV版本。
1
2
3
|
ipython
import
cv2
print(cv2.__version__)
|
希望这个脚本证明对你有用:)。请继续关注更多有趣的内容。如有任何疑问,请随时在下方发表评论,我们会尽快回复您。
订阅和下载代码
If you like this article and want to download the code (C ++ and Python) used in this article and sample pictures, please subscribe to our newsletter. You will also receive a free computer vision resource guide. In our newsletter, we shared with OpenCV tutorial and sample C ++ / Python written, and computer vision and machine learning algorithms and news.