#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
//save pictures
void SavePicture(Mat view, string dir, string name, int num)
{
string str_file = dir + "/" + name + to_string(num) + ".jpg";
//string str_file = dir + "/" + name + to_string(num) + ".jpg";
imwrite(str_file, view);
}
int main()
{
Mat image;
string dir = "img";
int num = 1;
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "cannot open camera" << endl;
}
else{
for (;;){
cap >> image;
if (image.empty()) {
cout << "image is empty." << endl;
return false;
}
char key = waitKey(1);
if (key == 32 || key == 'S' || key == 's') {
SavePicture(image, dir, "right", num);
++num;
}
putText(image, "Number is ", Point(15, 30), 1, 1, Scalar(255, 0, 0));
putText(image, to_string(num - 1), Point(110, 30), 1, 1, Scalar(0, 0, 255));
imshow("image", image);
if (key == 27) break;
}
}
system("pause");
return 0;
}
打开相机按空格保存一张图片
最新推荐文章于 2021-12-24 17:03:48 发布