-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.h
212 lines (172 loc) · 5.07 KB
/
utils.h
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* Extract distance matrix from an image.
* This was just used for testing.
*/
#ifndef UTILS_H
#define UTILS_H
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <math.h> // for sqrt
#include <assert.h>
static struct {
int size, alloc;
CvPoint *pts;
} s_ptsvec;
static void
ptsvec_init() {
s_ptsvec.pts = malloc(sizeof(s_ptsvec.pts[0]) * 1000);
assert(s_ptsvec.pts);
s_ptsvec.size = 0;
s_ptsvec.alloc = 1000;
}
static void
ptsvec_append(CvPoint pt) {
if (!(s_ptsvec.size < s_ptsvec.alloc)) {
s_ptsvec.pts = realloc(s_ptsvec.pts, sizeof(s_ptsvec.pts[0]) * (s_ptsvec.alloc + 1000));
s_ptsvec.alloc += 1000;
assert(s_ptsvec.pts);
}
s_ptsvec.pts[ s_ptsvec.size++ ] = pt;
}
static void
ptsvec_release() {
free(s_ptsvec.pts);
s_ptsvec.size = 0;
s_ptsvec.alloc = 0;
}
static int
ptsvec_size() {
return s_ptsvec.size;
}
static double
pts_dist(const CvPoint p0, const CvPoint p1) {
return sqrt((double)((p0.x - p1.x) * (p0.x - p1.x) + (p0.y - p1.y) * (p0.y - p1.y)));
}
static void
ptsvec_wtf(const char *dmname, const char *pixname) {
FILE *f = fopen(pixname, "w");
const int size = s_ptsvec.size;
double *dm = 0;
int i = 0, j = 0;
assert(f);
for (; i < size; ++i) {
fprintf(f, "%d %d\n", s_ptsvec.pts[i].x, s_ptsvec.pts[i].y);
}
fclose(f);
dm = malloc(sizeof(dm[0]) * size * size);
assert(dm);
for (i = 0; i < size; ++i) {
for (j = 0; j < size; ++j) {
dm[i * size + j] = pts_dist(s_ptsvec.pts[i], s_ptsvec.pts[j]);
}
}
f = fopen(dmname, "w");
assert(f);
fprintf(f, "%d %d\n", size, size);
for (i = 0; i < size; ++i) {
for (j = 0; j < size; ++j) {
fprintf(f, "%lf ", dm[i * size +j]);
}
fprintf(f, "\n");
}
fclose(f);
free(dm);
}
#define is_foreground(p) (p != 0)
/* Extract the foreground pixel's location to form a distance matrix.
* This will generate two files: the distance matrix file, the foreground
* pixels file.
*/
static int
extract_dm(const char *imgname, const char *dmname, const char *pixname) {
IplImage *img = cvLoadImage(imgname, 0);
int i, j, ws;
unsigned char *data;
assert(img);
ptsvec_init();
ws = img->widthStep;
data = img->imageData;
for (i = 0; i < img->height; ++i) {
for (j = 0; j < img->width; ++j) {
if ( is_foreground(data[i*ws + j]) ) {
ptsvec_append(cvPoint(j, i));
}
}
}
assert( ptsvec_size() > 0 );
ptsvec_wtf(dmname, pixname);
ptsvec_release();
cvReleaseImage(&img);
return 0;
}
#ifndef ABS
#define ABS(v) ((v) > 0 ? (v) : -(v))
#endif
static CvScalar s_colors[16] = {
{255, 255, 255, 0},
{255, 0, 255, 0},
{255, 255, 0, 0},
{0, 255, 255, 0},
{0, 0, 255, 0},
{255, 0, 0, 0},
{0, 255, 0, 0},
{50, 50, 255, 0},
{255, 50, 255, 0},
{20, 255, 80, 0},
{100, 100, 255, 0},
{90, 255, 0, 0},
{33, 99, 15, 0},
{200, 90, 0, 0},
{99, 0, 200, 0},
{100, 25, 85, 0},
};
static CvScalar
colors_get(int i) {
return s_colors[ i & 0x0f ];
}
static void
show_result_in_image(const char *imgname, const char *posname, const char *labelname) {
IplImage *img = cvLoadImage(imgname, 1); // load color image
IplImage *draw = cvCreateImage(cvGetSize(img), 8, 3);
FILE *f;
int i, j;
int *labels;
assert(img && draw);
f = fopen(posname, "r");
assert(f);
ptsvec_init();
while (2 == fscanf(f, "%d %d", &i, &j)) {
ptsvec_append(cvPoint(i, j));
}
fclose(f);
labels = malloc(sizeof(labels[0]) * ptsvec_size());
assert(labels);
f = fopen(labelname, "r");
assert(f);
j = ptsvec_size();
for (i = 0; i < j; ++i) {
if (1 != fscanf(f, "%d", &labels[i])) {
assert(0);
}
}
cvSetZero(draw);
// show result
j = ptsvec_size();
for (i = 0; i < j; ++i) {
cvCircle(draw, s_ptsvec.pts[i], 2, colors_get(labels[i]), 2, CV_AA, 0);
}
cvNamedWindow("original", 1);
cvNamedWindow("result", 1);
cvShowImage("original", img);
cvShowImage("result", draw);
cvWaitKey(0);
cvDestroyWindow("original");
cvDestroyWindow("result");
cvSaveImage("./data/result.bmp", draw, 0);
free(labels);
fclose(f);
ptsvec_release();
cvReleaseImage(&img);
cvReleaseImage(&draw);
}
#endif