00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZE_LabelFusion_cpp)
00056 #define ALIZE_LabelFusion_cpp
00057
00058 #include <iostream>
00059 #include <fstream>
00060 #include <cstdio>
00061 #include <cassert>
00062 #include <cmath>
00063 #include <liatools.h>
00064 #include "LabelFusion.h"
00065
00066
00067 using namespace alize;
00068 using namespace std;
00069
00070
00071
00072
00073 void interSeg(SegCluster& clusterOne,SegCluster& clusterTwo,SegCluster& clusterOutput)
00074 {
00075 unsigned long overlapTime=0;
00076 clusterOne.rewind();
00077 clusterTwo.rewind();
00078 SegServer & segServerOutput=clusterOutput.getServer();
00079 Seg *segOne;
00080 Seg *segTwo=clusterTwo.getSeg();
00081 while((segOne=clusterOne.getSeg())!=NULL){
00082 while ((segTwo) && (endSeg(segTwo)<segOne->begin()))
00083 segTwo=clusterTwo.getSeg();
00084 if ((segTwo) && (segTwo->begin()<=endSeg(segOne))){
00085 if (debug){
00086 cout << "overlap between :"<<endl;
00087 cout << segOne->sourceName()<<" "<<segOne->begin()<<" "<<segOne->length()<<endl;
00088 cout << segTwo->sourceName()<<" "<<segTwo->begin()<<" "<<segTwo->length()<<endl;
00089 }
00090 if (segTwo->begin()<=segOne->begin()){
00091 if (endSeg(segTwo)>=endSeg(segOne)){
00092 if (debug) cout << "seg suppressed"<<endl;
00093 overlapTime+=segOne->length();
00094 }
00095 else{
00096 Seg &seg=segServerOutput.createSeg(endSeg(segTwo)+1,endSeg(segOne)-endSeg(segTwo)+1,0,segOne->string(),segOne->sourceName());
00097 if (debug) cout << "Modify1 the seg "<<seg.begin()<<" "<<seg.length()<<endl;
00098 clusterOutput.add(seg);
00099 overlapTime+=segOne->length()-seg.length();
00100 }
00101 }
00102 else{
00103 Seg &seg=segServerOutput.createSeg(segOne->begin(),segTwo->begin()-segOne->begin(),0,segOne->string(),segOne->sourceName());
00104 if (debug) cout << "Modify2 the seg "<<seg.begin()<<" "<<seg.length()<<endl;
00105 clusterOutput.add(seg);
00106 overlapTime+=segOne->length();
00107 overlapTime-=seg.length();
00108 if (endSeg(segTwo)<endSeg(segOne)){
00109 Seg &seg=segServerOutput.createSeg(endSeg(segTwo)+1,endSeg(segOne)-endSeg(segTwo),0,segOne->string(),segOne->sourceName());
00110 if (debug) cout << "Adding the seg "<<seg.begin()<<" "<<seg.length()<<endl;
00111 clusterOutput.add(seg);
00112 overlapTime-=seg.length();
00113 }
00114 }
00115 }
00116 else{
00117 Seg & seg=segServerOutput.createSeg(segOne->begin(),segOne->length(),0,segOne->string(),segOne->sourceName());
00118 if (debug) cout << "saving the seg "<<seg.begin()<<" "<<seg.length()<<endl;
00119 clusterOutput.add(seg);
00120 }
00121 }
00122 if (verbose) cout <<"InterSeg, total overlap time (suppressed frames) ["<<overlapTime<<"]"<<endl;
00123 }
00124
00125 bool selFrame(double frm,double length, double threshold)
00126 {
00127 return (frm/length>=threshold);
00128 }
00129
00130
00131
00132 void morphologicalFilter(SegCluster& clusterIn,SegCluster& clusterOut,
00133 unsigned long winLength,double selectThreshold)
00134 {
00135 if (verbose) cout <<"beginning the morphological filter"<<endl;
00136 SegServer & segServerOut=clusterOut.getServer();
00137
00138 clusterIn.rewind();
00139 Seg *segIn=clusterIn.getSeg();
00140 if (segIn!=NULL){
00141 unsigned long suppressedTime=0;
00142 unsigned long addedTime=0;
00143 String label=segIn->string();
00144 String sourceName=segIn->sourceName();
00145 unsigned long winIdx=segIn->begin();
00146 unsigned long winFrm=segIn->length();
00147 unsigned long end=endSeg(segIn);
00148 while(segIn!=NULL){
00149 while (((segIn=clusterIn.getSeg())!=NULL)&&(endSeg(segIn)<winIdx+winLength)){
00150 winFrm+=segIn->length();
00151 end=endSeg(segIn);
00152 }
00153 if ((winFrm)&& selFrame(winFrm,winLength,selectThreshold)){
00154 unsigned long length=end-winIdx+1;
00155 Seg &seg=segServerOut.createSeg(winIdx,length,0,label,sourceName);
00156 if (debug) cout << "Adding the seg "<<seg.begin()<<" "<<seg.length()<<endl;
00157 clusterOut.add(seg);
00158 addedTime+=(length-winFrm);
00159 }
00160 else suppressedTime+=winFrm;
00161 if (segIn!=NULL){
00162 winIdx=segIn->begin();
00163 winFrm=segIn->length();
00164 end=endSeg(segIn);
00165 }
00166 }
00167 if (verbose){
00168 long diffTime=addedTime-suppressedTime;
00169 cout <<"morpho filter, suppressed frame["<<suppressedTime<<"] added frame["<<addedTime<<
00170 "] total["<<diffTime<<"]"<<endl;
00171 }
00172 }
00173 }
00174 void morphologicalFilter(SegCluster& clusterIn,SegCluster& clusterOut,Config &config)
00175 {
00176 unsigned long winLength=config.getParam("winLength").toLong();
00177 double selectThreshold=config.getParam("selectThreshold").toDouble();
00178 morphologicalFilter(clusterIn,clusterOut,winLength,selectThreshold);
00179 }
00180
00181 int labelFusion(Config& config)
00182 {
00183 String extOutput=".lbl";
00184 if (config.existsParam("saveLabelFileExtension")) extOutput=config.getParam("saveLabelFileExtension");
00185 String pathOutput;
00186 String pathInput;
00187 if (config.existsParam("labelOutputPath")) pathOutput=config.getParam("labelOutputPath");
00188
00189 if (config.existsParam("labelInputPath")){
00190 pathInput=config.getParam("labelInputPath");
00191 config.setParam("labelFilesPath", config.getParam("labelInputPath"));
00192 }
00193
00194 String fileOut=config.getParam("labelOneFilename");
00195 String fileOne=config.getParam("labelOneFilename");
00196 String fileTwo=config.getParam("labelTwoFilename");
00197 if (config.existsParam("outputFilename"))
00198 fileOut=config.getParam("outputFilename");
00199 String labelSelectedFrames=config.getParam("labelSelectedFrames");
00200
00201 try{
00202 SegServer segServerOne;
00203 SegServer segServerTwo;
00204 LabelServer labelServerOne;
00205 LabelServer labelServerTwo;
00206
00207 loadClusterFile(fileOne,segServerOne,labelServerOne,config);
00208 if (debug) cout <<"label 1 loaded"<<endl;
00209 loadClusterFile(fileTwo,segServerTwo,labelServerTwo,config);
00210 if (debug) cout <<"label 2 loaded"<<endl;
00211 long codeSelectedFrameOne=labelServerOne.getLabelIndexByString(labelSelectedFrames);
00212 if (codeSelectedFrameOne==-1){
00213 cout << " WARNING - NO DATA with the label["<<labelSelectedFrames<<"] in file ["<<fileOne<<"]"<<endl;
00214 exit(0);
00215 }
00216 long codeSelectedFrameTwo=labelServerTwo.getLabelIndexByString(labelSelectedFrames);
00217 if (codeSelectedFrameTwo==-1){
00218 cout << " WARNING - NO DATA with the label["<<labelSelectedFrames<<"] in file ["<<fileTwo<<"]"<<endl;
00219 }
00220 SegCluster& clusterOne=segServerOne.getCluster(codeSelectedFrameOne);
00221 SegCluster& clusterTwo=segServerTwo.getCluster(codeSelectedFrameTwo);
00222
00223 SegServer segServerOutput;
00224 SegCluster& clusterInt=segServerOutput.createCluster(0,labelSelectedFrames,clusterOne.sourceName());
00225 interSeg(clusterOne,clusterTwo,clusterInt);
00226 SegCluster& clusterOutput=segServerOutput.createCluster(1,labelSelectedFrames,clusterOne.sourceName());
00227 morphologicalFilter(clusterInt,clusterOutput,config);
00228 if (verbose){
00229 unsigned long init=totalFrame(clusterOne);
00230 unsigned long final=totalFrame(clusterOutput);
00231 long suppressed=init-final;
00232 cout <<"File["<<fileOne<<"] Initial number of Frame["<<init<<"] Final number of frame["<<final<<"] Suppressed ["<<suppressed<<"]"<<endl;
00233 cout << "Output the new label file in ["<<pathOutput+fileOut+extOutput <<"]"<<endl;
00234 }
00235 outputLabelFile(clusterOutput,pathOutput+fileOut+extOutput,config);
00236 }
00237
00238
00239 catch (Exception& e)
00240 {
00241 cout << e.toString().c_str() << endl;
00242 }
00243 return 0;
00244 }
00245
00246
00247
00248 int labelMorphing(Config& config)
00249 {
00250 String extOutput=".lbl";
00251 if (config.existsParam("saveLabelFileExtension")) extOutput=config.getParam("saveLabelFileExtension");
00252 String pathOutput;
00253 String pathInput;
00254 if (config.existsParam("labelOutputPath")){ pathOutput=config.getParam("labelOutputPath");
00255 }
00256
00257 if (config.existsParam("labelInputPath")){
00258 pathInput=config.getParam("labelInputPath");
00259 config.setParam("labelFilesPath", config.getParam("labelInputPath"));
00260 }
00261
00262 String fileIn= config.getParam("labelFilename");
00263 String fileOut=fileIn;
00264 if (config.existsParam("outputFilename"))
00265 fileOut=config.getParam("labelFilename");
00266 String labelSelectedFrames=config.getParam("labelSelectedFrames");
00267
00268 try{
00269 SegServer segServerIn;
00270 LabelServer labelServerIn;
00271 loadClusterFile(fileIn,segServerIn,labelServerIn,config);
00272 if (debug) cout <<"label loaded"<<endl;
00273 long codeSelectedFrameIn=labelServerIn.getLabelIndexByString(labelSelectedFrames);
00274 if (codeSelectedFrameIn==-1){
00275 cout << " WARNING - NO DATA with the label["<<labelSelectedFrames<<"] in file ["<<fileIn<<"]"<<endl;
00276 exit(0);
00277 }
00278 SegCluster& clusterIn=segServerIn.getCluster(codeSelectedFrameIn);
00279
00280 SegServer segServerOutput;
00281 SegCluster& clusterOutput=segServerOutput.createCluster(1,labelSelectedFrames,clusterIn.sourceName());
00282 morphologicalFilter(clusterIn,clusterOutput,config);
00283 if (verbose){
00284 unsigned long init=totalFrame(clusterIn);
00285 unsigned long final=totalFrame(clusterOutput);
00286 long suppressed=init-final;
00287 cout <<"File["<<fileIn<<"] Initial number of Frame["<<init<<"] Final number of frame["<<final<<"] Suppressed ["<<suppressed<<"]"<<endl;
00288 cout << "Output the new label file in ["<<fileOut+extOutput <<"]"<<endl;
00289 }
00290
00291 outputLabelFile(clusterOutput,pathOutput+fileOut+extOutput,config);
00292 }
00293
00294
00295 catch (Exception& e)
00296 {
00297 cout << e.toString().c_str() << endl;
00298 }
00299 return 0;
00300 }
00301
00302
00303 #endif //!defined(ALIZE_LabelFusion_cpp)