-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDT.c
126 lines (97 loc) · 3.69 KB
/
DT.c
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
/************************************************************************
File: 2D_DT_Library.c
Author(s): Alexander Vasilevskiy
Created: 24 May 2000
Last Revision: $Date$
Description:
$Revision$
$Log$
Copyright (c) 2000 by Alexander Vasilevskiy, Centre for Intelligent Machines,
McGill University, Montreal, QC. Please see the copyright notice
included in this distribution for full details.
***********************************************************************/
#include <stdio.h>
#include <math.h>
#include "mex.h"
#define INFINITY 999999
// detects edges between black and white regions
void EdgeDetect(double *in,double *out,int WIDTH,int HEIGHT){
int x,y;
for(y=0; y <HEIGHT; y++) {
for(x=0; x<WIDTH; x++) {
if ((x==0)||(y==0)||(y==HEIGHT-1)||(x==WIDTH-1)) out[y*WIDTH + x]=INFINITY;
else
if ( (in[y*WIDTH + x]!=INFINITY)&&
((in[(y-1)*WIDTH + x]==INFINITY)||
(in[(y+1)*WIDTH + x]==INFINITY)||
(in[y*WIDTH + x+1]==INFINITY)||
(in[y*WIDTH + x-1]==INFINITY))) out[y*WIDTH + x]=0; else out[y*WIDTH + x]=INFINITY;
}
}
}
// assignes sign to distance transform
void PutSign(double *in,double *out,int WIDTH,int HEIGHT){
for(int j=0; j<HEIGHT; j++)
for(int i=0; i<WIDTH; i++)
if (in[j*WIDTH + i] == INFINITY) out[j*WIDTH + i]=-out[j*WIDTH + i];
}
double MINforward(double *in,double d1,double d2,int i,int j,int WIDTH,int HEIGHT){
double mask[5];
double min=INFINITY;
int k;
if ((i>0)&&(j>0)) mask[0]=in[(j-1)*WIDTH + (i-1)]+d2; else mask[0]=INFINITY;
if (j>0) mask[1]=in[(j-1)*WIDTH + i]+d1; else mask[1]=INFINITY;
if ((i<WIDTH-1)&&(j>0)) mask[2]=in[(j-1)*WIDTH + i+1]+d2; else mask[2]=INFINITY;
mask[3]=in[j*WIDTH + i];
if (i>0) mask[4]=in[j*WIDTH + i-1]+d1; else mask[4]=INFINITY;
for(k=0;k<5;k++) if (mask[k]<min) min=mask[k];
return min;
}
double MINbackward(double *in,double d1,double d2,int i,int j,int WIDTH,int HEIGHT){
double mask[5];
double min=INFINITY;
int k;
mask[0]=in[j*WIDTH + i];
if (i<WIDTH-1) mask[1]=in[j*WIDTH + i+1]+d1; else mask[1]=INFINITY;
if ((j<HEIGHT-1)&&(i<WIDTH-1)) mask[2]=in[(j+1)*WIDTH + i+1]+d2; else mask[2]=INFINITY;
if (j<HEIGHT-1) mask[3]=in[(j+1)*WIDTH + i]+d1; else mask[3]=INFINITY;
if ((i>0)&&(j<HEIGHT-1)) mask[4]=in[(j+1)*WIDTH + i-1]+d2; else mask[4]=INFINITY;
for(k=0;k<5;k++) if (mask[k]<min) min=mask[k];
return min;
}
void nNeighbor(double *in,double d1,double d2,int WIDTH,int HEIGHT){
int i,j;
for (j=0;j<HEIGHT;j++)
for(i=0;i<WIDTH;i++)
in[j*WIDTH + i] = MINforward(in,d1,d2,i,j,WIDTH,HEIGHT);
for (j=HEIGHT-1;j>-1;j--)
for(i=WIDTH-1;i>-1;i--)
in[j*WIDTH + i]=MINbackward(in,d1,d2,i,j,WIDTH,HEIGHT);
}
void ComputeDistanceTransform(double *in,double *out,int WIDTH,int HEIGHT) {
EdgeDetect(in,out,WIDTH,HEIGHT);
nNeighbor(out,1,1.351,WIDTH,HEIGHT);
PutSign(in,out,WIDTH,HEIGHT);
}
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *in, *out;
int iWidth, iHeight;
/* Check for proper number of arguments. */
if (nrhs != 1) {
mexErrMsgTxt("One input required.");
} else if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments");
}
/* The input must be a noncomplex scalar double.*/
iWidth = mxGetM(prhs[0]);
iHeight = mxGetN(prhs[0]);
/* Create matrix for the return argument. */
plhs[0] = mxCreateDoubleMatrix(iWidth,iHeight, mxREAL);
/* Assign pointers to each input and output. */
in = mxGetPr(prhs[0]);
out = mxGetPr(plhs[0]);
ComputeDistanceTransform(in, out, iWidth, iHeight);
}