Install openCV 4.1 on Ubuntu 19.10

Linux作業系統安裝、設定與管理 (CentOS、Ubuntu)
回覆文章
inho
文章: 69
註冊時間: 2017-12-08, 23:00

Install openCV 4.1 on Ubuntu 19.10

文章 inho » 2019-10-22, 23:09

use the next post ....
最後由 inho 於 2019-10-24, 12:41 編輯,總共編輯了 5 次。

inho
文章: 69
註冊時間: 2017-12-08, 23:00

Re: Install openCV 4.1 on Ubuntu 19.10 (Version2)

文章 inho » 2019-10-23, 15:42

https://cv-tricks.com/installation/open ... untu18-04/
https://www.teknotut.com/en/install-ope ... ic-beaver/

Step 1: Installing dependencies of OpenCV

sudo apt-get update -y # Update the list of packages
sudo apt-get remove -y x264 libx264-dev # Remove the older version of libx264-dev and x264
sudo apt-get install -y build-essential checkinstall cmake pkg-config yasm

sudo apt-get install -y git gfortran

# The following command will add the link of the repository to install the libraries which does not support
# Ubuntu 19.04
sudo add-apt-repository -y "deb http://security.ubuntu.com/ubuntu disco-security main"
sudo apt-get install -y libjpeg8-dev libjasper-dev libpng12-dev

sudo apt-get install -y libtiff5-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get install -y libxine2-dev libv4l-dev

sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y qt5-default libgtk2.0-dev libtbb-dev

sudo apt-get install -y libatlas-base-dev
sudo apt-get install -y libfaac-dev libmp3lame-dev libtheora-dev

sudo apt-get install -y libvorbis-dev libxvidcore-dev
sudo apt-get install -y libopencore-amrnb-dev libopencore-amrwb-dev

sudo apt-get install -y x264 v4l-utils
sudo apt-get install -y libprotobuf-dev protobuf-compiler
sudo apt-get install -y libgoogle-glog-dev libgflags-dev
sudo apt-get install -y libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

#==================================================
Step 2: Installing the required Python version

# Installing Python and Pip
# To install Python 2
sudo apt-get install -y python-dev python-pip

# To install Python 3
sudo apt-get install -y python3-dev python3-pip

# To install both
sudo apt-get install -y python-dev python-pip python3-dev python3-pip
# Install numpy for python 2
sudo -H pip2 install -U pip numpy

# Install numpy for python 3
sudo -H pip3 install -U pip numpy
#=========================
Step 3: Installing the Virtual Environment
# Downloads Virtual Environment for python2
sudo pip2 install virtualenv virtualenvwrapper

# Downloads Virtual Environment for python3
sudo pip3 install virtualenv virtualenvwrapper
#===================================================
Step 4: Modify .bashrc file
# Run the following commands in the terminal one by one
echo "# Virtual Environment Wrapper" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc
#=============================================================
Step 5: Creating Virtual Environment and Installing basic Python libraries
# Run the following commands in the terminal
# For Python 2
# Create a virtual environment opencv_source_2
mkvirtualenv opencv_source_2 -p python2
# Activate the environment
workon opencv_source_2
# Install the basic libraries
pip install numpy scipy matplotlib scikit-image scikit-learn ipython

# For Python 3
# Create a virtual environment opencv_source_3
mkvirtualenv opencv_source_3 -p python3
# Activate the environment
workon opencv_source_3
# Install the basic libraries
pip install numpy scipy matplotlib scikit-image scikit-learn ipython

#==============================================
Step 6: Downloading OpenCV 4.1.2 and OpenCV Contrib:
cd ~
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.2.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contri ... /4.1.2.zip
unzip opencv.zip
unzip opencv_contrib.zip
mv opencv-4.1.2 opencv
mv opencv_contrib-4.1.2 opencv_contrib
rm -f opencv.zip opencv_contrib.zip

#=============================================
Step 7: Using CMake to build the library

cd ~/opencv
mkdir build
cd build

# Make sure that you have activated your virtual environment
workon opencv_source_2 # For Python 2
workon opencv_source_3 # For Python 3

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=YES ..

#=================================================

Step 8: Using make utility for building the library
# Compile the library and install

# We will specify the number of jobs that will be used to build the library.
# To find the number of threads compatible in your machine run the following command.
nproc

# The following command specifies the number of jobs with make utility
make -j8 # -j4

# Install the library by running the following commands.
sudo make install

# 完成安裝後的 configuration file 放在 /etc/ld.so.conf.d/x86_64-linux-gnu.conf
#sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/x86_64-linux-gnu.conf'
sudo ldconfig

ln -s /usr/local/lib/python3.7/site-packages/cv2/python-3.7/cv2.cpython-37m-x86_64-linux-gnu.so ~/.virtualenvs/opencv_source_3/lib/python3.7/site-packages/cv2.so

#------------------------------------
#successful installation test:
#dpkg is the typical way to get software package versions.

$ dpkg -l | grep libopencv

#=== look at the headers or libs installed. pkg-config can tell you where they are:
$ pkg-config --cflags opencv4
$ pkg-config --libs opencv4
#=== check opencv version
$ pkg-config --modversion opencv4
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv4.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found

#===================================================================
Step 9: Modifying opencv4.pc file

#如果前述指令出現問題,
#參考解決方案:
#1、尋找 /usr/lib/x86_64-linux-gnu 底下有無 pkgconfig 目錄,(/usr/lib/x86_64-linux-gnu/pkgconfig), 沒有的話就 mkdir pkgconfig.
#然後該目錄下建制一個 opencv4.pc 的檔案,內容為:
$ sudo vi opencv4.pc
#The file will look something like this:
prefix=/usr/local
exec_prefix=${prefix}/bin
includedir=${prefix}/include/opencv4
libdir=${prefix}/lib

Name: openCV
Description: The opencv library
Version: 4.1.2
Cflags: -I${includedir}/
Libs: -L${libdir}/ -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_calib3d -lopencv_ccalib -lopencv_core -lopencv_datasets -lopencv_dnn_objdetect -lopencv_dnn -lopencv_dpm -lopencv_face -lopencv_features2d -lopencv_flann -lopencv_freetype -lopencv_fuzzy -lopencv_gapi -lopencv_hfs -lopencv_highgui -lopencv_imgcodecs -lopencv_img_hash -lopencv_imgproc -lopencv_line_descriptor -lopencv_ml -lopencv_objdetect -lopencv_optflow -lopencv_phase_unwrapping -lopencv_photo -lopencv_plot -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_shape -lopencv_stereo -lopencv_stitching -lopencv_structured_light -lopencv_superres -lopencv_surface_matching -lopencv_text -lopencv_tracking -lopencv_videoio -lopencv_video -lopencv_videostab -lopencv_xfeatures2d -lopencv_ximgproc -lopencv_xobjdetect -lopencv_xphoto

#===== saved ====
2、然後在 home dir 底下 編輯 ~/.bashrc 檔案最後面加入這二行:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib/x86_64-linux-gnu/pkgconfig
export PKG_CONFIG_PATH
#--- reboot system ---
#=== look at the headers or libs installed. pkg-config can tell you where they are:
$ pkg-config --cflags opencv4
-I/usr/local/include/opencv4/
$ pkg-config --libs opencv4
-L/usr/local/lib/ -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_calib3d -lopencv_ccalib -lopencv_core -lopencv_datasets -lopencv_dnn_objdetect -lopencv_dnn -lopencv_dpm -lopencv_face -lopencv_features2d -lopencv_flann -lopencv_freetype -lopencv_fuzzy -lopencv_gapi -lopencv_hfs -lopencv_highgui -lopencv_imgcodecs -lopencv_img_hash -lopencv_imgproc -lopencv_line_descriptor -lopencv_ml -lopencv_objdetect -lopencv_optflow -lopencv_phase_unwrapping -lopencv_photo -lopencv_plot -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_shape -lopencv_stereo -lopencv_stitching -lopencv_structured_light -lopencv_superres -lopencv_surface_matching -lopencv_text -lopencv_tracking -lopencv_videoio -lopencv_video -lopencv_videostab -lopencv_xfeatures2d -lopencv_ximgproc -lopencv_xobjdetect -lopencv_xphoto
#=== check opencv version
$ pkg-config --modversion opencv4
4.1.2

#=== Sample Makefile =====
/* A simple example */
#include "opencv2/opencv.hpp"

using namespace cv;

int main(void){
Mat srcImg = imread("picture.jpg");

imshow("srcImg", srcImg);
waitKey(0);

return 0;
}
#=============================
compile:
g++ main.cpp -o output `pkg-config --cflags --libs opencv`

# or=== Sample Makefile saved as Makefile =====
CPP= g++
CPPFLAGS += -c -Wall $(shell pkg-config --cflags opencv4)
LDFLAGS += $$(pkg-config --libs opencv4)
all: opencv_example
opencv_example: main.o; $(CPP) $< -o $@ $(LDFLAGS)
%.o: %.cpp; $(CPP) $< -o $@ $(CPPFLAGS)
clean: ; rm -f main.o opencv_example
# run make
$ make

回覆文章