Switch to opencv in Ubuntu 16.04 environment

Summary:

This article mainly introduces the installation of opencv in the Ubuntu16.04 environment, and use opencv for simple image processing and video processing


One, install opencv3.4.10

I used to download in the windows environment, and then copied to Ubuntu16.04

step1: official website https://opencv.org/releases/# find and download the sources version

step2: Copy to the home directory, and then unzip the configuration:
unzip opencv-3.4.1.zip
step3: Enter the decompressed file package
cd opencv-3.4.10

step4: install dependent library cmake
sudo apt install cmake

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

step5: Enter the folder to start configuration
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

step6: start compiling, this process takes a long time, about an hour

Of course, you can use multiple threads, which will increase the speed

sudo make

carried out

sudo make install

End of compilation

step7: Configure OpenCV compilation environment

To add a path to the opencv library, let the system find

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

Let the path take effect:

sudo ldconfig

2_10

Configure bash

sudo gedit /etc/bash.bashrc  

Add at the end

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

2_11

Make the configuration effective and update

source /etc/bash.bashrc  
sudo updatedb  

This completes the configuration.


Second, perform image processing

step1: Create a test folder, and assign the screenshot of the communication electronic circuit courseware to the directory named wave.jpg
step2: and create a function (c++)

The function first displays the wave picture, then processes the picture, displays the processed picture and writes it in the directory as .png

step3: save and compile
g++ test.cpp -o test `pkg-config --cflags --libs opencv`

Explanation:

Compile the cpp file and use g++ test.c -o test to compile the executable file

pkg-config:

1. Check the version number of the library to avoid linking the wrong version of the library file

2. Get compilation preprocessing parameters, such as macro definitions and header file location

3. Get link parameters, such as the location of the library and other libraries since, the file name and some other link parameters

4. Automatically join all other library locations

And -cflags is used to specify the directory where the header files needed when the program is recompiled

-libs is the directory of the dynamic link library required when the specified program is relinked

Use this to get opencv header files and library files

step4: execute

You can see the processed picture


Three, simply play with the camera

1. First, configure the USB of the virtual machine camera

Win+R open cmd and enter services.msc to open the USB service of the virtual machine

Insert picture description here

Then select "Virtual Machine (M)" in the virtual machine menu bar -> select "Removable Device" -> select "xxxx Camera" -> select "Connect (disconnect from the host)", after setting this, The virtual machine took over the notebook camera.
Insert picture description here
Observe the lower right corner of the virtual machine, the camera logo is on

Insert picture description here

Can enter

ls /dev/video0

Check if there is a camera

Enter the following command in the virtual machine to open the camera to view

cheese

If not installed, you can install it

apt-get install cheese

If the camera is normal, you can proceed to the next step

2. Simple sentences
#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
	//从摄像头读取视频
	Videocapture capture(0);
	//循环显示每一帧
	white (1)
	{	Mat frame;//定义一个Mat变量,用于存储每一的图像
		Capture>>frame;//读取当前帧
		imshow("读取视频帧", frame);//显示当前顿
		waitKey(30);//延时30m
	}
	system("pause");
	return 0;
}

Code explanation:

1. Mat is an array used to store the pixels of a frame of pictures, each pixel corresponds to an RGB value, which can display the current pixel color block

2. A certain delay when reading frames is to achieve normal playback. According to statistics, when 27 and 28 frames per second are displayed, the video we see is smooth. If there is no delay, the picture will disappear immediately after running. It's gone if you don't see the screen. So the waitKey function is essential, it is best to delay about 30ms

3. If you want to control the video to pause or play, you can use the following sentence, and the space can control the pause

if(delay>=0&&waitKey (delay)>=32)
       waitKey(0);</span>

Run a screenshot here
Insert picture description here

3. Read the video file

Copy a video file in advance

If you want to open a video file to play, the seventh line can be changed to

cv::Videocapture capture;
capture.open("myvideo.flv");

Modified code:

Results of the

4. Improved code processing camera

This code can record video, press space to start recording, press space to end recording, ESC to stop recording and save

#include<iostream>
#include <opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;

void main()
{
	//打开电脑摄像头
	VideoCapture cap(0);
	if (!cap.isOpened())
	{
		cout << "error" << endl;
		waitKey(0);
		return;
	}

	//获得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();
}

You can try to open the computer camera to record a video for fun
Insert picture description here
Insert picture description here


Four, summary

Opencv is very efficient to process images. Playing opencv in a virtual machine through Ubuntu16.04 will bring you a different experience. The use of the camera and the processing of images and videos are very useful and interesting! In addition, machine vision is getting more and more popular nowadays. It can be seen from the topic of this year's electronic competition. In addition, machine vision is widely used, so what are you waiting for, let's learn together!

This article borrows part of the content of the cungudafa blog, thank you here! !

Guess you like

Origin blog.csdn.net/lee_goi/article/details/109265598