forked from kalxas/mseg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsegcli.cpp
208 lines (174 loc) · 7.67 KB
/
msegcli.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
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* msegcli.cpp: Multiscale SEGmentation Command Line Interface *
* Version: 0.9.x *
* Last revised: 01/01/2006 *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* MSEG algorithm by Angelos Tzotsos ([email protected]) *
* Remote Sensing Lab NTUA - GCpp January 2006 *
* *
* Copyright (C) Angelos Tzotsos <[email protected]> *
* *
* This program 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. *
* *
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
//Usage :mseg <input.ers> <output.ers> <image weights text file> <Level sequence and parameter text file>
//Future Usage : mseg <input.ers> <output.ers> <Level Name> <SuperLevel> <SubLevel>
// <Scale Parameter> <Compactness parameter> <Egde Compensation Parameter>
// <image weights text file> ..........
//The above is for single level creation
//To be implemented in version 1.1
#include "msegcore.h"
#include "msegstd.h"
#include "FreeImage.h"
using namespace std;
int main (int argc, char *argv[]){
int merges_occured = 0;
float average_size = 0.0;
string bound = "boundary";
string rst = "raster";
string xml = "stats";
if(argc != 5){//TODO: Add to Error Handling
cout << "Error in arguments:" << endl << endl
<< "Usage : mseg <input.ers> <output.ers> <image weights text file> <Level sequence and parameter text file>"
<< endl;
cin.get();
exit(1);
}
//read arguments
string arg1(argv[1]);
string arg2(argv[2]);
string arg3(argv[3]);
string arg4(argv[4]);
//Initialize one instance called mseg
MSEG_Init mseg;
//Fill LevelQueue
ifstream LevelQueueList(arg4.c_str());
LevelQueueObj tmpQueueObj;
while(LevelQueueList){
LevelQueueList >> tmpQueueObj.number;
if(!LevelQueueList) break;
LevelQueueList >> tmpQueueObj.sub_level_number;
LevelQueueList >> tmpQueueObj.param.Scale;
LevelQueueList >> tmpQueueObj.param.Color;
LevelQueueList >> tmpQueueObj.param.Compact;
LevelQueueList >> tmpQueueObj.param.Edge;
LevelQueueList >> tmpQueueObj.param.EC;
LevelQueueList >> tmpQueueObj.param.GHH;
LevelQueueList >> tmpQueueObj.param.MA;
mseg.LevelQueue.push_back(tmpQueueObj);
}
LevelQueueList.close();
//Fill Queue_to_Hierarchy
//Open image file for input
ERS_Image Img(arg1, 0);
//Store input band weights
ifstream WeightsFile(arg3.c_str());
if(!WeightsFile){
cout << "Error reading file" << WeightsFile << endl;
exit(1);
}
for(int i=1; i<=Img.Bands; i++){
WeightsFile >> Img.BandWeight[i];
}
WeightsFile.close();
Starting_Points_Estimation SPE(0);
Standard_Mode speed_mode;
//Run SPE module
SPE.HSI(Img, mseg);
//Run First pass
Level pass1;
merges_occured = speed_mode.firstpass(pass1, Img, mseg, 1.0, mseg.LevelQueue[0].param);
cout << "Merges occured at cycle " << 1 << " were " << merges_occured << endl;
//Run Second pass
Level pass2;
merges_occured = speed_mode.secondpass(pass2, Img, mseg, pass1);
cout << "Merges occured at cycle " << 2 << " were " << merges_occured << endl;
//pass1.clear();
//cout << "Level 1 deleted" << endl;
//Declare 2 temp nth pass levels
Level passn1;
Level passn2;
int cycle = 2;
int sw = -1;//switch to use with power...
int flag_npass;
//Run 3rd pass
if(merges_occured){//if second pass merged something, enter the nth pass loop...
merges_occured = speed_mode.nthpass(passn1, Img, mseg, pass2);
cycle++;
cout << "Merges occured at cycle " << cycle << " were " << merges_occured << endl;
flag_npass = 1;
}
//Run nth pass
while(merges_occured){
if(pow(sw, cycle)<0.0){
merges_occured = speed_mode.nthpass(passn2, Img, mseg, passn1);
cycle++;
cout << "Merges occured at cycle " << cycle << " were " << merges_occured << endl;
flag_npass = 2;
if(merges_occured) passn1.clear();
}else{
merges_occured = speed_mode.nthpass(passn1, Img, mseg, passn2);
cycle++;
cout << "Merges occured at cycle " << cycle << " were " << merges_occured << endl;
flag_npass = 1;
if(merges_occured) passn2.clear();
}
}
if(flag_npass == 1){
passn2.CalculateProperties(Img);
cout << "Statistics calculated" << endl;
passn2.SaveMeanAsERS(arg2, Img);//the last merged level will be the previous one since no merges occured
passn2.SaveRaster(rst, Img);
//passn2.SaveMiniXML(xml);
average_size = (passn2.Lines*passn2.Columns)/(passn2.Objects.size());
cout << "final level currently holds: " << passn2.Objects.size() << " objects" << endl;
cout << "Average object size: " << average_size << endl;
cout << "Segmentation file saved..." << endl;
passn2.CreateBoundaryMap();
passn2.SaveBoundaryMapERS(bound);
passn2.SaveXML(xml, Img);
passn2.SaveProperties(xml);
}
if(flag_npass == 2){
passn1.CalculateProperties(Img);
cout << "Statistics calculated" << endl;
passn1.SaveMeanAsERS(arg2, Img);//the last merged level will be the previous one since no merges occured
passn1.SaveRaster(rst, Img);
//passn1.SaveMiniXML(xml);
average_size = (passn1.Lines*passn1.Columns)/(passn1.Objects.size());
cout << "final level currently holds: " << passn1.Objects.size() << " objects" << endl;
cout << "Average object size: " << average_size << endl;
cout << "Segmentation file saved..." << endl;
passn1.CreateBoundaryMap();
passn1.SaveBoundaryMapERS(bound);
passn1.SaveXML(xml, Img);
passn1.SaveProperties(xml);
}
//close image file
Img.file.close();
//Debug mode:
cout << "Image file closed..." << endl;
//system("Pause");
return 0;
}//main end
//Generic run...
//For every level in LevelQueue,
//if no sublevel run 1stpass
//if no sublevel and if something was merged, run 2ndpass
//if something was merged, run nthpass
//while new merges occur run nthpass
//push level in LevelHierarchy
//Save levels to output file
//write output ers header file