I. Introduction to the environment
Development board: Raspberry PI 4B
OpenCV version: 3.4.9
Operating system: 32-bit operating system of the development board
pi@raspberrypi:~ $uname -a Linux Raspberrypi 4.19.97- v7L + #1294 SMP Thu Jan 30 13:21:14 GMT 2020 armV7L GNU/Linux pi@raspberrypi:~ $cat /proc/version Linux version 4.19.97- v7L + (dom@buildbot) (GCC version 4.9.3 (crosstool-ng) Crosstool-ng-1.22.0-88-g8460611)) #1294 SMP Thu Jan 30 13:21:14 GMT 2020Copy the code
Compiled libraries can be used directly: download.csdn.net/download/xi…
2. Compile and install OpenCV
2.1 download the OpenCV
OpenCV can be downloaded at opencv.org/releases/
Here I download: version 3.4.9
The package name is Opencv-3.4.9.zip
Operation Raspberry PI connection: Use Windows Remote Desktop connection.
Remote desktop does not look here: blog.csdn.net/xiaolong112…
2.2 install cmake
Compiling OpenCV source code needs to use Cmake, in order to facilitate compilation, here download and install cmake image interface version, convenient visual configuration.
The following commands are executed:
Sudo apt-get install cmake sudo apt install cmake-qt-gui sudo apt-get install libgtk2.0-dev sudo apt-get install pkg-configCopy the code
2.3 Description before Compilation
The space of Raspberry PI 4B itself is not enough, I insert a 16GB SD card here – access through USB card reader, and format this SD card into ext4 file system on the PC, decompress and compile OpenCV source code on the raspberry PI in this SD card space.
Note: It must be an ext4 file system, not a FAT or NTFS file system.
2.4 Compiling and configuring OpenCV source code
After this step is executed, no error can be reported, that is, no error can be reported on the interface. If there is an error, solve the error according to the prompt.
After executing step 2, close the software and go to the compile directory for compilation and installation.
PI @ raspberrypi: / media/PI/sdcard/work/opencv 3.4.9 / Build $CD/media/PI/sdcard/work/opencv 3.4.9 / Build PI @ raspberrypi: / media/PI/sdcard/work/opencv 3.4.9 / Build $make && make installCopy the code
After a successful compilation, copy all the generated library files and header files to the /use directory.
2.5 testing OpenCV: compile sample code using g++
Under the samples/ CPP /example_cmake directory of OpenCV, there is a sample test code with the effect of turning on the camera.
PI @ raspberrypi: / media/PI/sdcard/work/opencv - 3.4.9 / samples/CPP/example_cmake $g + + example, CPP - lopencv_calib3d -lopencv_objdetect -lopencv_core -lopencv_photo -lopencv_dnn -lopencv_shape -lopencv_features2d -lopencv_stitching -lopencv_flann -lopencv_superres -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_imgproc - lopencv_videostab - lopencv_ml PI @ raspberrypi: / media/PI/sdcard/work/opencv - 3.4.9 / samples/CPP/example_cmake $ PI @ raspberrypi: / media/PI/sdcard/work/opencv - 3.4.9 / samples/CPP/example_cmake $. / a.outCopy the code
To test the code, find a USB camera and plug it into the USB port of the Raspberry PI 4B.
2.6 Testing the Python OpenCV module
Go to the PYTHon3 command line, import the Cv2 module, and view the OpenCV version.
Python2 is also normal.
2.7 Invoke the OpenCV module using Python to open the PC camera
import numpy as np
import cv2
# call the laptop built-in camera, so the parameter is 0, if there are other cameras can be adjusted to 1,2
cap=cv2.VideoCapture(0)
while True:
# Read pictures from camera
sucess,img=cap.read()
# Transform to grayscale image
#gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Show camera, background is grayscale.
#cv2.imshow("img",gray)
cv2.imshow("img",img) # Display color image
# Keep the picture going.
k=cv2.waitKey(1)
if k == 27:
Exit camera by pressing esc
cv2.destroyAllWindows()
break
elif k==ord("s") :Press the S key to save the image and exit.
cv2.imwrite("image2.jpg",img)
cv2.destroyAllWindows()
break
# Turn off camera
cap.release()
Copy the code
The following public number in a full set of microcontroller, QT, C, C++ basic tutorial, welcome your attention.