-
Notifications
You must be signed in to change notification settings - Fork 0
/
dm_helper.cpp
168 lines (127 loc) · 3.64 KB
/
dm_helper.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "dm_helper.h"
#include "image.h"
#include "io.h"
int compute(dm_params_t* dm_params, image_t* im1, image_t* im2, corres_t** corres, int* count)
{
float_image* dm_results;
corres_t* dm_corres;
int ret;
// Compute matches
dm_results = deep_matching(im1, im2, dm_params, NULL);
// Allocate output memory
dm_corres = (corres_t*)malloc(sizeof(corres_t) * dm_results->ty);
if (dm_corres == NULL)
{
ret = 0;
goto eof;
}
// Copy correspodences
for (int i = 0; i < dm_results->ty; ++i)
dm_corres[i] = ((corres_t*)dm_results->pixels)[i];
// Return output
*corres = dm_corres;
*count = dm_results->ty;
ret = 1;
eof:
free_image(dm_results);
return ret;
}
void* dm_alloc(size_t size)
{
return malloc(size);
}
void dm_free(void* p)
{
free(p);
}
int dm_alloc_params(dm_params* params)
{
// Allocate memory for dm_params_t
dm_params_t* dm_params = (dm_params_t*)malloc(sizeof(dm_params_t));
if (dm_params == NULL)
return 0;
// Set default parameters
set_default_dm_params(dm_params);
#ifdef _DEBUG
dm_params->verbose = 1;
#endif
// Copy values from dm_params
params->downscale_factor = dm_params->prior_img_downscale;
params->neighbor_radius = dm_params->ngh_rad;
params->max_patch_size = dm_params->max_psize;
params->verbose = dm_params->verbose;
params->nthread = dm_params->n_thread;
// Assign dm_params to inner_ptr to use deep_matching()
params->inner_ptr = dm_params;
return 1;
}
int dm_update_params(dm_params* params)
{
// prams is empty, do nothing
if (params->inner_ptr == NULL)
return 0;
// Unlike dm_alloc_params, copy values of dm_params from params
dm_params_t* dm_params = (dm_params_t*)params->inner_ptr;
dm_params->prior_img_downscale = params->downscale_factor;
dm_params->ngh_rad = params->neighbor_radius;
dm_params->max_psize = params->max_patch_size;
dm_params->verbose = params->verbose;
dm_params->n_thread = params->nthread;
return 1;
}
void dm_free_params(dm_params* params)
{
if (params->inner_ptr != NULL)
free(params->inner_ptr);
params->inner_ptr = NULL;
}
int dm_copy_params(const dm_params* sparams, dm_params* dparams)
{
// sparams is empty, do nothing
if (sparams->inner_ptr == NULL)
return 0;
// Reuse inner_ptr of dparams if available
dm_params_t* dm_params = (dm_params_t*)dparams->inner_ptr;
// inner_ptr of dparams is empty, allocate new memory of dm_params_t
if (dm_params == NULL)
{
if ((dm_params = (dm_params_t*)malloc(sizeof(dm_params_t))) == NULL)
return 0;
}
// Copy inner_ptr values from sparams
*dm_params = *((dm_params_t*)sparams->inner_ptr);
// Copy sparams to dparams deeply
*dparams = *sparams;
dparams->inner_ptr = dm_params;
return 1;
}
int dm_move_params(dm_params* sparams, dm_params* dparams)
{
// Copy deeply
if (dm_copy_params(sparams, dparams) == 1)
{
// Make sparams used
sparams->inner_ptr = NULL;
return 1;
}
return 0;
}
int dm_compute_unsafe(dm_params* params, dm_image* im1, dm_image* im2, dm_corres** corres, int* count)
{
return compute((dm_params_t*)params->inner_ptr, (image_t*)im1, (image_t*)im2, (corres_t**)corres, count);
}
int dm_compute(dm_params* params, dm_image* im1, dm_image* im2, dm_corres** corres, int* count)
{
int ret;
image_t* _im1;
image_t* _im2;
// Copy image from original
_im1 = image_cpy((image_t*)im1);
_im2 = image_cpy((image_t*)im2);
// Compute matches
ret = compute((dm_params_t*)params->inner_ptr, _im1, _im2, (corres_t**)corres, count);
// Delete copied image
image_delete(_im1);
image_delete(_im2);
return ret;
}