[299] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // Merger.cc: Merging a list of Detections, and rejecting on the basis |
---|
| 3 | // of number of channels or pixels. |
---|
| 4 | // ----------------------------------------------------------------------- |
---|
| 5 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 6 | // |
---|
| 7 | // This program is free software; you can redistribute it and/or modify it |
---|
| 8 | // under the terms of the GNU General Public License as published by the |
---|
| 9 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 10 | // option) any later version. |
---|
| 11 | // |
---|
| 12 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 13 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 14 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 15 | // for more details. |
---|
| 16 | // |
---|
| 17 | // You should have received a copy of the GNU General Public License |
---|
| 18 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 19 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 20 | // |
---|
| 21 | // Correspondence concerning Duchamp may be directed to: |
---|
| 22 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 23 | // Postal address: Dr. Matthew Whiting |
---|
| 24 | // Australia Telescope National Facility, CSIRO |
---|
| 25 | // PO Box 76 |
---|
| 26 | // Epping NSW 1710 |
---|
| 27 | // AUSTRALIA |
---|
| 28 | // ----------------------------------------------------------------------- |
---|
[3] | 29 | #include <iostream> |
---|
[258] | 30 | #include <fstream> |
---|
[3] | 31 | #include <iomanip> |
---|
| 32 | #include <math.h> |
---|
| 33 | #include <vector> |
---|
[943] | 34 | #include <duchamp/param.hh> |
---|
[393] | 35 | #include <duchamp/PixelMap/Object3D.hh> |
---|
| 36 | #include <duchamp/Cubes/cubes.hh> |
---|
[463] | 37 | #include <duchamp/Cubes/cubeUtils.hh> |
---|
[393] | 38 | #include <duchamp/Detection/detection.hh> |
---|
[744] | 39 | #include <duchamp/Detection/ObjectGrower.hh> |
---|
[393] | 40 | #include <duchamp/Utils/utils.hh> |
---|
| 41 | #include <duchamp/Utils/feedback.hh> |
---|
[3] | 42 | |
---|
[232] | 43 | using std::vector; |
---|
[258] | 44 | using namespace PixelInfo; |
---|
[232] | 45 | |
---|
[378] | 46 | namespace duchamp |
---|
[3] | 47 | { |
---|
| 48 | |
---|
[378] | 49 | void Cube::ObjectMerger() |
---|
| 50 | { |
---|
[528] | 51 | /// @details |
---|
| 52 | /// A Function that takes a Cube's list of Detections and |
---|
| 53 | /// combines those that are close (according to the |
---|
| 54 | /// thresholds specified in the parameter list par). |
---|
| 55 | /// It also excludes those that do not make the minimum |
---|
| 56 | /// number of channels requirement. |
---|
| 57 | /// A front end to simpler functions mergeList and finaliseList, |
---|
| 58 | /// with code to cover the option of growing objects. |
---|
[3] | 59 | |
---|
[378] | 60 | int startSize = this->objectList->size(); |
---|
[258] | 61 | |
---|
[378] | 62 | if(startSize > 0){ |
---|
[3] | 63 | |
---|
[378] | 64 | // make a vector "currentList", which starts as a copy of the Cube's |
---|
| 65 | // objectList, but is the one worked on. |
---|
| 66 | vector <Detection> currentList(startSize); |
---|
| 67 | for(int i=0;i<startSize;i++) currentList[i] = this->objectList->at(i); |
---|
| 68 | this->objectList->clear(); |
---|
[3] | 69 | |
---|
[672] | 70 | if(this->par.getFlagRejectBeforeMerge()) |
---|
| 71 | finaliseList(currentList, this->par); |
---|
| 72 | |
---|
[423] | 73 | mergeList(currentList, this->par); |
---|
[378] | 74 | |
---|
| 75 | // Do growth stuff |
---|
| 76 | if(this->par.getFlagGrowth()) { |
---|
[744] | 77 | ObjectGrower grower; |
---|
| 78 | grower.define(this); |
---|
[623] | 79 | for(size_t i=0;i<currentList.size();i++){ |
---|
[629] | 80 | if(this->par.isVerbose()){ |
---|
| 81 | std::cout.setf(std::ios::right); |
---|
| 82 | std::cout << "Growing: " << std::setw(6) << i+1 << "/"; |
---|
| 83 | std::cout.unsetf(std::ios::right); |
---|
| 84 | std::cout.setf(std::ios::left); |
---|
| 85 | std::cout << std::setw(6) << currentList.size() << std::flush; |
---|
| 86 | printBackSpace(22); |
---|
| 87 | std::cout << std::flush; |
---|
| 88 | } |
---|
[744] | 89 | grower.grow(¤tList[i]); |
---|
[378] | 90 | } |
---|
[984] | 91 | grower.updateDetectMap(this->detectMap); |
---|
[378] | 92 | std::cout.unsetf(std::ios::left); |
---|
| 93 | |
---|
| 94 | // and do the merging again to pick up objects that have |
---|
| 95 | // grown into each other. |
---|
[423] | 96 | mergeList(currentList, this->par); |
---|
[137] | 97 | } |
---|
[3] | 98 | |
---|
[672] | 99 | if(!this->par.getFlagRejectBeforeMerge()) |
---|
| 100 | finaliseList(currentList, this->par); |
---|
[3] | 101 | |
---|
[378] | 102 | // *this->objectList = currentList; |
---|
| 103 | this->objectList->resize(currentList.size()); |
---|
[623] | 104 | for(size_t i=0;i<currentList.size();i++) |
---|
[378] | 105 | this->objectList->at(i) = currentList[i]; |
---|
[291] | 106 | |
---|
[378] | 107 | currentList.clear(); |
---|
[3] | 108 | |
---|
[378] | 109 | } |
---|
[137] | 110 | } |
---|
[3] | 111 | |
---|
[378] | 112 | void ObjectMerger(vector<Detection> &objList, Param &par) |
---|
| 113 | { |
---|
[528] | 114 | /// @details |
---|
| 115 | /// A simple front-end to the mergeList() and finaliseList() functions, |
---|
| 116 | /// so that if you want to merge a single list, it will |
---|
| 117 | /// do both the merging and the cleaning up afterwards. |
---|
| 118 | |
---|
[378] | 119 | mergeList(objList, par); |
---|
| 120 | finaliseList(objList, par); |
---|
| 121 | } |
---|
[138] | 122 | |
---|
[378] | 123 | void mergeList(vector<Detection> &objList, Param &par) |
---|
| 124 | { |
---|
[528] | 125 | /// @details |
---|
| 126 | /// A function that merges any objects in the list of |
---|
| 127 | /// Detections that are within stated threshold distances. |
---|
| 128 | /// Determination of whether objects are close is done by |
---|
| 129 | /// the function areClose. |
---|
[86] | 130 | |
---|
[378] | 131 | if(objList.size() > 0){ |
---|
[3] | 132 | |
---|
[378] | 133 | bool isVerb = par.isVerbose(); |
---|
| 134 | vector <Detection>::iterator iter; |
---|
[770] | 135 | |
---|
| 136 | std::vector<bool> isValid(objList.size(),true); |
---|
| 137 | int numRemoved=0; |
---|
[3] | 138 | |
---|
[770] | 139 | size_t counter=0, compCounter,goodCounter=0; |
---|
| 140 | |
---|
[378] | 141 | while( counter < (objList.size()-1) ){ |
---|
| 142 | if(isVerb){ |
---|
| 143 | std::cout.setf(std::ios::right); |
---|
[770] | 144 | std::cout << "Merging: " << std::setw(6) << goodCounter+1 << "/" ; |
---|
[378] | 145 | std::cout.unsetf(std::ios::right); |
---|
| 146 | std::cout.setf(std::ios::left); |
---|
[770] | 147 | std::cout << std::setw(6) << objList.size()-numRemoved; |
---|
[378] | 148 | printBackSpace(22); |
---|
| 149 | std::cout << std::flush; |
---|
| 150 | std::cout.unsetf(std::ios::left); |
---|
| 151 | } |
---|
[3] | 152 | |
---|
[770] | 153 | if(isValid[counter]){ |
---|
[3] | 154 | |
---|
[770] | 155 | compCounter = counter + 1; |
---|
[3] | 156 | |
---|
[770] | 157 | do { |
---|
[3] | 158 | |
---|
[770] | 159 | if(isValid[compCounter]){ |
---|
[137] | 160 | |
---|
[770] | 161 | bool close = objList[counter].canMerge(objList[compCounter], par); |
---|
[137] | 162 | |
---|
[770] | 163 | if(close){ |
---|
| 164 | objList[counter].addDetection(objList[compCounter]); |
---|
| 165 | isValid[compCounter]=false; |
---|
| 166 | numRemoved++; |
---|
[137] | 167 | |
---|
[770] | 168 | if(isVerb){ |
---|
| 169 | std::cout.setf(std::ios::right); |
---|
| 170 | std::cout << "Merging: " |
---|
| 171 | << std::setw(6) << goodCounter+1 << "/"; |
---|
| 172 | std::cout.unsetf(std::ios::right); |
---|
| 173 | std::cout.setf(std::ios::left); |
---|
| 174 | std::cout << std::setw(6) << objList.size()-numRemoved; |
---|
| 175 | printBackSpace(22); |
---|
| 176 | std::cout << std::flush; |
---|
| 177 | std::cout.unsetf(std::ios::left); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | compCounter = counter + 1; |
---|
| 181 | |
---|
| 182 | } |
---|
| 183 | else compCounter++; |
---|
| 184 | } |
---|
| 185 | else compCounter++; |
---|
[3] | 186 | |
---|
[770] | 187 | } while( (compCounter<objList.size()) ); |
---|
[3] | 188 | |
---|
[770] | 189 | } |
---|
[378] | 190 | counter++; |
---|
[770] | 191 | if(isValid[counter]) goodCounter++; |
---|
[3] | 192 | |
---|
[378] | 193 | } // end of while(counter<(objList.size()-1)) loop |
---|
[770] | 194 | |
---|
| 195 | std::vector<Detection> newlist(objList.size()-numRemoved); |
---|
| 196 | size_t ct=0; |
---|
| 197 | for(size_t i=0;i<objList.size();i++){ |
---|
| 198 | if(isValid[i]) newlist[ct++]=objList[i]; |
---|
| 199 | } |
---|
| 200 | objList.clear(); |
---|
| 201 | objList=newlist; |
---|
| 202 | |
---|
[378] | 203 | } |
---|
[137] | 204 | } |
---|
[3] | 205 | |
---|
| 206 | |
---|
[378] | 207 | void finaliseList(vector<Detection> &objList, Param &par) |
---|
| 208 | { |
---|
[528] | 209 | /// @details |
---|
| 210 | /// A function that looks at each object in the Detection vector |
---|
| 211 | /// and determines whether is passes the requirements for the |
---|
| 212 | /// minimum number of channels and spatial pixels, as provided by |
---|
| 213 | /// the Param set par. |
---|
| 214 | /// If it does not pass, it is removed from the list. |
---|
[728] | 215 | /// In the process, the offsets are set. |
---|
[3] | 216 | |
---|
[637] | 217 | if(par.isVerbose()){ |
---|
| 218 | std::cout << "Rejecting:" << std::setw(6) << objList.size(); |
---|
| 219 | printSpace(6); |
---|
| 220 | printBackSpace(22); |
---|
| 221 | std::cout << std::flush; |
---|
| 222 | } |
---|
| 223 | |
---|
[770] | 224 | std::vector<Detection> newlist; |
---|
| 225 | |
---|
[577] | 226 | std::vector<Detection>::iterator obj = objList.begin(); |
---|
[770] | 227 | int numRej=0; |
---|
| 228 | for(;obj<objList.end();obj++){ |
---|
[577] | 229 | obj->setOffsets(par); |
---|
[570] | 230 | |
---|
[577] | 231 | if( (obj->hasEnoughChannels(par.getMinChannels())) |
---|
[720] | 232 | && (obj->getSpatialSize() >= par.getMinPix()) |
---|
[721] | 233 | && (obj->getSize() >= par.getMinVoxels() ) ){ |
---|
[139] | 234 | |
---|
[770] | 235 | newlist.push_back(*obj); |
---|
[265] | 236 | |
---|
[378] | 237 | } |
---|
| 238 | else{ |
---|
[770] | 239 | numRej++; |
---|
[378] | 240 | if(par.isVerbose()){ |
---|
[770] | 241 | std::cout << "Rejecting:" << std::setw(6) << objList.size()-numRej; |
---|
[378] | 242 | printSpace(6); |
---|
| 243 | printBackSpace(22); |
---|
| 244 | std::cout << std::flush; |
---|
| 245 | } |
---|
| 246 | |
---|
[137] | 247 | } |
---|
[265] | 248 | } |
---|
[770] | 249 | |
---|
| 250 | objList.clear(); |
---|
| 251 | objList = newlist; |
---|
| 252 | |
---|
[3] | 253 | } |
---|
| 254 | |
---|
[137] | 255 | |
---|
[378] | 256 | } |
---|