table of Contents
One, install opencv3.4.10
1. Put the opencv3.4.10 compressed package under home
Unzip
unzip opencv-3.4.1.zip
2. Install dependent library cmake
3. After installing cmake, execute the command to create a compilation folder
4. Start configuration
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
5. Start compiling
sudo make -j4
Excuting an order
sudo make install
Configure OpenCV compilation environment
After sudo make install is executed, the OpenCV compilation process is over. Next, you need to configure some OpenCV compilation environment. First add the OpenCV library to the path, so that the system can find it
sudo gedit /etc/ld.so.conf.d/opencv.conf
Let the path take effect and configure bash
[External link image transfer failed, the source site may have anti-theft insert here! Chain mechanism, suggest description] It is recommended to upload the image to https://(imblogC5WGsdnimg.cn/2020105330-640565.png#pic_center3) (https://imgblog.csdnimg.cn/20201030153005656.png#pic_center)]
add at the end
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
Make the configuration effective and update
source /etc/bash.bashrc
sudo updatedb
Two, image preprocessing
Create function
#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;
}
Save and translate
g++ test.cpp -o test `pkg-config --cflags --libs opencv`
Three, open the camera with opencv
Configure the virtual machine camera USB
Win+R Open cmd and enter services.msc to confirm that the USB service of the virtual machine is turned on.
Click the camera icon in the lower right corner, and the camera will
light up.
Simple execution
#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;
}
Open the camera to capture images and save
Code
#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();
}
The method of obtaining the path of the opencv header file and linking lib library file during the compilation process:
If the path is not specified in the compilation, the header file will check the gcc environment variables and find the system default directory: /usr/include;/usr/local/include, Link the environment variables of the lib library gcc and find the default path /lib;/usr/lib;/usr/local/lib. After that, we can run: