Ubuntu system installation opencv detailed operation and specific application

foreword

Since the ubuntu system has been accidentally crashed recently, some things that were installed earlier have to be reinstalled. I have nothing to do. In order to improve the efficiency of the reinstaller, I wrote some things into a blog.

1. What is opencv

Open Source Computer Vision (OpenCV) is a programming library primarily aimed at real-time computer vision.
Application areas of OpenCV include: 2D and 3D feature toolkits, motion estimation, facial recognition systems, gesture recognition, human-computer interaction, mobile robotics, motion understanding, object recognition, segmentation and recognition, solid image stereo vision: from two cameras Depth perception, motion tracking, augmented reality, and more.

Second, the installation of opencv

1. Resource preparation and preliminary preparation

Since the download of the official website is too slow, I found a domestic reliable website to use.
Domestic quick download address: https://www.raoyunsoft.com/wordpress/index.php/2020/03/09/opencvdownload/Select
insert image description here
version 3.4.11 here,
insert image description here
download to download by default
insert image description here
and right-click to extract it here, or terminal Enter the command:

unzip opencv-3.4.11.zip

2. Install and compile

Enter the unzipped folder: opencv-3.4.11

cd opencv-3.4.11

Enter root and update

    sudo su
    sudo apt-get update

insert image description hereThen the following command installs cmake

sudo apt-get install cmake

insert image description hereCopy the following command to install the dependent library

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

insert image description hereCreate a build folder and enter it, use cmake to compile parameters

cmake ..

insert image description hereBuild and compile with make

sudo make

insert image description hereIf you want to compile faster, you can use the command: sudo make -j4 to compile with 4 threads.
Install

sudo make install

insert image description hereBasically no error

3. Configuration changes

Modify the opencv.conf file, add the installation path of the opencv library in this empty file: /usr/local/lib

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

After saving the command, a warning message appears, don’t worry, it’s normal, but you must close the document, otherwise there will only be two lines of warnings, and you can’t continue to execute the command. Update the system shared link
library

sudo ldconfig

insert image description hereConfigure bash, modify the bash.bashrc file

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

source /etc/bash.bashrc

insert image description hereupdate it

sudo updatedb

4. Verify the version

Next, check the version information of opencv

pkg-config --modversion opencv

success.

Three, opencv application

1. Open the picture for special effect display

Enter the OpenCV folder, create a working directory, and create the first OpenCV program

mkdir mytest
gedit test1.cpp

insert image description here

#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("WWW.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;
}

Put the pictures that need to be opened and processed in the same directory as test1.cpp to
insert image description herecompile the program

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

insert image description here

2. Use the opencv library to open the camera

New test2.cpp

gedit test2.cpp
#include<opencv2/opencv.hpp>
using namespace cv;

int main()
{
	//从摄像头读取视频
	VideoCapture capture(0);
	//循环显示每一帧
	while (1)
	{
		Mat frame;//定义一个Mat变量,用于存储每一帧的图像
		capture >> frame;//读取当前帧
		imshow("读取视频帧", frame);//显示当前帧
		if(waitKey(30)==27) //按esc键退出,
		{
			break;
		}
	}
	system("pause");
	return 0;
}

Compile and run

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

insert image description here

3. Modify the previous program to open the video file to play

Replace the 0 in capture() with the address of the video file

#include<opencv2/opencv.hpp>
using namespace cv;

int main()
{
        //从摄像头读取视频
        VideoCapture capture("/home/wei/视频/1.mp4");
        //循环显示每一帧
        while (1)
        {
                Mat frame;//定义一个Mat变量,用于存储每一帧的图像
                capture >> frame;//读取当前帧
                imshow("读取视频帧", frame);//显示当前帧
                waitKey(30);//延时30ms
        }

        system("pause");
        return 0;
}

Compile and run

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

insert image description here

4. Problems that are easy to encounter

Does anyone still know how to download web videos?
Click to go to the video home page, the third option in the upper right corner
insert image description hereto select more tools - Web developer tools

insert image description hereSelect the network –
insert image description hereselect the media and double-click the pop-up mp4 to download it

5. References

https://blog.csdn.net/clyrjj/article/details/109367729?spm=1001.2014.3001.5506
https://blog.csdn.net/bookzhan/article/details/104753855/

Guess you like

Origin blog.csdn.net/m0_48609250/article/details/124225995