opencv实现拍照的程序
实现拍照的简单程序,按空格拍照,esc退出
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<iostream>
#include<stdio.h>
using namespace cv;
using namespace std;
int main(int argc, char* argv)
{
VideoCapture capture(0);
Mat frame;
if (!capture.isOpened())
{
return -1;
}
char filename[200];
int count = 23;
while (1)
{
char key = cv::waitKey(50);
capture.read(frame);
imshow("video", frame);
if (key == 27)break;//按ESC键退出程序
if (key == 32)//按空格键进行拍照
{
sprintf(filename, "D:\\pic\\pic%d.jpg", ++count);
imwrite(filename, frame);//图片保存到本工程目录中
imshow("image", frame);
}
}
return 0;
}
读取文件夹中的图片并将其按照尺寸进行裁剪
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<iostream>
#include<stdio.h>
using namespace cv;
using namespace std;
String face_cascade_name = "haarcascade_frontalface_default.xml";
CascadeClassifier face_cascade; //定义人脸分类器
int i = 1;
void detectAndCut(Mat image);
int main(int argc, char* argv)
{
Mat image;
char fileName[244];
if (!face_cascade.load(face_cascade_name)){ printf("--(!)Error loading face cascade\n"); return -1; };
for (int i = 1; i < 245; i++)
{
sprintf(fileName, "D:\\pic\\pic%d.jpg", i);//用这个函数来转换图片名称,存放在filename中。
image = imread(fileName, 1);
//imshow(filename, image);
detectAndCut(image);
}
return 0;
}
void detectAndCut(Mat image)
{
std::vector<Rect> faces;
Mat img_gray;
char fileOutName[244];
cvtColor(image, img_gray, COLOR_BGR2GRAY);
equalizeHist(img_gray, img_gray);
//-- Detect faces
face_cascade.detectMultiScale(img_gray, faces, 1.1, 3, CV_HAAR_DO_ROUGH_SEARCH, Size(50, 50));
for (size_t j = 0; j < faces.size(); j++)
{
Mat faceROI = image(faces[j]);
Mat MyFace;
if (faceROI.cols > 100)
{
resize(faceROI, MyFace, Size(92, 112));
sprintf(fileOutName, "D:\\MyFace\\MyFcae%d.jpg", i++);
imwrite(fileOutName, MyFace);
imshow("ii", MyFace);
}
waitKey(10);
}
}