-
Notifications
You must be signed in to change notification settings - Fork 6
/
zoom.cpp
152 lines (139 loc) · 3.88 KB
/
zoom.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
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
// This program is free software: you can use, modify and/or redistribute it
// under the terms of the simplified BSD License. You should have received a
// copy of this license along this program. If not, see
// <http://www.opensource.org/licenses/bsd-license.html>.
//
// Copyright (C) 2018, Thibaud Briand <[email protected]>
// Copyright (C) 2015, Javier Sánchez Pérez <[email protected]>
// Copyright (C) 2014, Nelson Monzón López <[email protected]>
// All rights reserved.
#include <math.h>
#include <stdio.h>
#include "zoom.h"
#include "mask.h"
#include "bicubic_interpolation.h"
#include "transformation.h"
#define ZOOM_SIGMA_ZERO 0.6
/**
*
* Compute the size of a zoomed image from the zoom factor
*
**/
void zoom_size
(
int nx, //width of the orignal image
int ny, //height of the orignal image
int &nxx, //width of the zoomed image
int &nyy, //height of the zoomed image
double factor //zoom factor between 0 and 1
)
{
nxx = (int) ((double) nx * factor + 0.5);
nyy = (int) ((double) ny * factor + 0.5);
}
/**
*
* Function to downsample the image
*
**/
void zoom_out
(
double *I, //input image
double *Iout, //output image
int nx, //image width
int ny, //image height
int nz, // number of color channels in image
double factor //zoom factor between 0 and 1
)
{
int nxx, nyy, original_size =nx*ny*nz;
double ifactor = 1.0/factor;
double *Is=new double[original_size];
for (int i=0; i<original_size; i++)
Is[i]=I[i];
//calculate the size of the zoomed image
zoom_size(nx, ny, nxx, nyy, factor);
//compute the Gaussian sigma for smoothing
double sigma=ZOOM_SIGMA_ZERO*sqrt(ifactor*ifactor-1.0);
//pre-smooth the image
gaussian(Is, nx, ny, nz, sigma);
if ( (int) ifactor == ifactor) {
for (int i1=0; i1<nyy; i1++)
for (int j1=0; j1<nxx; j1++)
{
int i2= i1*ifactor;
int j2= j1*ifactor;
for(int index_color=0; index_color<nz; index_color++)
Iout[(i1*nxx+j1)*nz+index_color]=Is[(i2*nx+j2)*nz+index_color];
}
}
else {
// re-sample the image using bicubic interpolation
for (int i1=0; i1<nyy; i1++)
for (int j1=0; j1<nxx; j1++)
{
double i2=(double)i1*ifactor;
double j2=(double)j1*ifactor;
for(int index_color=0; index_color<nz; index_color++)
Iout[(i1*nxx+j1)*nz+index_color]=
bicubic_interpolation(Is, j2, i2, nx, ny, nz, index_color);
}
}
delete []Is;
}
/**
*
* Function to upsample the parameters of the transformation
*
**/
void zoom_in_parameters
(
double *p, //input parameters
double *pout, //output parameters
int nparams, //number of parameters
int nx, //width of the original image
int ny, //height of the original image
int nxx, //width of the zoomed image
int nyy //height of the zoomed image
)
{
//compute the zoom factor
double factorx=((double)nxx/nx);
double factory=((double)nyy/ny);
double nu=(factorx>factory)?factorx:factory;
switch(nparams) {
default: case TRANSLATION_TRANSFORM: //p=(tx, ty)
pout[0]=p[0]*nu;
pout[1]=p[1]*nu;
break;
case EUCLIDEAN_TRANSFORM: //p=(tx, ty, tita)
pout[0]=p[0]*nu;
pout[1]=p[1]*nu;
pout[2]=p[2];
break;
case SIMILARITY_TRANSFORM: //p=(tx, ty, a, b)
pout[0]=p[0]*nu;
pout[1]=p[1]*nu;
pout[2]=p[2];
pout[3]=p[3];
break;
case AFFINITY_TRANSFORM: //p=(tx, ty, a00, a01, a10, a11)
pout[0]=p[0]*nu;
pout[1]=p[1]*nu;
pout[2]=p[2];
pout[3]=p[3];
pout[4]=p[4];
pout[5]=p[5];
break;
case HOMOGRAPHY_TRANSFORM: //p=(h00, h01,..., h21)
pout[0]=p[0];
pout[1]=p[1];
pout[2]=p[2]*nu;
pout[3]=p[3];
pout[4]=p[4];
pout[5]=p[5]*nu;
pout[6]=p[6]/nu;
pout[7]=p[7]/nu;
break;
}
}