segmentation
All Data Structures Namespaces Files Functions Variables Modules Pages
BgEdgeList.cpp
1 
3 // Name: BgEdgeList.cpp
4 
5 // Purpose: BgEdgeList class functions
6 
7 // Author: Bogdan Georgescu
8 
9 // Modified by:
10 
11 // Created: 06/22/2000
12 
13 // Copyright: (c) Bogdan Georgescu
14 
15 // Version: v0.1
16 
18 
19 
20 
21 #include <stdio.h>
22 
23 #include <stdlib.h>
24 
25 #include <math.h>
26 
27 #include "edge/BgDefaults.h"
28 
29 #include "edge/BgImage.h"
30 
31 #include "edge/BgEdge.h"
32 
33 #include "edge/BgEdgeList.h"
34 
35 
36 
37 BgEdgeList::BgEdgeList()
38 
39 {
40 
41  nEdges_ = 0;
42 
43  edgelist_ = 0;
44 
45  crtedge_ = 0;
46 
47 }
48 
49 
50 
51 BgEdgeList::~BgEdgeList()
52 
53 {
54 
55  if (nEdges_>0)
56 
57  {
58 
59  BgEdge* edge;
60 
61  for (int i=0; i<nEdges_; i++)
62 
63  {
64 
65  edge = edgelist_->next_;
66 
67  delete edgelist_;
68 
69  edgelist_=edge;
70 
71  }
72 
73  }
74 
75 }
76 
77 
78 
79 void BgEdgeList::AddEdge(float* edge, int nPoints)
80 
81 {
82 
83  BgEdge* tedge;
84 
85  tedge = new BgEdge();
86 
87  tedge->SetPoints(edge, nPoints);
88 
89  if (nEdges_==0)
90 
91  {
92 
93  nEdges_ = 1;
94 
95  edgelist_ = tedge;
96 
97  crtedge_ = tedge;
98 
99  }
100 
101  else
102 
103  {
104 
105  nEdges_++;
106 
107  crtedge_->next_ = tedge;
108 
109  crtedge_ = tedge;
110 
111  }
112 
113 }
114 
115 
116 
117 void BgEdgeList::AddEdge(int* edge, int nPoints)
118 
119 {
120 
121  BgEdge* tedge;
122 
123  tedge = new BgEdge();
124 
125  tedge->SetPoints(edge, nPoints);
126 
127  if (nEdges_==0)
128 
129  {
130 
131  nEdges_ = 1;
132 
133  edgelist_ = tedge;
134 
135  crtedge_ = tedge;
136 
137  }
138 
139  else
140 
141  {
142 
143  nEdges_++;
144 
145  crtedge_->next_ = tedge;
146 
147  crtedge_ = tedge;
148 
149  }
150 
151 }
152 
153 
154 
155 void BgEdgeList::SetGradient(float* grx, float* gry, float* mark, int ncol)
156 
157 {
158 
159  BgEdge* it;
160 
161  int i;
162 
163 
164 
165  it=edgelist_;
166 
167  for (i=0; i<nEdges_; i++)
168 
169  {
170 
171  it->SetGradient(grx, gry, mark, ncol);
172 
173  it = it->next_;
174 
175  }
176 
177 }
178 
179 
180 
181 void BgEdgeList::RemoveShortEdges(int minp)
182 
183 {
184 
185  if (nEdges_==0)
186 
187  return;
188 
189 
190 
191  int nEdges=nEdges_;
192 
193  BgEdge* it1;
194 
195  BgEdge* it2;
196 
197  it1 = edgelist_;
198 
199  it2 = it1->next_;
200 
201 
202 
203  for (int i=1; i<nEdges_; i++)
204 
205  {
206 
207  if (it2->nPoints_ < minp)
208 
209  {
210 
211  it1->next_ = it2->next_;
212 
213  delete it2;
214 
215  it2 = it1->next_;
216 
217  nEdges--;
218 
219  }
220 
221  else
222 
223  {
224 
225  it1 = it2;
226 
227  it2 = it1->next_;
228 
229  }
230 
231  }
232 
233 
234 
235  if (edgelist_->nPoints_ < minp)
236 
237  {
238 
239  it1 = edgelist_;
240 
241  edgelist_ = edgelist_->next_;
242 
243  delete it1;
244 
245  nEdges--;
246 
247  }
248 
249  nEdges_=nEdges;
250 
251 }
252 
253 
254 
255 void BgEdgeList::SetBinImage(BgImage* image)
256 
257 {
258 
259  int i, j;
260 
261  int ix, iy;
262 
263  int x, y;
264 
265 
266 
267  x = image->x_;
268 
269  y = image->y_;
270 
271  unsigned char* im=image->im_;
272 
273 
274 
275  for (i=0; i<x; i++)
276 
277  {
278 
279  for (j=0;j<y;j++)
280 
281  {
282 
283  *(im++) = 0;
284 
285  }
286 
287  }
288 
289 
290 
291  im = image->im_;
292 
293  int* ite;
294 
295  crtedge_=edgelist_;
296 
297  for (i=0; i<nEdges_; i++)
298 
299  {
300 
301  ite = crtedge_->edge_;
302 
303  for (j=0; j<crtedge_->nPoints_; j++)
304 
305  {
306 
307  ix = *(ite++);
308 
309  iy = *(ite++);
310 
311  *(im+iy*x+ix) = 255;
312 
313  }
314 
315  crtedge_=crtedge_->next_;
316 
317  }
318 
319 }
320 
321 
322 
323 bool BgEdgeList::SaveEdgeList(char* edgeFile)
324 
325 {
326 
327  int length;
328 
329  int i,j;
330 
331  BgEdge *crtedge;
332 
333 
334 
335  FILE* fp;
336 
337  fp=fopen(edgeFile,"wb");
338 
339  crtedge = edgelist_;
340 
341  for (i=0; i<nEdges_; i++)
342 
343  {
344 
345  length = crtedge->nPoints_;
346 
347  for (j=0; j<length; j++)
348 
349  {
350 
351  fprintf(fp, "%d %d %d\n", *((crtedge->edge_)+2*j), *((crtedge->edge_)+2*j+1), i);
352 
353  }
354 
355  crtedge = crtedge->next_;
356 
357  }
358 
359  fclose(fp);
360 
361  return true;
362 
363 }
364 
365 
366 
367 void BgEdgeList::GetAllEdgePoints(int* x, int* y, int* n)
368 
369 {
370 
371  int length;
372 
373  int i,j;
374 
375  BgEdge *crtedge;
376 
377  int *edgep;
378 
379 
380 
381  crtedge = edgelist_;
382 
383  *n = 0;
384 
385  for (i=0; i<nEdges_; i++)
386 
387  {
388 
389  length = crtedge->nPoints_;
390 
391  edgep = crtedge->edge_;
392 
393  for (j=0; j<length; j++)
394 
395  {
396 
397  x[*n] = edgep[2*j];
398 
399  y[*n] = edgep[2*j+1];
400 
401  (*n)++;
402 
403  }
404 
405  crtedge = crtedge->next_;
406 
407  }
408 
409 }
410 
411 
412 
413 void BgEdgeList::SetNoMark(void)
414 
415 {
416 
417  int length;
418 
419  int i,j;
420 
421  BgEdge* crtedge;
422 
423  unsigned char* mark;
424 
425  crtedge = edgelist_;
426 
427  for (i=0; i<nEdges_; i++)
428 
429  {
430 
431  length = crtedge->nPoints_;
432 
433  mark = crtedge->mark_ = new unsigned char[length];
434 
435  crtedge->isMarkSet_ = true;
436 
437  for (j=0; j<length; j++)
438 
439  {
440 
441  *(mark+j) = 0;
442 
443  }
444 
445  crtedge = crtedge->next_;
446 
447  }
448 
449 }
450