-
Notifications
You must be signed in to change notification settings - Fork 3
/
FTRMDL.h
179 lines (168 loc) · 4.99 KB
/
FTRMDL.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
#ifndef _FTREntropy_H__
#define _FTREntropy_H__
#include <FTRBase.h>
class FTREntropy: public FTRBase{
public:
FTREntropy(const Image & image_, double w_smooth_, double w_bits_, int num_comp_);
virtual void computeenergy(const Table2D<Label> & labeling,
double * act_energy_p, double * app_energy_p);
// collect statistics about current labeling and update approximate model
virtual void updatemodel();
virtual GraphType * ftrbasegraph(double lambda_);
double colorseparationcost(const Table2D<Label> & labeling) const;
double trivialsolutionenergy()const;
private:
double w_smooth;
double w_bits;
// for color separation term
int num_comp; // number of components for approximating JS color separation term
vector<double> thetamaxes, slopes;
Image image;
vector<int> ROIhist;
double JSoffset; // n_k*log(n_k) sum over k
// appriximate model: a*|s| + b
double app_a;
double app_b;
Table2D<double> dtOBJ;
Table2D<double> dtBKG;
Table2D<double> dt;
};
FTREntropy::FTREntropy(const Image & image_, double w_smooth_, double w_bits_, int num_comp_)
:image(image_),w_smooth(w_smooth_),w_bits(w_bits_),num_comp(num_comp_)
{
cout<<"number of breakpoints: "<<num_comp<<endl;
char txtfilename[100];
strcpy(txtfilename,"D:/repositories/openSEG/JS");
strcat(txtfilename, "/thetamaxslope");
char anumbrk[3];
itoa(num_comp, anumbrk, 10);
strcat(txtfilename, anumbrk);
strcat(txtfilename, ".txt");
cout<<txtfilename<<endl;
Table2D<double> thetamaxslopetable = readtxtfile(txtfilename,num_comp,2);
thetamaxes = vector<double>(num_comp,0);
slopes = vector<double>(num_comp,0);
for(int i=0;i<num_comp;i++)
{
thetamaxes[i] = thetamaxslopetable[0][i];
slopes[i] = thetamaxslopetable[1][i];
}
}
void FTREntropy::computeenergy(const Table2D<Label> & labeling,
double * act_energy_p, double * app_energy_p)
{
if(ROIhist.size()==0)
{
// ROI compacthist
ROIhist = vector<int>(image.colorbinnum,0);
for(int x =0; x< img_w;x++)
{
for(int y=0;y<img_h;y++)
{
if(ROI[x][y])
{
ROIhist[image.colorlabel[x][y]]++;
}
}
}
JSoffset = 0;
for(int i=0;i<image.colorbinnum;i++)
{
if(ROIhist[i]) JSoffset += ROIhist[i]*log((double)ROIhist[i])/log(2.0);
}
}
double smooth_cost = getsmoothnesscost(image,labeling);
int objsize = countintableROI(labeling,OBJ,ROI);
int bkgsize = countintableROI(labeling,BKG,ROI);
double volume_cost = objsize*log(max((double)objsize,1.0))+bkgsize*log(max((double)bkgsize,1.0));
volume_cost = volume_cost/log(2.0);
double separation_cost = colorseparationcost(labeling);
separation_cost -= JSoffset;
double act_energy = w_smooth*smooth_cost+w_bits*(volume_cost+separation_cost);
*act_energy_p = act_energy;
if(app_energy_p)
{
double app_volume_cost = app_a*objsize + app_b;
double app_energy = w_smooth*smooth_cost+w_bits*(app_volume_cost+separation_cost);
*app_energy_p = app_energy;
}
}
void FTREntropy::updatemodel()
{
int S0 = countintableROI(current_labeling,OBJ,ROI);
S0 = max(1,S0); // make sure S0 is not zero
S0 = min(ROISize-1,S0); // make sure S0 is not ROI_size
app_a = log((double)S0/(ROISize-S0))/log(2.0);
app_b = (S0*log((double)S0)+(ROISize-S0)*log((double)ROISize-S0))/log(2.0)-S0*app_a;
dt = getDistanceTransform(replacelabeling(current_labeling, OBJ));
//dtOBJ = getDistanceTransform(replacelabeling(current_labeling, OBJ));
//dtBKG = getDistanceTransform(replacelabeling(current_labeling, BKG));
//dt = dtOBJ;
/*for(int i=0;i<img_w;i++)
{
for(int j=0;j<img_h;j++)
{
if(current_labeling[i][j]==BKG)
dt[i][j] = - dtBKG[i][j];
}
}*/
}
GraphType * FTREntropy::ftrbasegraph(double lambda_)
{
int colorbinnum = image.colorbinnum;
int N = img_w*img_h;
GraphType * g = new GraphType(/*estimated # of nodes*/ N+2*num_comp*colorbinnum,
/*estimated # of edges*/ (4+2*num_comp)*N);
g->add_node(N+2*num_comp*colorbinnum); // adding nodes
addsmoothnessterm(g, image, w_smooth, ROI);
for(int i=0;i<img_w;i++)
{
for(int j=0;j<img_h;j++)
{
int n = i+j*img_w;
g->add_tweights(n,dt[i][j]*lambda_,app_a);
}
}
addJSseparationterm(g, image.colorlabel,w_bits*2, ROI, num_comp, ROIhist, thetamaxes, slopes);
return g;
}
double FTREntropy::colorseparationcost(const Table2D<Label> & labeling) const
{
int colorbinnum = image.colorbinnum;
vector<int> OBJhist(colorbinnum,0);
vector<int> BKGhist(colorbinnum,0);
for(int i=0;i<img_w;i++)
{
for(int j=0;j<img_h;j++)
{
if((labeling[i][j]==OBJ)&&(ROI[i][j]))
OBJhist[image.colorlabel[i][j]]++;
else if((labeling[i][j]==BKG)&&(ROI[i][j]))
BKGhist[image.colorlabel[i][j]]++;
}
}
double returnv = 0.0;
double theta=0,slope = 0;
for(int k=0;k<colorbinnum;k++)
{
int nk = ROIhist[k];
if(nk)
{
int nsk = OBJhist[k];
int nsbark = BKGhist[k];
for(int i=0;i<num_comp;i++)
{
theta = thetamaxes[i];
slope = slopes[i];
double v = min(theta*nk,min(slope*nsk*2,(slope*nsbark*2)));;
returnv += v;
}
}
}
return returnv;
}
double FTREntropy::trivialsolutionenergy()const
{
return ROISize * log(max(1.0,(double)ROISize))/log(2.0);
}
#endif