Basic use of Opencv and sample pictures and videos

1. Understanding 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 Toolkit
Motion Estimation
Facial Recognition Systems
Gesture Recognition
Human-Computer Interaction Mobile
Robotics
Action Understanding
Object Recognition
Segmentation and Recognition
Solid Image Stereo Vision: Depth Perception from Two Cameras
Structure in Motion (SFM)
Motion Tracking
Augmented Reality
To support some of the above areas, OpenCV includes a statistical machine learning library that includes:
Boosting
Decision Tree Learning
Gradient Boosting Trees
Expectation Maximization Algorithm
k-Nearest Neighbors
Naive Bayesian Classifiers
Artificial Neural Networks
Random Forests
Support Vector Machine (SVM)
Deep Neural Network (DNN)

2. Install Opencv

Because the process is too complicated, I forgot to take a screenshot when I installed it, but you can take a look at the following blog, which can be called a nanny-level teaching https://blog.csdn.net/ssj925319/article/details/109231145

3. Use example (picture)

First, enter touch code and cd code in the terminal window to create and open the code file,
then enter gedit test1.cpp to create a file named test1, and
enter the following code in the blank file:
#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;

}
( This code is also from the blog linked above )
Enter g++ test1.cpp -o test1pkg-config --cflags --libs opencv
because the file is a .cpp file, so the gcc command is no longer used. Also prepare a picture named lena.jpg
insert image description here
in the code folder and enter the command **./test1** At this time, a new picture format of test.png will appear in the code folder, and the rendering effect is different
insert image description here


insert image description here
insert image description here

4. Example of use (video)

1. The virtual machine obtains the permission of the camera

insert image description here
Keyboard win+R , enter services.msc in the command window
insert image description here
to find VMware USB Arbitration S... is it in use?
insert image description here
Enter vm, Ctrl+D enter settings, change USB compatibility to
USB3.0

insert image description here
, click the virtual machine to connect to the host, after the connection is successful, the virtual machine right Below are the following patterns
insert image description here

2. Play video

First prepare a video in the previously created code file
insert image description here
and then go back to the terminal window and enter gedit test2.cpp to create a cpp file!
insert image description here

Enter the above code, remember to change the name of the video file
Input **g++ test2.cpp -o test2 pkg-config --cflags --libs opencv**Compile test2.cpp
insert image description here
Input ./test2 Output result
insert image description here
insert image description here

3. Record video.

Also create test3.cpp in the code folder
insert image description here
#include
#include <opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std ;

int main()
{ //Open computer camera 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;

}
Enter the above code
insert image description here
to compile test3.cpp
and input ./test3 to output the result
insert image description here

The code file generates an .avi file.
insert image description here
Continuously generate frames
. Click Esc to exit recording
insert image description here
. Since the computer camera is broken, the recorded video is a black screen, so I will not play the video screenshot.

5. References

1、https://blog.csdn.net/ssj925319/article/details/109231145

Guess you like

Origin blog.csdn.net/qq_54761976/article/details/127040380