-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
157 lines (117 loc) · 4.4 KB
/
Source.cpp
File metadata and controls
157 lines (117 loc) · 4.4 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace cv;
using namespace std;
// Read points stored in the text files
vector<Point2f> readPoints(string pointsFileName)
{
vector<Point2f> points;
ifstream ifs(pointsFileName);
float x, y;
while (ifs >> x >> y)
{
points.push_back(Point2f(x, y));
}
return points;
}
// Apply affine transform calculated using srcTri and dstTri to src
void applyAffineTransform(Mat& warpImage, Mat& src, vector<Point2f>& srcTri, vector<Point2f>& dstTri)
{
// Given a pair of triangles, find the affine transform.
Mat warpMat = getAffineTransform(srcTri, dstTri);
// Apply the Affine Transform just found to the src image
warpAffine(src, warpImage, warpMat, warpImage.size(), INTER_LINEAR, BORDER_REFLECT_101);
}
// Warps and alpha blends triangular regions from img1 and img2 to img
void morphTriangle(Mat& img1, Mat& img2, Mat& img, vector<Point2f>& t1, vector<Point2f>& t2, vector<Point2f>& t, double alpha)
{
// Find bounding rectangle for each triangle
Rect r = boundingRect(t);
Rect r1 = boundingRect(t1);
Rect r2 = boundingRect(t2);
// Offset points by left top corner of the respective rectangles
vector<Point2f> t1Rect, t2Rect, tRect;
vector<Point> tRectInt;
for (int i = 0; i < 3; i++)
{
tRect.push_back(Point2f(t[i].x - r.x, t[i].y - r.y));
tRectInt.push_back(Point(t[i].x - r.x, t[i].y - r.y)); // for fillConvexPoly
t1Rect.push_back(Point2f(t1[i].x - r1.x, t1[i].y - r1.y));
t2Rect.push_back(Point2f(t2[i].x - r2.x, t2[i].y - r2.y));
}
// Get mask by filling triangle
Mat mask = Mat::zeros(r.height, r.width, CV_32FC3);
fillConvexPoly(mask, tRectInt, Scalar(1.0, 1.0, 1.0), 16, 0);
// Apply warpImage to small rectangular patches
Mat img1Rect, img2Rect;
img1(r1).copyTo(img1Rect);
img2(r2).copyTo(img2Rect);
Mat warpImage1 = Mat::zeros(r.height, r.width, img1Rect.type());
Mat warpImage2 = Mat::zeros(r.height, r.width, img2Rect.type());
applyAffineTransform(warpImage1, img1Rect, t1Rect, tRect);
applyAffineTransform(warpImage2, img2Rect, t2Rect, tRect);
// Alpha blend rectangular patches
Mat imgRect = (1.0 - alpha) * warpImage1 + alpha * warpImage2;
// Copy triangular region of the rectangular patch to the output image
multiply(imgRect, mask, imgRect);
multiply(img(r), Scalar(1.0, 1.0, 1.0) - mask, img(r));
img(r) = img(r) + imgRect;
}
int main(int argc, char** argv)
{
string filename1("chel.jpg");
string filename2("cat.jpg");
//alpha controls the degree of morph
double alpha = 0.0;
//Read input images
Mat img1 = imread(filename1);
Mat img2 = imread(filename2);
//convert Mat to float data type
img1.convertTo(img1, CV_32F);
img2.convertTo(img2, CV_32F);
for (int ii = 0; ii < 10; ii++) {
alpha += 0.1;
//empty average image
Mat imgMorph = Mat::zeros(img1.size(), CV_32FC3);
//Read points
vector<Point2f> points1 = readPoints(filename1 + ".txt");
vector<Point2f> points2 = readPoints(filename2 + ".txt");
vector<Point2f> points;
//compute weighted average point coordinates
for (int i = 0; i < points1.size(); i++)
{
float x, y;
x = (1 - alpha) * points1[i].x + alpha * points2[i].x;
y = (1 - alpha) * points1[i].y + alpha * points2[i].y;
points.push_back(Point2f(x, y));
}
//Read triangle indices
ifstream ifs("tri.txt");
int x, y, z;
while (ifs >> x >> y >> z)
{
// Triangles
vector<Point2f> t1, t2, t;
// Triangle corners for image 1.
t1.push_back(points1[x]);
t1.push_back(points1[y]);
t1.push_back(points1[z]);
// Triangle corners for image 2.
t2.push_back(points2[x]);
t2.push_back(points2[y]);
t2.push_back(points2[z]);
// Triangle corners for morphed image.
t.push_back(points[x]);
t.push_back(points[y]);
t.push_back(points[z]);
morphTriangle(img1, img2, imgMorph, t1, t2, t, alpha);
}
// Display Result
imshow("Morphed Face", imgMorph / 255.0);
waitKey(0);
}
}