-
Notifications
You must be signed in to change notification settings - Fork 0
/
watershed.c
121 lines (119 loc) · 2.43 KB
/
watershed.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
//This is the fast version of watershed algorithm..
#include <stdio.h>
#include <stdlib.h>
#include "point.h"
#include "image.h"
#include "show_image.h"
#include "heap.h"
#include "morphology.h"
#include "watershed.h"
//******************************************Comparison***********************************
int comparenode(void *p1,void *p2)
{
Wnode *n1,*n2;
n1=(Wnode *)p1;
n2=(Wnode *)p2;
if(n1->intensity>n2->intensity)
return 1;
else if(n1->intensity<n2->intensity)
return -1;
return 0;
}
//******************************************Impose minimum*******************************
void imimposemin(GrayImage *img,GrayImage *marker,GrayImage *result)
{
int i,j;
for(i=0;i<img->row;i++)
{
for(j=0;j<img->col;j++)
{
if(marker->f[i][j]!=0)
{
result->f[i][j]=0;
}
else
result->f[i][j]=img->f[i][j];
}
}
}
//******************************************Fast watershed*******************************
void watershed(GrayImage *img,GrayImage *result)
{
GrayImage S,Sm,marker;
Heap Sd;
Wnode *n;
int **SE=strel("111111111",3);
int N[8][2]={{1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
int *labels,nL,i,j,heapsize=0,ix,iy;
initGrayImage(&marker,img->row,img->col,img->maxval);
Sm=imcomplement(img);
imregionalmax(&Sm,&marker);
freeImage(&Sm);
/*Sm=imcomplement(&marker);
for(i=0;i<img->row;i++)
{
for(j=0;j<img->col;j++)
{
marker.f[i][j]=Sm.f[i][j];
}
}
freeImage(&Sm);*/
S=imlabel(&marker,&labels,&nL);
Sm=imerode(&marker,SE,3);
freeImage(&marker);
for(i=0;i<img->row;i++)
{
for(j=0;j<img->col;j++)
{
if(Sm.f[i][j]!=img->maxval)
heapsize++;
}
}
initHeap(&Sd,sizeof(Wnode),heapsize);
n=(Wnode *)malloc(sizeof(Wnode));
for(i=0;i<img->row;i++)
{
for(j=0;j<img->col;j++)
{
if(Sm.f[i][j]!=img->maxval)
{
n->p.x=i;
n->p.y=j;
n->intensity=img->f[i][j];
insert2Heap(&Sd,n,sizeof(Wnode),comparenode);
}
}
}
free(n);
while(!emptyH(&Sd))
{
n=(Wnode *)del4mHeap(&Sd,comparenode);
for(i=0;i<8;i++)
{
ix=n->p.x+N[i][0];
iy=n->p.y+N[i][1];
if(ix>=0 && ix<img->row && iy>=0 && iy<img->col)
{
if(S.f[ix][iy]==0)
S.f[ix][iy]=S.f[i][j];
}
}
free(n);
}
for(i=0;i<img->row;i++)
{
for(j=0;j<img->col;j++)
{
result->f[i][j]=S.f[i][j];
}
}
}
main(int argc,char **argv)
{
GrayImage img,marker,temp;
readPGM(argv[1],&img);
readPGM(argv[2],&marker);
initGrayImage(&temp,img.row,img.col,img.maxval);
watershed(&img,&temp);
label2rgb(&temp);
}