download watershed.c
Language: C
License: GPL
Copyright: (C) 1998 Gloria Bueno (C) 1998,1999,2000,2001,2002 Yohann Sulaiman. (C) 2002 Free Software Foundation, Inc.
LOC: 320
Project Info
MultiSlice RTP Environment(yhs)
Server: SourceForge
Type: cvs
...eForge\y\yhs\yhs\root\root\
   app_glob.c
   command_run.c
   command_slice.c
   ctomass.c
   edge.c
   editing_tools.c
   exit.c
   FILE_ID.DIZ
   file_viewer.c
   file2.raw
   filters.c
   glxdino.c
   glxmotif.c
   glxsimple.c
   gpl.html
   icon.h
   initapp.c
   inject.c
   label.c
   log.h
   logmen.h
   logo.h
   main.h
   makefile.32
   makefile.64
   makefile.f32
   measure.c
   menu.c
   multislice.c
   neighbour.c
   open.c
   open.old.c
   paperplane.c
   print.c
   program1.c
   region.c
   region_ops.c
   save.c
   slice.c
   split_merge.c
   textfun.c
   version.c
   viewslice.c
   wals.study
   watershed.c
   xbutton.c
   xmenu.c
   xwins.h
   xwinutil.c
   ys_ext.c
   zoom.c
   zoom2.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
  /*

MultiSlice RTP Environment
(C) 1998,1999,2000,2001,2002 Yohann Sulaiman.
(C) 1998 Gloria Bueno
(C) 2002  Free Software Foundation, Inc. 


    This file is part of the source code of the MultiSlice RTP Environment.

    MultiSlice RTP Environment is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    MultiSlice RTP Environment  is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MultiSlice RTP Environment; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/


/*--------------------------------------------------------------*/
/*								*/
/*     File: watershed.c					*/
/*								*/
/*     CT Image Application Program				*/
/*								*/
/*     OSF/Motif version.					*/
/*								*/
/*     V1.0 March 1996						*/
/*								*/
/*--------------------------------------------------------------*/

#include "main.h"

#include <Xm/Xm.h>
#include <Xm/SelectioB.h>

#define MAX_PIXELS (IMAGE_WIDTH*IMAGE_HEIGHT)
#define WSHED 0
#define MASK  1
#define INIT  2


typedef struct {		/* structure for FIFO stack */
          TPINFO pix_info[MAX_PIXELS];
          unsigned long head, tail;
} FIFO;


/* Extern variables */

extern char 	*action;
extern int 	selection;
extern XImage 	*theXImage_1;
extern XImage 	*theXImage_2;
extern unsigned long bg, fg;
extern Pixmap	thePixmap_1;
extern Pixmap	thePixmap_2;
extern Cursor	theCursor;
extern GC	image_gc_2, theGC;
extern Widget 	draw_1,
		draw_2,
		main_window;
extern int 	array1[512][512];
extern int 	array2[512][512];
extern int	yhs_filename[21];
extern char 	*yhs_filename1;
extern char     *tempfileold;
extern char     *tempfilenew;
extern char	*addcharac;
extern int      file_loader;
extern int      run_once;
extern char     *file_yhs;
extern int 	yhs_files_open;

/* Variables for setting resources */

static NODEPTR 	p, list_pixels[COLORMAP_SIZE];
static unsigned long intensity;


/* Function prototypes */
void watershed_callback(Widget w, XtPointer client_data,
		XmAnyCallbackStruct *call_data);
static void watershed_contour();
static void create_p_list(NODEPTR *list);
static void insert_p_list(NODEPTR *list, TPINFO *info);
static void view_p_list(NODEPTR list);
static void free_p_list(NODEPTR *list);
static void fifo_init(FIFO *pixel_q);
static void fifo_add(TPINFO p, FIFO *pixel_q);
static TPINFO fifo_first(FIFO *pixel_q);
static int fifo_empty(FIFO *pixel_q);
static int image_pixel(int x, int y);
static int labeled_neighbour(int x, int y,
		unsigned long o[IMAGE_WIDTH][IMAGE_HEIGHT]);
extern void refresh_action(void);
extern void prepare_handlers (int sel);
extern void activate_total_gradient_dialog(Widget w, XtPointer client_data,
		XmAnyCallbackStruct *call_data);



/* Function definition */

/*----------------------------------------------------------------------*/
/*  s t a r t  _  w a t e r s h e d					*/
/*									*/
/*  Event handler to start watershed procedure				*/
/*----------------------------------------------------------------------*/
void watershed_callback(Widget w, XtPointer client_data,
		XmAnyCallbackStruct *call_data)
{
   int i,j;
   FILE *p_file;
   int a,b;
if (yhs_filename[0] >= 1 && yhs_files_open > 0 && yhs_files_open < 20)
{
      /* Fill in the image with background color */
  /*    for(i=0; i<IMAGE_WIDTH; i++)
          for(j=0; j<IMAGE_HEIGHT; j++)
               XPutPixel(theXImage_2,i,j,bg); */

      /* Clear the drawing window so the image is displayed again */
/*      XClearArea(XtDisplay(draw_1),XtWindow(draw_2),0,0,0,0,True); */

      /* Associate the watch cursor with the main window */
/*      XDefineCursor(XtDisplay(draw_1), XtWindow(main_window), theCursor); */

      /* Flush the request buffer and wait for all events */
      /* and errors to be processed by the server.        */
/*      XSync(XtDisplay(draw_1), False); */
 
 for (a=0;a<512;a++){
    for (b=0;b<512;b++)
    { array1[a][b]=array2[a][b];}}
      watershed_contour();

      /* Clear the drawing window so the image is displayed again */
/*      XClearArea(XtDisplay(draw_1),XtWindow(draw_1),0,0,0,0,True); */

      /* Disassociate the watch cursor from the main window */
/*      XUndefineCursor(XtDisplay(draw_1), XtWindow(main_window)); */
      /* Copy image into pixmap */
/*      XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2, theXImage_2, 
                      0, 0, 0, 0, theXImage_2->width, theXImage_2->height); */
      /* Clear the drawing window so the image is displayed again */
/*      XClearArea(XtDisplay(draw_1),XtWindow(draw_2),0,0,0,0,True); */

       strcpy(tempfileold,tempfilenew); /* copy the new filename to the old file i.e. save the old file */
       strcat(tempfilenew,addcharac); /* new free temp file */
  if((p_file = fopen(tempfileold,"w")) == NULL)
    {
        fprintf(stderr, "Cannot open: %s - Temporary file for writing.\n",tempfileold);
        exit(1);
    }
    for (a=0;a<512;a++){
    for (b=0;b<512;b++)
    { putc(array2[a][b],p_file); }}
    fclose(p_file);
    run_once=0;
    file_loader=1;
    file_yhs=tempfileold;
}
}



/*-------------------------------------------------------------*/
/*  c r e a t e _ p _ l i s t                                  */
/*                                                             */
/*  Initialise the list                                        */
/*-------------------------------------------------------------*/
void create_p_list(NODEPTR *list)
{
   *list = NULL;
}


/*-------------------------------------------------------------*/
/*  i n s e r t _ p _ l i s t                                  */
/*                                                             */
/*  Insert a new node in the list                              */
/*-------------------------------------------------------------*/
void insert_p_list(NODEPTR *list, TPINFO *info)
{
    NODEPTR newone;

    newone = (NODEPTR) malloc(sizeof(NODE)); 
    newone->infolist.coord_x = info->coord_x;
    newone->infolist.coord_y = info->coord_y;
    newone->infolist.grey_level = info->grey_level;
    newone->next = NULL;

    if (*list == NULL) {
	*list = newone;
    } else {
	newone->next = *list;
	*list = newone;
    }
}



/*-------------------------------------------------------------*/
/*  v i e w _ p _ l i s t                                      */
/*                                                             */
/*  Copy the pixels  to the pixmap                             */
/*-------------------------------------------------------------*/
void view_p_list(NODEPTR list)
{
    NODEPTR p;

    p = list;
    while (p != NULL) {
	XPutPixel(theXImage_2,p->infolist.coord_x, 
		p->infolist.coord_y,p->infolist.grey_level);
	p = p->next;
    }
}



/*-------------------------------------------------------------*/
/*  f r e e _ p _ l i s t                                      */
/*                                                             */
/*  Release the memory allocated by the list                   */
/*-------------------------------------------------------------*/
void free_p_list(NODEPTR *list)
{
    NODEPTR p;

    while (*list != NULL) {
	p = *list;
	*list = (*list)->next;
	free(p);
    }
}



/*-------------------------------------------------------------*/
/*  f i f o _ i n i t                                          */
/*                                                             */
/*  writes value 0 to fifo array, head = tail = 0              */
/*-------------------------------------------------------------*/
void fifo_init(FIFO *pixel_q)
{
    unsigned long i;

    for (i=0; i<MAX_PIXELS; i++) {
	pixel_q->pix_info[i].grey_level = 0;
	pixel_q->pix_info[i].coord_x = 0;
	pixel_q->pix_info[i].coord_y =0;
    }
    pixel_q->head = 0;
    pixel_q->tail = 0;
}



/*-------------------------------------------------------------*/
/*  f i f o _ a d d                                            */
/*                                                             */
/*  adds a pixel to the fifo stack                             */
/*-------------------------------------------------------------*/
void fifo_add(TPINFO p, FIFO *pixel_q)
{
    unsigned long t;

    t = pixel_q->tail;  
    pixel_q->pix_info[t].grey_level = p.grey_level;
    pixel_q->pix_info[t].coord_x = p.coord_x;
    pixel_q->pix_info[t].coord_y = p.coord_y;
    t = t+1;
    pixel_q->tail = t % MAX_PIXELS;
}



/*-------------------------------------------------------------*/
/*  f i f o _ f i r s t                                        */
/*                                                             */
/*  removes a pixel from the fifo stack                        */
/*-------------------------------------------------------------*/
TPINFO fifo_first(FIFO *pixel_q)
{
    unsigned long h;
    TPINFO p;

    h = pixel_q->head;
    p = pixel_q->pix_info[h];
    h = h + 1;
    pixel_q->head = h % MAX_PIXELS;
    return p;
}



/*-------------------------------------------------------------*/
/*  f i f o _ e m p t y                                        */
/*                                                             */
/*  returns true if fifo is empty                              */
/*-------------------------------------------------------------*/
int fifo_empty(FIFO *pixel_q)
{
    unsigned long h,t;

    h = pixel_q->head;
    t = pixel_q->tail;
    return (h==t);
}
 


/*-------------------------------------------------------------*/
/*  i m a g e _ p i x e l                                      */
/*                                                             */
/*  returns true if pixel lies within the image window         */
/*-------------------------------------------------------------*/
int image_pixel(int x, int y)
{
    if ( x>=0 && x<IMAGE_WIDTH && y>=0 && y<IMAGE_HEIGHT )
	return(TRUE);
    else 
	return(FALSE);
}  



/*-------------------------------------------------------------*/
/*  l a b e l e d _ n e i g h b o u r                          */
/*                                                             */
/*  returns true if pixel has 8-neighbour which is part of     */
/*  labeled region or watershed                                */
/*-------------------------------------------------------------*/
int labeled_neighbour(int x, int y,
		unsigned long o[IMAGE_WIDTH][IMAGE_HEIGHT])
{
    int i,j,r;

    r = FALSE;
    for (i=-1; i<=1; i++)
	for (j=-1; j<=1; j++) {
	    if ((image_pixel(x+i,y+j)==TRUE) && ((i==0&&j==0)==FALSE)) {
		if ((o[x+i][y+j]>2) || (o[x+i][y+j]==WSHED)) r=TRUE;
	}
    }
    return(r);
}



/*-------------------------------------------------------------*/
/*  s n a k e _ c o n t o u r                                  */
/*                                                             */
/*  Throws a closed contour around a pixel                     */
/*-------------------------------------------------------------*/
void watershed_contour()
{
    int s,t,i,j,n,x,y;
    TPINFO p_info, p_dash_info, p_dash_dash_info;
    TPINFO m_info;			/* fictitious marker pixel */
    TPINFO t_info; 
    FIFO pixel_q;
    unsigned long v, output_pixel, current_label = 2; /* values 0,1,2 reserved */
    int current_distance;
    unsigned long d[IMAGE_WIDTH][IMAGE_HEIGHT], o[IMAGE_WIDTH][IMAGE_HEIGHT];
    int forever;
   
    /* initialise fictitious marker with unique values */
    m_info.grey_level = 328;                     /* non existant pixel value */
    m_info.coord_x = IMAGE_WIDTH+10;             /* non existant x value */
    m_info.coord_y = IMAGE_HEIGHT+10;            /* non existant y value */

    /* initialise fifo  */
    fifo_init(&pixel_q);

    /* initialise the array of pointers */
    for (n=0; n<COLORMAP_SIZE; n++)
	create_p_list(&list_pixels[n]);

    /* initialise the output and work images and make the histogram */ 
    for (i=0; i<IMAGE_WIDTH; i++)
	for (j=0; j<IMAGE_HEIGHT; j++) {
	    array2[i][j]=fg; 
	    o[i][j] = INIT;                      /* output image */
	    d[i][j] = 0;                         /* work image */
	    p_info.grey_level = array1[i][j];
	    p_info.coord_x = i;
	    p_info.coord_y = j;
	    insert_p_list(&list_pixels[p_info.grey_level], &p_info);
	}


    /* compute the watershed by incremental thresholding */

    for (n=0; n<COLORMAP_SIZE; n++) {
	p = list_pixels[n];
	while (p != NULL) {
	    x = p->infolist.coord_x;
	    y = p->infolist.coord_y;
	    v = p->infolist.grey_level;
	    o[x][y] = MASK; 
	    /* is p adjacent to labeled region or watershed? */
	    if (labeled_neighbour(x,y,o)) {
	    d[x][y] = 1;
	    fifo_add(p->infolist, &pixel_q);
	}  
	p = p ->next;
    }


    /*  propagate pixel distances  */
  
    current_distance = 1;
    fifo_add(m_info, &pixel_q);
    forever = TRUE;
    while (forever == TRUE) {
	p_info = fifo_first(&pixel_q);
	if (p_info.grey_level == m_info.grey_level) {
	    if (fifo_empty(&pixel_q)) break;
	    else {
		fifo_add(m_info, &pixel_q);
		current_distance++;
		p_info = fifo_first(&pixel_q);
	    }
	}
	x = p_info.coord_x;
	y = p_info.coord_y;
	for (i= -1; i<=1; i++)
	    for (j= -1; j<=1; j++) {
		if ((image_pixel(x+i,y+j)==TRUE)&&((i==0&&j==0)==FALSE)) {
		    output_pixel = o[x+i][y+j];
		    if ((d[x+i][y+j]<current_distance) && 
			(output_pixel>2 || output_pixel==WSHED)) {
			/* neighbour belongs to already labeled */
			/* region or to the watersheds */
			if (output_pixel > 2) {
			    /* neighbour belongs to already labeled region */
			    if (o[x][y]==MASK || o[x][y]==WSHED) {
				o[x][y] = output_pixel;
				/* recheck neighbours to locate possible watershed */
				for (s= -1; s<=1; s++)
				  for (t= -1; t<=1; t++) 
				    if ((image_pixel(x+s,y+t)==TRUE) && 
					((s==0&&t==0)==FALSE) ) 
					if (o[x+s][y+t]>2)
					  if (o[x][y]!=o[x+s][y+t])
					    o[x][y] = WSHED;
			    } 
			    else
			    if (o[x][y] != output_pixel) {
				/* two adjacent pixels belong to different regions */
				o[x][y] = WSHED;
			    }
			}
			else
			/* neighbour belongs to the watersheds */
			if (o[x][y] == MASK) o[x][y] = WSHED;
		    }
		    else
		    if (output_pixel == MASK && d[x+i][y+j] == 0) {
			d[x+i][y+j] = current_distance + 1;
			p_dash_info.grey_level = array1[x+i][y+j];
			p_dash_info.coord_x = x+i;
			p_dash_info.coord_y = y+j;
			fifo_add(p_dash_info, &pixel_q);
		    }
		}    /* if ((image_pixel(x+i,y+j)==TRUE ...*/
	    }    /* for (j=-1; j<-1 ... */
    }    /* while */                
               
    /*  checks if new minima have been discovered */
    p = list_pixels[n];
    while (p != NULL) {
      x = p->infolist.coord_x;
      y = p->infolist.coord_y;
      v = p->infolist.grey_level;
      d[x][y] = 0;
      output_pixel = o[x][y];
      if (output_pixel == MASK) {
        current_label = current_label+1;
        fifo_add(p->infolist, &pixel_q);
        o[x][y] = current_label;
        t_info.coord_x = x;
        t_info.coord_y = y;
        t_info.grey_level = current_label;
        while (fifo_empty(&pixel_q) == FALSE) {
          p_dash_info = fifo_first(&pixel_q);
          x = p_dash_info.coord_x;
          y = p_dash_info.coord_y;
          v = p_dash_info.grey_level;
          for (i= -1; i<=1; i++)
	    for (j= -1; j<=1; j++) {
              if ((image_pixel(x+i,y+j) == TRUE) && 
                  ((i==0 && j==0) == FALSE)) {
                output_pixel = o[x+i][y+j];
                if (output_pixel == MASK) {
                  p_dash_dash_info.grey_level = array1[x+i][y+j];
                  p_dash_dash_info.coord_x = x+i;
                  p_dash_dash_info.coord_y = y+j;
                  fifo_add(p_dash_dash_info, &pixel_q);
                  o[x+i][y+j] = current_label;
	        }
	      }
	    }    /* for */
        }    /* while */
      }    /* if */
      p = p->next;
    }    /* while */
  }



    /* display the image - and the watersheds */

    for (i=0; i<IMAGE_WIDTH; i++)
	for (j=0; j<IMAGE_HEIGHT; j++) {
	    output_pixel = o[i][j];
		if (output_pixel==0) {
		    array2[i][j]=fg;
		} else {
		    intensity = array1[i][j];
		    array2[i][j]=intensity;
		}
	} 


    /* do garbage collection */
    for (n=0; n<COLORMAP_SIZE; n++)
	free_p_list(&list_pixels[n]);     

}

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us