Install OpenCV 4.1.2 on Raspberry Pi 4

提供 Raspberry Pi 4 安裝、設定
inho
文章: 69
註冊時間: 2017-12-08, 23:00

Install OpenCV 4.1.2 on Raspberry Pi 4

文章inho » 2019-10-29, 21:35

1.Dependencies.

$ sudo apt-get install build-essential cmake git unzip pkg-config
$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install libgtk2.0-dev libcanberra-gtk*
$ sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev
$ sudo apt-get install python3-dev python3-numpy
$ sudo apt-get install python-dev python3-pip python-numpy
$ sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev
$ sudo apt-get install libv4l-dev v4l-utils
$ sudo apt-get install libjasper-dev libopenblas-dev libatlas-base-dev libblas-dev
$ sudo apt-get install liblapack-dev gfortran
$ sudo apt-get install gcc-arm*
$ sudo apt-get install protobuf-compiler

2.Download OpenCV.
$ 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

$ cd ~/opencv/
$ mkdir build
$ cd build


3. Build Make

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D CMAKE_SHARED_LINKER_FLAGS=-latomic \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=TRUE \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..

https://www.pyimagesearch.com/2019/09/1 ... an-buster/

Before we can start the actual build, the memory swap space needs to be enlarged. For daily use a swap memory of 100 Mbyte is sufficient. However, with the massive build ahead of use, extra memory space is crucial. Enlarge the swap space with the following command.

sudo vi /etc/dphys-swapfile

move the cursor to the CONF_SWAPSIZE line where the new value 4096 can be entered.
Two additional commands are required before the new enlarge swap space is active.
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start

4. Make.

$ make -j4
$ sudo make install

# 安裝成功,會在 /etc/ld.so.conf.d/ 底下產生 一個 configuration file : arm-linux-gnueabihf.conf
並在 /usr/local/lib/pkgconfig 底下產生一個 opencv4.pc 的 file
內容如下:
#====================================
# Package Information for pkg-config

prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv4/opencv
includedir_new=${prefix}/include/opencv4

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.1.2
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_videoio -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
Libs.private: -latomic -ldl -lm -lpthread -lrt
Cflags: -I${includedir_old} -I${includedir_new}
#========================
$ sudo ldconfig
$ sudo apt-get update
$ sudo vi /etc/dphys-swapfile

set CONF_SWAPSIZE=100 with the vi text editor
#------------------------------------
#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
-I/usr/local/include/opencv4/opencv -I/usr/local/include/opencv4
$ pkg-config --libs opencv4
-L/usr/local/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_videoio -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core

#=== check opencv version
$ pkg-config --modversion opencv4
4.1.2
若有前述顯示內容,表示 opencv4安裝完成。

$ sudo reboot

5.Checking.

cd ~/opencv/build/lib/python3
$ sudo cp cv2.cpython-37m-arm-linux-gnueabihf.so \
/usr/local/lib/python3.7/dist-packages/cv2.so
$ cd ~/opencv
$ rm -r build

#=== 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 opencv4`

# 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

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客