This article mainly introduces the practice of compiling and installing the famous C/C++ image processing open source software library Opencv 3.4.12 under the Ubuntu18.04 system (the process is long, time-consuming, and requires patience and care)
Table of contents
3. Unzip the installation package
4. Use cmake to install opencv
5. Use make to create and compile
1. The virtual machine obtains camera permission
3. Turn on the camera to record video
1. Introduction to Opencv
The full name of OpenCV is Open Source Computer Vision Library, which is a cross-platform open source software library for computer vision processing. It is initiated, participated and maintained by the Russian team of Intel Corporation. It supports many algorithms related to computer vision and machine learning, with BSD license Released under license, free for commercial and research use. OpenCV can be used to develop real-time image processing, computer vision and pattern recognition programs, and the library can also use Intel's IPP for accelerated processing.
The application areas of Opencv include:
- Augmented Reality
- face recognition
- Gesture Recognition
- human-computer interaction
- Action recognition
- motion tracking
- object recognition
- Image segmentation
- robot
Basic core modules:
core module : implement the core data structure and its basic operations, such as drawing functions, array operation related functions, etc.;
highgui module : realize the interface of video and image reading, displaying and storing;
imgproc module : implements the basic methods of image processing, including image filtering, image geometric transformation, smoothing, threshold segmentation, morphological processing, edge detection, target detection, motion analysis and object tracking, etc.;
Image processing Other higher-level directions and application-related modules:
Stitching module : realize image stitching function;
features2d module : used to extract image features and feature matching;
nonfree module : implement some patent algorithms, such as sift feature;
photo module : Contains two parts of image restoration and image denoising;
ml module : machine learning module (SVM, decision tree, Boosting, etc.);
G-API module : contains an ultra-efficient image processing pipeline engine;
FLANN module : contains fast approximate nearest neighbor search FLANN and clustering Clustering algorithms;
Video module : for video processing, such as background separation, foreground detection, object tracking, etc.;
objdetect module : implement some target detection functions, classic face detection based on Haar and LBP features, target detection based on HOG such as pedestrians and cars, classifiers using Cascade Classification (cascade classification) and Latent SVM, etc.;
Calib3d module : Calibration (calibration) 3D, this module is mainly related to camera calibration and 3D reconstruction. Including basic multi-view geometry algorithm, single stereo camera calibration, object pose estimation, stereo similarity algorithm, 3D information reconstruction, etc.;
Two, Opencv installation
1. Installation environment
Ubuntu 18.04 system installed on VMware virtual machine
Opencv version: 3.4.12
2. Download Opencv3.4.12
Domestic quick download website: Index of /opencv/opencv-3.4.12/ (raoyunsoft.com)
Note: You can use the browser (Firefox Web Browser) to download directly in the virtual machine (you can also download it from the foreign official website address: Releases opencv/opencv GitHub , it is not recommended to download from foreign websites, it is very slow).
3. Unzip the installation package
Copy the installation package to the main directory (home) and decompress it.
Enter the command: unzip opencv-3.4.12.zip
4. Use cmake to install opencv
First enter the decompressed folder: opencv-3.4.12
Enter the command: cd opencv-3.4.12
Enter the root user and update it.
Enter the command: sudo su
sudo apt-get update
Next enter this command to install cmake.
Enter the command: sudo apt-get install cmake
Install dependent libraries:
Enter the command: sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff5.dev libswscale-dev libjasper-dev
( If you encounter the following problem in the process, you can refer to this link to solve it:) (8 messages) libjasper-dev error occurred in the opencv process: unable to locate package libjasper-dev problem solving_a new new Xiaobai's Blog-CSDN Blog
Create the build folder again, and then enter the folder we created: build
Enter the command: mkdir build
cd build
It is also possible to use cmake to compile parameters or the second default parameter:
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
cmake ..
5. Use make to create and compile
Still under the build file:
Enter the command: sudo make
Note: Single-threaded compilation:
sudo make
, which will wait for a long time (about 30 minutes), if you want to compile faster, you can use the command:sudo make -j4
, and -j4 means to use 4 threads to compile.
6. Install
Enter the command: sudo make install
The installation is complete.
7. Configure the environment
Modify the opencv.conf file, the opened file is empty, add the installation path of the opencv library: /usr/local/lib
Enter the command: sudo gedit /etc/ld.so.conf.d/opencv.conf
After saving, you will see a warning message, don't worry, it's normal.
Update the system shared link library:
Enter the command: sudo ldconfig
Configure bash and modify the bash.bashra file:
Enter the command: sudo gedit /etc/bash.bashrc
Add at the end of the file:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH
Save and exit, then execute the following command to make the configuration take effect:
Enter the command: source /etc/bash.bashrc
Update it:
Enter the command: sudo updatedb
Next, check the version information of opencv:
Enter the command: pkg-config --modversion opencv
At this point, the installation is over.
3. Example of use: pictures
Create a folder to store code files:
Enter the command: mkdir code
cd code
Create a test1.cpp file.
vim tset1.cpp
Copy and paste the following code into it:
Note: To copy and paste here, you need to install VMware Tools. For detailed installation, please refer to the following link:
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
CvPoint center;
double scale = -3;
IplImage* image = cvLoadImage("lena.jpg");
argc == 2? cvLoadImage(argv[1]) : 0;
cvShowImage("Image", image);
if (!image) return -1; center = cvPoint(image->width / 2, image->height / 2);
for (int i = 0;i<image->height;i++)
for (int j = 0;j<image->width;j++) {
double dx = (double)(j - center.x) / center.x;
double dy = (double)(i - center.y) / center.y;
double weight = exp((dx*dx + dy*dy)*scale);
uchar* ptr = &CV_IMAGE_ELEM(image, uchar, i, j * 3);
ptr[0] = cvRound(ptr[0] * weight);
ptr[1] = cvRound(ptr[1] * weight);
ptr[2] = cvRound(ptr[2] * weight);
}
Mat src;Mat dst;
src = cvarrToMat(image);
cv::imwrite("test.png", src);
cvNamedWindow("test",1); imshow("test", src);
cvWaitKey();
return 0;
}
Compile the file:
C++ file compilation instructions: g++ test1.cpp -o test1 `pkg-config --cflags --libs opencv`
If it is a built .c file, use: gcc test1.cpp -o test1 `pkg-config --cflags -- libs opencv`
gcc compiler: gcc + file name + -o + output file stream name + `support package`
In the above compilation command, we actually used a tool " pkg-config ", which mainly has the following functions:
- Check the version number of the library. If the version of the required library does not meet the requirements, it will print an error message to avoid linking the wrong version of the library file.
- Obtain compilation preprocessing parameters, such as macro definitions, and the location of header files.
- Obtain link parameters, such as the location of the library and other libraries it depends on, the file name and some other connection parameters.
- Automatically add the settings of other libraries that depend on
With this tool, our compilation is very convenient (but before that, you need to make sure that there is a pkgconfig folder in the directory of the OpenCV installation link library file you installed, and there is an opencv.pc in this folder. file, in fact, this is the configuration file of OpenCV under pkg-config).
When using pkg-config, the option –cflags is used to specify the directory where the header files required by the program are compiled, and the option –libs is to specify the directory of the dynamic link library required by the program when linking.
Note: If you encounter the following situations
You can enter the command: sudo apt-get install libcanberra-gtk-module
Just reinstall the following
no problem
Then prepare a picture in the same folder, the file name is lena.jpg (note the suffix)
Then enter the command: ./test1
You can see that a test.png is generated from lena.jpg. The rendering effect is different.
4. Example of use: video
1. The virtual machine obtains camera permission
Use the shortcut key Win + R, enter services.msc, and click OK:
Find VMware USB Arbitration S... make sure it's enabled.
Click on "Virtual Machine" and then click on "Settings"
Select "USB Controller", set "USB Compatibility" to "USB 3.1", and click OK.
Then click "Virtual Machine", select "Removable Devices", then select "IMC Networks USB2.0 VGA UVC WebCam", and finally click Connect, and click OK in the pop-up window.
If there is a small green dot on the camera icon in the lower right corner of the virtual machine, the connection is successful.
2. Play video
Create a test2.cpp file.
Enter the command: vim test2.cpp
Copy and paste the following code into it:
test2.cpp:
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
//读取视频
VideoCapture capture("MH.mp4");
//循环显示每一帧
while(1){
Mat frame;//定义一个Mat变量,用于存储每一帧的图像
capture >> frame;//读取当前帧
if(frame.empty())//播放完毕,退出
break;
imshow("读取视频帧",frame);//显示当前帧
waitKey(30);//掩饰30ms
}
system("pause");
return 0;
}
Code explanation:
- If the statement: VideoCapture capture(0) and the following parameters are set to 0, then the video will be read from the camera and each frame will be displayed in a loop; if it is set to a video file name, such as: PC.mp4 , the video will be read and loop through each frame.
- The Mat data structure in the while loop body is actually a dot matrix, corresponding to each point on the image, and the collection of dots forms a frame of image. For a detailed explanation of Mat, please see: Mat data structure in OpenCV
- Statement: waitKey(30) , the parameter unit is ms milliseconds, that is, the interval between each frame is 30 ms, this statement cannot be deleted, otherwise it will execute an error, and the video cannot be played or recorded.
This code will keep running in the while loop. If you try to close the image display window with the mouse, you will find that it cannot be closed. You need to use the keyboard Ctrl+C to forcibly interrupt the program.
Prepare a small video, I prepared PC.mp4 here
Compile the test2.cpp file.
Enter the command: g++ test2.cpp -o test2 `pkg-config --cflags --libs opencv`
Output result:
Enter the command: ./test2
Note: If you encounter the following situation, you can refer to the link:
(9 messages) Ubuntu does not support H.264_Old Bean Sprouts Blog-CSDN Blog_h.264 ubuntu
3. Turn on the camera to record video
Create a test3.cpp file.
Enter the command: vim test3.cpp
Copy and paste the following code into it:
test3.cpp:
/*********************************************************************
打开电脑摄像头,空格控制视频录制,ESC退出并保存视频RecordVideo.avi
*********************************************************************/
#include<iostream>
#include <opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
//打开电脑摄像头
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "error" << endl;
waitKey(0);
return 0;
}
//获得cap的分辨率
int w = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_WIDTH));
int h = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_HEIGHT));
Size videoSize(w, h);
VideoWriter writer("RecordVideo.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25, videoSize);
Mat frame;
int key;//记录键盘按键
char startOrStop = 1;//0 开始录制视频; 1 结束录制视频
char flag = 0;//正在录制标志 0-不在录制; 1-正在录制
while (1)
{
cap >> frame;
key = waitKey(100);
if (key == 32)//按下空格开始录制、暂停录制 可以来回切换
{
startOrStop = 1 - startOrStop;
if (startOrStop == 0)
{
flag = 1;
}
}
if (key == 27)//按下ESC退出整个程序,保存视频文件到磁盘
{
break;
}
if (startOrStop == 0 && flag==1)
{
writer << frame;
cout << "recording" << endl;
}
else if (startOrStop == 1)
{
flag = 0;
cout << "end recording" << endl;
}
imshow("picture", frame);
}
cap.release();
writer.release();
destroyAllWindows();
return 0;
}
Compile the test3.cpp file.
Enter the command: g++ test3.cpp -o test3 `pkg-config --cflags --libs opencv`
Enter the command: ./test3
Turn on the camera (recording has not yet started):
Press the space bar to start recording:
Press esc to stop and save.
V. Summary
Installing opencv under Ubuntu is a very troublesome thing. It really needs a lot of patience. You may encounter many problems in the middle. We need to cultivate our ability to solve problems. If you encounter Baidu, you will be done. If you encounter Others have encountered the problems we have experienced, and it is not difficult for us to step on the shoulders of our predecessors. It feels good to use it.
6. References
(7 messages) Introduction to OpenCV_Banhao Chunshui's Blog-CSDN Blog_opencv
(9 messages) Ubuntu does not support H.264_Old Bean Sprouts Blog-CSDN Blog_h.264 ubuntu