提取人脸敏感区域并保存成图像

论坛 期权论坛 脚本     
匿名技术用户   2021-1-2 12:40   11   0

没事间整理

#include <opencv2\opencv.hpp>
#include<time.h>
#include<string>
#include<vector>
#include<iostream>
#include<cv.h>
#include<cxcore.h>
#include<highgui.h>
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include<fstream>
using namespace cv;
using namespace std;
string tem3 = "C:/Users/sudi/Desktop/临时2/train/jieguo";
//ostream os;
//istream is;
const char*filename = "C:/Users/sudi/Desktop/临时/val/face.txt";
ifstream in;
void SetRoI(Mat tem, Point temP, Point temP2,string str1);
void piliangDuQu();
void chuliTuxiang(string filename1,string tuxingming1);
int main()
{
string tem2 = "C:/Users/sudi/Desktop/临时/val1";
string tem5;
in.open(filename,ios::in);
while (getline(in,tem5))
{
string tem = "C:/Users/sudi/Desktop/临时/val";
tem.append("/");
tem.append(tem5);
chuliTuxiang(tem,tem5);
cout << tem5 << endl;
}
return 0;
}
void chuliTuxiang(string filename1,string tuxingming1){
String cascadename = "haarcascade_frontalface_alt2.xml";
CascadeClassifier cascade;
cascade.load(cascadename);
//VideoCapture capture(0);
cout << "测试点2" << endl;
vector<Rect>faces;
//vector<Rect>eye;
// while (1)
//{
Mat edges;
clock_t start, finish;
double totaltime;
start = clock();
cout << "测试点3" << endl;
//Mat frame; //定义一个Mat变量,用于存储每一帧的图像
Mat frame1 = imread(filename1);
//imshow("hehe", frame1);
Mat gray;
boxFilter(frame1, gray, -1, Size(7, 7));
cvtColor(gray, edges, CV_BGR2GRAY);
Mat temp;
cout << "neicun" << sizeof(edges) << endl;
//cout << "测试点3.3" << endl;
equalizeHist(edges, edges);
start = clock();
cascade.detectMultiScale(edges, faces, 1.1, 3, 0 | CV_HAAR_DO_CANNY_PRUNING);
finish = clock();
totaltime = (double)(finish - start) / CLOCKS_PER_SEC;
cout << "\n此程序的运行时间为" << totaltime << "秒!" << endl;
if (faces.size() == 0)
{
cout << "跳过" << endl;
//continue;
}
else
{
int i = 0;
for (vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++)
{
Point center;
Point center2;
//Scalar color = colors[i % 8];
center.x = cvRound(r->x);
center.y = cvRound(r->y);
center2.x = cvRound(r->x + r->width);
center2.y = cvRound(r->y + r->height);
//rectangle(frame1, center, center2,Scalar(255,255,1));
//Mat img;
if (faces.size() != 0){
SetRoI(frame1, center, center2, tuxingming1);
}

//IplImage *img(frame1);
}
}

//cout << "断点7" << endl;
cout << "测试点5" << endl;
//imshow("result", frame1);
}
void SetRoI(Mat tem,Point temP,Point temP2,string str1){
//rectangle(tem,temP,temP2, Scalar(0, 255, 255), 0.1, CV_AA);
Mat imgROI = tem(Rect(temP,temP2));
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
string image_name = tem3 + "/" + str1;
//sprintf(image_name, "%s%d%s", "roi", 0, ".jpg");//保存的图片名
imwrite(image_name, imgROI, compression_params);
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP