Installation and use of OpenCV3.4.11 under Ubuntu 18.04

catalogue 1, Install OpenCV (1) Installation package (2)...
(1) Installation package
(2) Configuration environment  
(1) Get camera permissions for virtual machine
  (2) Video playback
  (3) Video recording

catalogue

1, Install OpenCV

(1) Installation package

(2) Configuration environment  

  2, Pictures of OpenCv usage examples

  3, Video of an example of OpenCv

(1) Get camera permissions for virtual machine

  (2) Video playback

  (3) Video recording

4, References

1, Install OpenCV

(1) Installation package

1. Download OpenCV 3.4.11 data package. Domestic fast download address: OpenCV/opencv_contrib domestic fast download | cloud technology blog

  2. Decompression

Before decompressing the package, copy opencv-3.4.11.zip to the home directory folder, and then decompress it.

3. Install opencv

Enter the command to enter the extracted folder

cd opencv-3.4.11

Enter command:   sudo su   Then execute the installation command

sudo apt-get install cmake

Then install the dependent libraries:

sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff5.dev libswscale-dev

  Create the build folder, and then enter the created folder

mkdir build cd build

  Compile the parameter using cmake, or use the second default parameter

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local .. cmake ..

4. Use make to create a compiler

sudo make

5. Installation

  After compiling, enter the command:

sudo make install

Wait for the installation to complete.

(2) Configuration environment  

Enter command:

sudo gedit /etc/ld.so.conf.d/opencv.conf

  After that, you can modify the opencv.conf file. You can find that the file is empty. Add the installation path of OpenCV Library: / usr/local/lib

Click save, and then return to the terminal. You can find that there is a warning message, there is no impact, or the command is input normally.  

Input:

sudo ldconfig

Update the system shared link library and enter:

sudo gedit /etc/bash.bashrc

  Configure Bash and modify the bash.bashrc file.

  The text editor opens

  Add at the end of the document

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH

  Click save, then exit... And enter the command to make the configuration effective.

source /etc/bash.bashrc

Input instruction: sudo updatedb   To update  

Input command:

pkg-config --modversion opencv

  Check the version of opencv we installed and find that it is 3.4.11. The installation is over.

  2, Pictures of OpenCv usage examples

First, create a folder for storing code. Here I name it code1. Enter the following command to create and enter the folder

mkdir code1 cd code1

  Create a test1.cpp file

vim test1.cpp

  Then enter the following code:

#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; }

  Execute the following command to compile the file

g++ test1.cpp -o test1 `pkg-config --cflags --libs opencv`

  Prepare a picture in the same folder with the file name lena.jpg

Input instruction:. / test

  You can see that lena.jpg generates a test.png, and the picture is different

  3, Video of an example of OpenCv

(1) Get camera permissions for virtual machine

Under windows, use the shortcut key Win + R, enter services.msc, and press enter.

  Locate the VMware USB Arbitration S... Service and make sure it starts.

  Click virtual machine, and then click Settings.

Select "USB controller", set "USB compatibility" to "USB 3.0", and click OK.

Select "virtual machine", then select "removable device", and then select the camera name of your device, such as Quanta USB2.0 VGA UVC WebCam  , Finally, click "connect", and then click "OK" in the pop-up window.  

  After the connection is successful, you can see the green dot in the lower right corner of the virtual machine.

  (2) Video playback

Create a test2.cpp file

Enter the following code:

#include <opencv2/opencv.hpp> using namespace cv; int main() { //Read video from camera VideoCapture capture("man.mp4"); //Cycle through each frame while(1){ Mat frame;//Define a Mat variable to store the image of each frame capture >> frame;//Read current frame if(frame.empty())//Play finished, exit break; imshow("Read video frame",frame);//Displays the current frame waitKey(30);//Cover up 30ms } system("pause"); return 0; }

Download a video from the browser. Here is a man.mp4

  Compile test2.cpp

g++ test2.cpp -o test2 `pkg-config --cflags --libs opencv`

Output results

./test2

  You can find the video playing.

  (3) Video recording

Create a test3.cpp file  

  Enter the following code:

/********************************************************************* Turn on the computer camera, control the video recording with a blank space, and ESC exits and saves the video 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() { //Turn on the computer camera VideoCapture cap(0); if (!cap.isOpened()) { cout << "error" << endl; waitKey(0); return 0; } //Get the resolution of 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;//Record keyboard keys char startOrStop = 1;//0 starts recording video; 1 end recording video char flag = 0;//Recording flag 0 - not recording; 1 - recording while (1) { cap >> frame; key = waitKey(100); if (key == 32)//Press the space to start recording and pause recording to switch back and forth { startOrStop = 1 - startOrStop; if (startOrStop == 0) { flag = 1; } } if (key == 27)//Press ESC to exit the whole program and save the video file to disk { 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 test3.cpp with the input code  

g++ test3.cpp -o test3 `pkg-config --cflags --libs opencv`

Output results:

./test3

  An. avi file is generated, and frames are generated continuously, and the camera is turned on.

  The installation and use of OpenCv is over.

4, References

Learning OpenCV from scratch (I) -- Installation of OpenCV_ PittAndJolie's blog - CSDN blog learn OpenCV from scratch tips: Here you can add the directories of all articles in the series of articles. The directories need to be added manually. For example, tips on the use of pandas in Chapter 1 Introduction to Python machine learning: after writing articles, the directories can be generated automatically, How to generate the help document article directory on the right? Learn OpenCV preconditions from scratch 1. OpenCV installation 2. Introduction to OpenCV basic modules 1. Import and storage 2. Read in data summary preconditions [tips]: the installation environment of this tutorial is as follows: operating system: windows10VS version: 2017 tip: the following is the main content of this article. The following cases are for reference 1 Installation of OpenCVhttps://blog.csdn.net/YangChengKang/article/details/118750655Opencv installation tutorial_ Phoebezr's blog - CSDN blog_ Opencv download and install Opencv3.4.6 installation tutorial download opencv download opencv_contribCmake configuration and generation of Visual Studio 2017 environment configuration test how to insert a beautiful piece of code, generate a list suitable for you, create a table, set the content to be centered, left and right, SmartyPants create a custom list, Download opencv, and download the required opencv version from the official website: https://opencv.org/releases/ Download opencv.ehttps://blog.csdn.net/Phoebezr/article/details/90487325

8 October 2021, 06:49 | Views: 8229

Add new comment

For adding a comment, please log in
or create account

0 comments