-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTestChanVese.cpp
76 lines (66 loc) · 2.19 KB
/
TestChanVese.cpp
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
// Robert Crandall
#include "ReadWritePBM.h"
#include "ChanVeseSegmentation.h"
#include "ZeroCrossings.h"
int main()
{
cout << "Reading in image...\n";
Image<unsigned char>* img = ReadP5image("test.pgm");
Image<unsigned char>* imageOut = new Image<unsigned char>(img->nRows(),
img->nCols());
Image<double>* phi0 = new Image<double>(img->nRows(),
img->nCols());
Image<double>* phi = new Image<double>(img->nRows(),
img->nCols());
Image<unsigned char>* edges = new Image<unsigned char>(img->nRows(),
img->nCols());
// Set up parameters (don't hardcode this!)
struct CVsetup* pCVinputs = new struct CVsetup;
pCVinputs->dt = 0.1;
pCVinputs->h = 1.0;
pCVinputs->lambda1 = 1.0;
pCVinputs->lambda2 = 1.0;
pCVinputs->mu = 0.5;
pCVinputs->nu = 0;
pCVinputs->p = 1;
// Set up initial circular contour for a 256x256 image
double x;
double y;
for (unsigned int i = 0; i < img->nRows(); ++i)
{
for (unsigned int j = 0; j < img->nCols(); ++j)
{
x = double(i) - img->nRows()/2.0;
y = double(j) - img->nCols()/2.0;
phi0->data()[i][j] = 900.0/(900.0 + x*x + y*y) - 0.5;
}
}
cout << "Performing segmentation...\n";
ChanVeseSegmentation(img,phi0, &phi, pCVinputs);
cout << "Getting zero crossings...\n";
ZeroCrossings(phi,&edges,(unsigned char)255,(unsigned char)0);
imageOut->CopyFrom(img);
for (unsigned int i = 0; i < img->nRows(); ++i)
{
for (unsigned int j = 0; j < img->nCols(); ++j)
{
if (edges->data()[i][j] == (unsigned char)255)
imageOut->data()[i][j] = (unsigned char)255;
}
}
cout << "Writing output...\n";
WriteP5image(imageOut,"CVoutEdges.pgm",255);
imageOut->CopyFrom(img);
for (unsigned int i = 0; i < img->nRows(); ++i)
{
for (unsigned int j = 0; j < img->nCols(); ++j)
{
if (phi->data()[i][j] >= 0)
imageOut->data()[i][j] = (unsigned char)0;
else
imageOut->data()[i][j] = (unsigned char)255;
}
}
WriteP5image(imageOut,"CVoutRegions.pgm",255);
cout << "Done.\n";
}