在opencv4下,对摄像头进行调用显示画面,在按下p键后对当前画面进行压缩保存到路径/root/20204367yyh并从photo1开始命名,并在视频画面左上角显示“当前已保存的图片数量”,最后在按下q键后退出画面。同时给出相应的Makefile文件,注意opencv的路径为/root/opencv-4.5.1
时间: 2023-05-28 21:02:56 浏览: 91
以下是实现代码和Makefile文件:
```c++
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() {
VideoCapture cap(0);
if (!cap.isOpened()) {
cerr << "Error opening camera." << endl;
return -1;
}
int photoNum = 0;
Mat frame;
namedWindow("Camera", WINDOW_NORMAL);
while (true) {
cap >> frame;
imshow("Camera", frame);
int key = waitKey(30);
if (key == 'p') {
string path = "/root/20204367yyh/photo" + to_string(++photoNum) + ".jpg";
imwrite(path, frame);
cout << "Save photo: " << path << endl;
} else if (key == 'q') {
break;
}
putText(frame, "Photos: " + to_string(photoNum), Point(10, 30), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 2);
}
cap.release();
destroyAllWindows();
return 0;
}
```
Makefile文件:
```makefile
CC = g++
CFLAGS = -Wall -std=c++11
LDFLAGS = $(shell pkg-config --libs opencv4)
all: camera
camera: camera.cpp
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
clean:
rm camera
```
阅读全文