基于Opencv 的 粗糙 美颜 人眼放大

老规矩,先上图

代码:

// eyebigger.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include

#include   
#include   
#include   
#include   



#include 
#include 



using namespace std;
using namespace cv;

String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
std::vector eyes;

CascadeClassifier eyes_cascade;
Mat frame_from_caream;
Mat frame_gray;
Mat latten;
Mat roi; //放大后的人眼局部图像
int main()
{
	VideoCapture cap(0);
	if (cap.isOpened() == 0)
	{
		cout << "打开摄像头失败" << endl;

	}
	else
	{
		if (!eyes_cascade.load(eyes_cascade_name))
		{ 
			cout<<"eye_cascade_name加载失败\n";
			return -1; 
		}
		else
		{
			cout << "eye_cascade_name加载成功\n";
			
		}
		while (1)
		{
			cap >> frame_from_caream;
			//imshow("原图", frame_from_caream);
			frame_from_caream.copyTo(latten);
			cvtColor(frame_from_caream, frame_gray, CV_BGR2GRAY);
			eyes_cascade.detectMultiScale(frame_gray, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
			if (eyes.size() > 0)
			{
				
				cout << "检测到眼睛";

				for (int i = 0; i < eyes.size(); i++)
				{
					Rect eye = eyes[i];
					//rectangle(latten, eye, Scalar(0, 0, 255), 2);
					cout << "人眼坐标x" << eye.x <<  "人眼坐标y" << eye.y << endl; 
					roi = frame_from_caream(eye);
					resize(roi, roi, Size(eye.height*1.2, eye.width*1.2));
					
					roi.copyTo(latten(Rect(eye.x- (eye.height*1.2-roi.cols)/2, eye.y - (eye.width*1.2- roi.rows)/2, roi.cols, roi.rows)));
					imshow("人眼fangda ", latten);
				}
				imshow("人眼", frame_from_caream);
				
				
				

			}
			else 
			{
				cout<< "未检测到眼睛" << endl;
			}
			char key = waitKey(30);
			if (key == 27)
			{
				break;
			}
		}
	}



    return 0;
}


你可能感兴趣的:(opencv,自定义目标检测,目标检测)