1 | #include <iostream> |
---|
2 | #include <iomanip> |
---|
3 | #include <math.h> |
---|
4 | #include <vector> |
---|
5 | #include <Cubes/cubes.hh> |
---|
6 | #include <Detection/detection.hh> |
---|
7 | #include <Utils/utils.hh> |
---|
8 | |
---|
9 | void Cube::ObjectMerger() |
---|
10 | { |
---|
11 | /** |
---|
12 | * Cube::ObjectMerger(): |
---|
13 | * A Function that takes a Cube's list of Detections and |
---|
14 | * combines those that are close (according to the |
---|
15 | * thresholds specified in the parameter list par). |
---|
16 | * It also excludes those that do not make the minimum |
---|
17 | * number of channels requirement. |
---|
18 | * A front end to simpler functions mergeList and finaliseList, |
---|
19 | * with code to cover the option of growing objects. |
---|
20 | */ |
---|
21 | |
---|
22 | if(this->objectList.size() > 0){ |
---|
23 | |
---|
24 | // make a vector "currentList", which starts as a copy of the Cube's objectList, |
---|
25 | // but is the one worked on. |
---|
26 | vector <Detection> *currentList = new vector <Detection>(this->objectList.size()); |
---|
27 | *currentList = this->objectList; |
---|
28 | this->objectList.clear(); |
---|
29 | |
---|
30 | mergeList(*currentList, this->par); |
---|
31 | |
---|
32 | // Do growth stuff |
---|
33 | if(this->par.getFlagGrowth()) { |
---|
34 | std::cout << "Growing objects... "<< std::flush; |
---|
35 | this->setCubeStats(); |
---|
36 | vector <Detection> *newList = new vector <Detection>; |
---|
37 | std::cout<< "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"<<std::flush; |
---|
38 | std::cout << " Growing object # "<< std::flush; |
---|
39 | std::cout.setf(std::ios::left); |
---|
40 | for(int i=0;i<currentList->size();i++){ |
---|
41 | std::cout<< "\b\b\b\b\b\b"<<std::setw(6)<<i+1<<std::flush; |
---|
42 | Detection *obj = new Detection; |
---|
43 | *obj = (*currentList)[i]; |
---|
44 | growObject(*obj,*this); |
---|
45 | newList->push_back(*obj); |
---|
46 | delete obj; |
---|
47 | } |
---|
48 | delete currentList; |
---|
49 | currentList = newList; |
---|
50 | std::cout.unsetf(std::ios::left); |
---|
51 | std::cout<< "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"<<std::flush; |
---|
52 | |
---|
53 | // and do the merging again to pick up objects that have |
---|
54 | // grown into each other. |
---|
55 | mergeList(*currentList, this->par); |
---|
56 | } |
---|
57 | |
---|
58 | finaliseList(*currentList, this->par); |
---|
59 | |
---|
60 | this->objectList = *currentList; |
---|
61 | currentList->clear(); |
---|
62 | delete currentList; |
---|
63 | |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | void ObjectMerger(vector<Detection> &objList, Param &par) |
---|
68 | { |
---|
69 | /** |
---|
70 | * ObjectMerger(objList, par) |
---|
71 | * A simple front-end to the two following functions, |
---|
72 | * so that if you want to merge a single list, it will |
---|
73 | * do both the merging and the cleaning up afterwards. |
---|
74 | */ |
---|
75 | mergeList(objList, par); |
---|
76 | finaliseList(objList, par); |
---|
77 | } |
---|
78 | |
---|
79 | void mergeList(vector<Detection> &objList, Param &par) |
---|
80 | { |
---|
81 | /** |
---|
82 | * mergeList(objList, par) |
---|
83 | * A function that merges any objects in the list of |
---|
84 | * Detections that are within stated threshold distances. |
---|
85 | * Determination of whether objects are close is done by |
---|
86 | * the function areClose. |
---|
87 | */ |
---|
88 | |
---|
89 | if(objList.size() > 0){ |
---|
90 | |
---|
91 | bool isVerb = par.isVerbose(); |
---|
92 | vector <Detection>::iterator iter; |
---|
93 | |
---|
94 | int counter=0, compCounter; |
---|
95 | while( counter < (objList.size()-1) ){ |
---|
96 | if(isVerb){ |
---|
97 | std::cout.setf(std::ios::right); |
---|
98 | // if(nloop>0) std::cout << "Progress#2: " << std::setw(6) << counter << "/" ; |
---|
99 | // else |
---|
100 | std::cout << "Progress: " << std::setw(6) << counter << "/" ; |
---|
101 | std::cout.unsetf(std::ios::right); |
---|
102 | std::cout.setf(std::ios::left); |
---|
103 | std::cout << std::setw(6) << objList.size() |
---|
104 | << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" << std::flush; |
---|
105 | // if(nloop>0) std::cout << "\b\b" << std::flush; ; |
---|
106 | std::cout.unsetf(std::ios::left); |
---|
107 | } |
---|
108 | |
---|
109 | compCounter = counter + 1; |
---|
110 | |
---|
111 | do { |
---|
112 | |
---|
113 | Detection *obj1 = new Detection; |
---|
114 | *obj1 = objList[counter]; |
---|
115 | Detection *obj2 = new Detection; |
---|
116 | *obj2 = objList[compCounter]; |
---|
117 | |
---|
118 | if(areClose(*obj1, *obj2, par)){ |
---|
119 | obj1->addAnObject( *obj2 ); |
---|
120 | iter = objList.begin() + compCounter; |
---|
121 | objList.erase(iter); |
---|
122 | iter = objList.begin() + counter; |
---|
123 | objList.erase(iter); |
---|
124 | objList.push_back( *obj1 ); |
---|
125 | |
---|
126 | if(isVerb){ |
---|
127 | std::cout.setf(std::ios::right); |
---|
128 | std::cout << "Progress: " |
---|
129 | << std::setw(6) << counter << "/"; |
---|
130 | std::cout.unsetf(std::ios::right); |
---|
131 | std::cout.setf(std::ios::left); |
---|
132 | std::cout << std::setw(6) << objList.size() |
---|
133 | << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" |
---|
134 | << std::flush; |
---|
135 | std::cout.unsetf(std::ios::left); |
---|
136 | } |
---|
137 | |
---|
138 | compCounter = counter + 1; |
---|
139 | |
---|
140 | } |
---|
141 | else compCounter++; |
---|
142 | |
---|
143 | delete obj2; |
---|
144 | delete obj1; |
---|
145 | |
---|
146 | } while( (compCounter<objList.size()) ); |
---|
147 | |
---|
148 | counter++; |
---|
149 | |
---|
150 | } // end of while(counter<(objList.size()-1)) loop |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | void finaliseList(vector<Detection> &objList, Param &par) |
---|
156 | { |
---|
157 | /** |
---|
158 | * finaliseList(objList, par) |
---|
159 | * A function that looks at each object in the Detection vector |
---|
160 | * and determines whether is passes the requirements for the |
---|
161 | * minimum number of channels and spatial pixels, as provided by |
---|
162 | * the Param set par. |
---|
163 | * If it does not pass, it is removed from the list. |
---|
164 | * In the process, the object parameters are calculated and offsets |
---|
165 | * are added. |
---|
166 | */ |
---|
167 | |
---|
168 | int listCounter = 0; |
---|
169 | while(listCounter < objList.size()){ |
---|
170 | |
---|
171 | objList[listCounter].addOffsets(par); |
---|
172 | objList[listCounter].calcParams(); |
---|
173 | |
---|
174 | if( objList[listCounter].hasEnoughChannels(par.getMinChannels()) |
---|
175 | && (objList[listCounter].getSpatialSize() >= par.getMinPix()) ){ |
---|
176 | |
---|
177 | listCounter++; |
---|
178 | if(par.isVerbose()){ |
---|
179 | std::cout << "Final total:"<<std::setw(5)<<listCounter<<" " |
---|
180 | << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"<<std::flush; |
---|
181 | |
---|
182 | } |
---|
183 | } |
---|
184 | else objList.erase(objList.begin()+listCounter); |
---|
185 | |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | |
---|