Installation and use examples of OpenCV under Ubuntu

Keywords: OpenCV Ubuntu

catalogue

1, Install OpenCV

1. Download installation package

1) Download OpenCV package

2) Installing opencv using cmake

4) Use make to create a compilation.

5) Installation

2. Configuration environment

2, Usage example -- picture

3, Usage example -- Video

1. Get camera permissions for virtual machine

2. Play video

​​

  3. Record video

4, Summary

5, References

1, Install OpenCV

1. Download installation package

1) Download OpenCV package

Download directly in the virtual machine using the browser at OpenCV/opencv_contrib domestic fast download | cloud technology blog  

2) Installing opencv using cmake

First copy the folder to the home folder

Enter the folder opencv-3.4.11.

​​cd opencv-3.4.11

 

  Enter the root user first and update it.

sudo su
sudo apt-get update

  Then execute the following command to install cmake.

sudo apt-get install cmake

 

  Copy the following instructions to install the dependent libraries.

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

Create the build folder.

mkdir build

 

  Then go to the folder we created: build.

cd build

Compile parameters using cmake.

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

 

4) Use make to create a compilation.

Still under the build file.

sudo make

 

Compilation is complete.

5) Installation

Enter the following command. (the waiting time for the following process is a little long)

sudo make install

 

Installation succeeded!!!

2. Configuration environment

Modify the opencv.conf file. The opened file is empty. At this time, add the installation path of opebcv Library: / usr/local/lib

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

The saved warning information can be ignored.

Update the system shared connection library.

sudo ldconfig

 

Then enter the following command:

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

 

Add the following instructions at the end of the above documents:

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

 

Exit after saving, and enter the following command in the terminal:

source /etc/bash.bashrc

 

Enter the following command update:

sudo updatedb

 

View the opencv version and enter the following command:

​
pkg-config --modversion opencv

​

It shows that the version of opencv is 3.4.11  

Proceeding to this step indicates that the installation is successful.

2, Usage example -- picture

Coding:

First create a folder code to store the code.

Then go to the folder.

cd picture

Create a test1.cpp file.

gedit test1.cpp

  Copy 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("dl.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;
}

  Compiled file:

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

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

Output results:

Enter the following command

./test1

 

  As can be seen from the above figure, dl.jpg generates a test.png. The effects of these two images are different, and the following image is darkened.

3, Usage example -- Video

1. Get camera permissions for virtual machine

Use the shortcut key Win+R, enter services.msc and press enter.

Find the service shown in the figure below and ensure that it is started.

Click "virtual machine" and then click settings.

Select as shown in the figure below.

  After the connection is successful, the camera icon has a small green dot.

2. Play video

Create a test2.cpp file

gedit test2.cpp

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

  Prepare a small video, such as man.mp4.

Compile the test2.cpp file.

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

 

Output results.

./test2

 

  3. Record video

Create a test3.cpp.

gedit test3.cpp

  Copy 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 the test3.cpp file.

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

Output results.

./test3

Finally, a vai file is generated and frames are generated continuously.

4, Summary

        The process of installing Opencv is time-consuming, but Opencv can be used in many fields. In the process of practicing using Opencv, I also found it interesting and useful. I learned how to record videos and simply process images

5, References

Installation and use example of OpenCV3.4.11 under Ubuntu 18.04_ ssj925319 blog - CSDN blog

Posted by jcantrell on Sun, 10 Oct 2021 01:49:51 -0700