-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
88 lines (66 loc) · 2.76 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
Mat mascara[3];
Mat transparencia(Mat,Mat,int,int);
int main()
{
mascara[0] = imread("anbu.png",CV_LOAD_IMAGE_UNCHANGED);
mascara[1] = imread("obito.png",CV_LOAD_IMAGE_UNCHANGED);
mascara[2] = imread("rock.png",CV_LOAD_IMAGE_UNCHANGED);
VideoCapture cap;
CascadeClassifier detector("haarcascade_frontalface_alt.xml" );
if(!cap.open(0))
cout << "No se puede acceder a la webcam." << endl;
while(true)
{
Mat dest, gray, imagen,mascara1,mascara2,mascara3;
cap >> imagen;
flip(imagen,imagen,1);
cvtColor(imagen, gray, CV_BGR2GRAY);
equalizeHist(gray, dest);
vector<Rect> rect;
detector.detectMultiScale(dest, rect, 1.2, 3, 0, Size(60,60),Size(400,500));
for(Rect rc : rect)
{
if(rect.size()==1){
resize(mascara[0],mascara1,Size(rect.at(0).width*1.5,rect.at(0).height*1.5));
imagen=transparencia(mascara1,imagen,rect.at(0).x-rect.at(0).x*.15,rect.at(0).y-rect.at(0).y*.3);
}
if(rect.size()==2){
resize(mascara[0],mascara1,Size(rect.at(1).width*1.5,rect.at(1).height*1.5));
imagen=transparencia(mascara1,imagen,rect.at(1).x-rect.at(1).x*.15,rect.at(1).y-rect.at(1).y*.3);
resize(mascara[1],mascara2,Size(rect.at(0).width*1.5,rect.at(0).height*1.5));
imagen=transparencia(mascara2,imagen,rect.at(0).x-rect.at(0).x*.15,rect.at(0).y-rect.at(0).y*.3);
}
if(rect.size()==3){
resize(mascara[0],mascara1,Size(rect.at(2).width*1.5,rect.at(2).height*1.5));
imagen=transparencia(mascara1,imagen,rect.at(2).x-rect.at(2).x*.15,rect.at(2).y-rect.at(2).y*.3);
resize(mascara[1],mascara2,Size(rect.at(1).width*1.5,rect.at(1).height*1.5));
imagen=transparencia(mascara2,imagen,rect.at(1).x-rect.at(1).x*.15,rect.at(1).y-rect.at(1).y*.3);
resize(mascara[2],mascara3,Size(rect.at(0).width*1.5,rect.at(0).height*1.5));
imagen=transparencia(mascara3,imagen,rect.at(0).x-rect.at(0).x*.15,rect.at(0).y-rect.at(0).y*.3);
}
}
imshow("Snapchat", imagen);
if(waitKey(1) >= 0) break;
}
return 1;
}
Mat transparencia(Mat img, Mat video,int x, int y){
Mat trans;
Mat canales[4],canal[3];
split(img, canales);
split(video, canal);
for(int i=0;i<img.rows;i++){
for(int j=0;j<img.cols;j++){
if((canales[3].at<uchar>(i,j))==255){
canal[0].at<uchar>(i+y,j+x)=canales[0].at<uchar>(i,j);
canal[1].at<uchar>(i+y,j+x)=canales[1].at<uchar>(i,j);
canal[2].at<uchar>(i+y,j+x)=canales[2].at<uchar>(i,j);
}
}
}
merge(canal,3,trans);
return trans;
}