-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathComputeGeodesicDistanceImage.cpp
134 lines (109 loc) · 4.39 KB
/
ComputeGeodesicDistanceImage.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
#include"mex.h" /* Matlabのデータ構造の読み込み */
#include <algorithm>
using namespace std;
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
/*
* 0: Img (double 2D)
* 1: Si (logical 2D)
* 2: gamma (double)
* 3: T (doule)
*/
int rows = mxGetM(prhs[1]);
int cols = mxGetN(prhs[1]);
int N = rows * cols;
double *Img = (double*)mxGetData(prhs[0]);
double *mask = (double*)mxGetData(prhs[1]);
double gamma = mxGetScalar(prhs[2]);
int T = (int)mxGetScalar(prhs[3]);
plhs[0] = mxCreateDoubleMatrix(rows, cols, mxREAL);
double *geodist = (double*)mxGetData(plhs[0]);
// initialize
for ( int i = 0; i < N; i++ ){
if ( mask[i] > 0 )
geodist[i] = 0;
else
geodist[i] = 10000000;
}
double *imgR = &Img[N*0];
double *imgG = &Img[N*1];
double *imgB = &Img[N*2];
for ( int t = 0; t < T; t++ ){
for ( int j = 0, n = 0; j < cols; j++ ){
for ( int i = 0; i < rows; i++,n++ ){
double current = geodist[n];
if ( i && j ) {
int m = (i-1) + (j-1)*rows;
double dr = imgR[m]-imgR[n];
double dg = imgG[m]-imgG[n];
double db = imgB[m]-imgB[n];
double proposal = sqrt(2.0 + gamma*(dr*dr+dg*dg+db*db)) + geodist[m];
current = min(current, proposal);
}
if ( j ) {
int m = i + (j-1)*rows;
double dr = imgR[m]-imgR[n];
double dg = imgG[m]-imgG[n];
double db = imgB[m]-imgB[n];
double proposal = sqrt(1.0 + gamma*(dr*dr+dg*dg+db*db)) + geodist[m];
current = min(current, proposal);
}
if ( i < rows-1 && j ) {
int m = (i+1) + (j-1)*rows;
double dr = imgR[m]-imgR[n];
double dg = imgG[m]-imgG[n];
double db = imgB[m]-imgB[n];
double proposal = sqrt(2.0 + gamma*(dr*dr+dg*dg+db*db)) + geodist[m];
current = min(current, proposal);
}
if ( i ) {
int m = (i-1) + j*rows;
double dr = imgR[m]-imgR[n];
double dg = imgG[m]-imgG[n];
double db = imgB[m]-imgB[n];
double proposal = sqrt(1.0 + gamma*(dr*dr+dg*dg+db*db)) + geodist[m];
current = min(current, proposal);
}
geodist[n] = current;
}
}
for ( int j = cols-1, n = N-1; j >= 0; j-- ){
for ( int i = rows-1; i >= 0; i--, n-- ){
double current = geodist[n];
if ( i && j < cols-1 ) {
int m = (i-1) + (j+1)*rows;
double dr = imgR[m]-imgR[n];
double dg = imgG[m]-imgG[n];
double db = imgB[m]-imgB[n];
double proposal = sqrt(2.0 + gamma*(dr*dr+dg*dg+db*db)) + geodist[m];
current = min(current, proposal);
}
if ( j < cols-1 ) {
int m = i + (j+1)*rows;
double dr = imgR[m]-imgR[n];
double dg = imgG[m]-imgG[n];
double db = imgB[m]-imgB[n];
double proposal = sqrt(1.0 + gamma*(dr*dr+dg*dg+db*db)) + geodist[m];
current = min(current, proposal);
}
if ( i < rows-1 && j < cols-1 ) {
int m = (i+1) + (j+1)*rows;
double dr = imgR[m]-imgR[n];
double dg = imgG[m]-imgG[n];
double db = imgB[m]-imgB[n];
double proposal = sqrt(2.0 + gamma*(dr*dr+dg*dg+db*db)) + geodist[m];
current = min(current, proposal);
}
if ( i < rows-1 ) {
int m = (i+1) + j*rows;
double dr = imgR[m]-imgR[n];
double dg = imgG[m]-imgG[n];
double db = imgB[m]-imgB[n];
double proposal = sqrt(1.0 + gamma*(dr*dr+dg*dg+db*db)) + geodist[m];
current = min(current, proposal);
}
geodist[n] = current;
}
}
}
}