This repository was archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsp.cpp
75 lines (68 loc) · 2.86 KB
/
tsp.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
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
// enum to describe location of face
//enum Face { LEFT, BASE, RIGHT, BOTTOM, BACK, TOP };
enum Face {TOP, LEFT, BASE, RIGHT, BACK, BOTTOM};
// passes by reference the requested transform matrix
void get_transform(Face face_type, int max_offset, int face_edge, Point2f* output_quad) {
switch(face_type) {
case BASE:
output_quad[0] = Point2f( 0, 0 ); // top left
output_quad[1] = Point2f( face_edge, 0 ); // top right
output_quad[2] = Point2f( 0, face_edge ); // bottom left
output_quad[3] = Point2f( face_edge, face_edge ); // bottom right
break;
case LEFT:
output_quad[0] = Point2f( 0, max_offset ); // tl
output_quad[1] = Point2f( face_edge, 0 ); // tr
output_quad[2] = Point2f( 0, face_edge - max_offset ); // bl
output_quad[3] = Point2f( face_edge , face_edge); // br
break;
case RIGHT:
output_quad[0] = Point2f( 0, 0 ); // tl
output_quad[1] = Point2f( face_edge, max_offset ); // tr
output_quad[2] = Point2f( 0, face_edge ); // bl
output_quad[3] = Point2f( face_edge, face_edge - max_offset ); // br
break;
case TOP:
output_quad[0] = Point2f( max_offset, 0 ); // tl
output_quad[1] = Point2f( face_edge - max_offset, 0 ); // tr
output_quad[2] = Point2f( 0, face_edge ); // bl
output_quad[3] = Point2f( face_edge, face_edge ); // br
break;
case BOTTOM:
output_quad[0] = Point2f( 0, 0 ); // tl
output_quad[1] = Point2f( face_edge, 0 ); // tr
output_quad[2] = Point2f( max_offset, face_edge ); // bl
output_quad[3] = Point2f( face_edge - max_offset, face_edge ); // br
break;
case BACK:
output_quad[0] = Point2f( 0, max_offset ); // tl
output_quad[1] = Point2f( face_edge - 2*max_offset, max_offset ); // tr
output_quad[2] = Point2f( 0, face_edge - max_offset); // bl
output_quad[3] = Point2f( face_edge-2*max_offset, face_edge - max_offset ); // br
break;
}
}
// function to form tsp for a specific face
// takes the face type, face image, and truncation parameter
// also takes a boolean revert flag to allow reversions when appropriate
void tspform(Face face_type, Mat& face_mat, float trunc_param, Mat& dest_mat, bool revert)
{
// this line is independent of cols or rows used, as face guaranteed to be square
int max_offset = (int) (0.5*(face_mat.rows)*(1 - trunc_param));
// the maximum index of pixels in the face
int face_edge = face_mat.cols;
// get the appropriate transform
Point2f input_quad[4];
Point2f output_quad[4];
get_transform(face_type, max_offset, face_edge, output_quad);
get_transform(BASE, max_offset, face_edge, input_quad);
// warp the square image
if(revert){
warpPerspective(face_mat, dest_mat, getPerspectiveTransform(output_quad, input_quad), dest_mat.size());
} else {
warpPerspective(face_mat, dest_mat, getPerspectiveTransform(input_quad, output_quad), dest_mat.size());
}
}