-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo_translation.cpp
79 lines (63 loc) · 1.45 KB
/
geo_translation.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "myImageIO.h"
int linear_interpolation(myImageData *in, double xx, double yy)
{
int W = in->getWidth();
int H = in->getHeight();
int x, y;
int v1, v2, v3, v4;
x = int(xx);
y = int(yy);
v1 = v2 = v3 = v4 = 0;
if(x>=0 && x<W-1 && y>=0 && y<H-1) { // Input check
v1 = in->get(x , y);
v2 = in->get(x+1, y);
v3 = in->get(x , y+1);
v4 = in->get(x+1, y+1);
}
int value;
double dx, dy;
dx = xx - x;
dy = yy - y;
value = (unsigned char)(
v1 * (1-dx)*(1-dy) +
v2 * dx*(1-dy) +
v3 * (1-dx)*dy +
v4 * dx*dy
+ 0.5);
return value;
}
int translation(myImageData *in, myImageData *out, double delta_x, double delta_y)
{
int W = in->getWidth();
int H = in->getHeight();
int x, y;
for(int x=0; x<W; x++) {
for(int y=0; y<H; y++) {
// shift
double xx, yy;
xx = x - delta_x;
yy = y - delta_y;
int value;
value = linear_interpolation(in, xx, yy);
out->set(x, y, value);
}
}
return 0;
}
int main(int argc, char **argv){
// read image data to img1
myImageData * img1 = new myImageData();
img1->read(argv[1]);
myImageData * img2 = new myImageData();
img2->init(img1->getWidth(), img1->getHeight(), 1);
int W = img1->getWidth();
int H = img1->getHeight();
translation(img1, img2, 128, 128);
img2->save("out");
delete img1;
delete img2;
return 0; // normal termination
}