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