Installation and use of Opencv under Ubuntu18.04


foreword

We will learn the installation and some usage of Opencv under Ubuntu18.04 system.

1. Installation of Opencv

Because the use and learning of Opencv requires the desktop version of Ubuntu, if it is already installed, you can skip this step, and if it is the Server version, you need to download the desktop components.

sudo apt-get update  //更新源
sudo apt-get install ubuntu-desktop //安装桌面组件
sudo reboot //重启虚拟机

Installation URL: https://opencv.org/releases/
1. Open the website in Ubuntu 18.04. For downloading, it is recommended to download the newest version, which is relatively stable and has a lot of information on the Internet. Click Sourcesto download .

insert image description here
2. Enter the terminal with ctrl+alt+T and install the corresponding dependent environment

sudo apt-get install build-essential 
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libjasper-dev libdc1394-22-dev

3. Copy the downloaded installation package to the home directory to facilitate the next operation. Unzip the downloaded compressed package

unzip opencv-4.5.5.zip

4. Enter the decompressed file, and create a new build folder in this directory

cd opencv-4.5.5
mkdir build

insert image description here
5. Enter the build pathcmake

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

CMAKE_INSTALL_PREFIX: The installation path of opencv is installed under /usr/local.
CMAKE_BUILD_TYPE: The installation version of opencv, Release is installed by default here.

insert image description here
6. In the build directorymake

sudo make -j2 //我这里是双线程,大家可以根据自己虚拟机的配置来

insert image description here
7. Install in the changed directory

sudo make install

8. Add the opencv library to the system path and configure the ld.so.conf file

sudo vim /etc/ld.so.conf
//在打开的文件中加一行
include /usr/local/lib
并执行 sudo ldconfig  //使配置生效

insert image description here
9. Configure system bash

sudo vim /etc/bash.bashrc
//再打开的文件末尾添加以下内容
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
//添加完之后执行以下命令,使配置生效
source /etc/bash.bashrc

10. The installation is complete!

2. Application examples of opencv under Ubuntu18.04

1. Display of pictures

1.code Create a folder under the directory and enter the directory

mkdir code
cd code

2. Use the vim editor to write a test.cpp in the code directory

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

insert image description here
3. Execute the following command

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

insert image description here
4. Execute the following command

./test  //执行输出文件

insert image description here
You can see that the picture has been displayed with special effects

2. Use of camera

(1) Obtain the permission of the camera

  • In the windows system, win+r, enter services.msc, find the VMware USB Arbitration S... service, and start the service.
  • Click on the settings in the virtual machine, set the "USB Compatibility" of the USB controller to usb3.0
    - select "Removable Devices" in the virtual machine, then select "Quanta USB2.0 VGA UVC WebCam", and finally click Connect Can.

(2) Camera calls to record video

1. Create a .cpp file and enter the code

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

2. Enter the following command to compile and generate an executable file

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

insert image description here
It can be found that the computer camera is being used to shoot (because my camera is broken, there is no shooting effect)

(3) Play video

1. Similarly, a .cpp file is also needed, paste the following code into it, compile and run it. (There must be a video under the folder)

#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
    
    
	//从摄像头读取视频
	VideoCapture capture("2.mp4");
	//循环显示每一帧
	while(1){
    
    
		Mat frame;//定义一个Mat变量,用于存储每一帧的图像
		capture >> frame;//读取当前帧
		if(frame.empty())//播放完毕,退出
			break;
		imshow("读取视频帧",frame);//显示当前帧
		waitKey(30);//掩饰30ms
	}
	system("pause");
	return 0;
}

insert image description here
The video can be played normally!

3. References

1、 https://blog.csdn.net/ssj925319/article/details/109231145?spm=1001.2014.3001.5501


Summarize

Opencv is a very interesting open source library for image processing. I have used it under the windows system before, but I have not used it under the ubuntu system. I never thought that it would be troublesome under the ubuntu system. Of course, the trouble we feel is just that we are not familiar with the Linux operating system. After all, we are all used to the graphical operating system of windows, and hope to make greater progress through continuous learning!

Guess you like

Origin blog.csdn.net/wer4567/article/details/127033885
Recommended