Open pictures and videos with opencv

Open pictures and videos with opencv

1. The installation of opencv

Reference article http://t.csdn.cn/QO7dr

2. Open the picture with opencv,
create a code folder to store the code, and then open the folder to create the test1.cpp file

Copy the following code in the test1.cpp 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;
}

insert image description here

Compile the file:

Execute the following command:

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

gcc compiler: gcc + file name + -o + output file stream name + ` support package

When using pkg-config, the option –cflags is used to specify the directory where the header files required by the program are compiled, and the option –libs is to specify the directory of the dynamic link library required by the program when linking.
Prepare a picture named lena.jpg under this folder, and execute the == ./test1== command to get the running result.
insert image description here
insert image description here

3. Play video with opencv

3.1 Enable the virtual machine to obtain camera permissions

Use the shortcut key Win + R, enter services.msc, and press Enter.
insert image description here
Find the VMware USB Arbitration S… service and make sure it is started.
insert image description here
Click "Virtual Machine", then click "Settings (S)..." or Ctrl+D
insert image description here
to select "USB Controller", set "USB Compatibility" to "USB 3.0", and click OK
insert image description here

Select "Virtual Machine", then select "Removable Device", then select "Quanta USB2.0 VGA UVC WebCam", and finally click "Connect", and then click "OK" in the pop-up window.
insert image description here
The fourth light in the picture can be

3.2 Play video

Create test2.cpp file, write the following code, save and compile

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

Code explanation:
If the statement: VideoCapture capture(0), the following parameters are set to 0, then the video is read from the camera and each frame is displayed in a loop; if it is set to a video file name, such as: man.mp4, it will be The video reads and loops through each frame.
The Mat data structure in the while loop body is actually a dot matrix, corresponding to each point on the image, and the collection of dots forms a frame of image. For a detailed explanation of Mat, please see: Mat data structure in OpenCV

Statement: waitKey(30), the parameter unit in it is ms milliseconds, that is, the interval between each frame is 30 ms, this statement cannot be deleted, otherwise it will execute an error, and the video cannot be played or recorded.
VideoCapture capture(0), if the following parameters are set to 0, the video will be read from the camera and each frame will be displayed in a loop; if it is set to a video file name, such as: park.mp4, the video will be read and displayed in a loop every frame.
insert image description here

4. Turn on the camera to record video

Create the test3.cpp file, save the file after copying the following code, and then compile
insert image description here

/*********************************************************************
打开电脑摄像头,空格控制视频录制,ESC退出并保存视频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()
{
	//打开电脑摄像头
	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;
}

Compile the test3.cpp file

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

output result
./test3
insert image description here

Guess you like

Origin blog.csdn.net/qq_52201641/article/details/126998076
Recommended