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 #ifndef CMAP_H
00039 #define CMAP_H
00040
00041 #include "Corba/Objects/CMapSK.hh"
00042 #include "Corba/ImageOrbUtil.H"
00043 #include "Corba/CorbaUtil.H"
00044 #include "Image/PyramidTypes.H"
00045
00046 static omni_mutex CMapWorkerThreadMutex;
00047
00048
00049 class CMapWorkerThread : public omni_thread {
00050
00051 public:
00052 CMapWorkerThread(CMap_ptr cmap, Image<float> &dst, Image<byte> &img, PyramidType ptype,
00053 double ori, double weight, int &frame, bool isUndetached) : th_dst(dst), th_bias(NULL),
00054 th_ptype(ptype), th_ori(ori), th_weight(weight), th_frame(frame) {
00055
00056 CMapObj = cmap;
00057 th_img1 = img;
00058
00059 LDEBUG("Starting thread for with %i %g %g\n", ptype, ori, weight);
00060
00061 runBias = false;
00062 if (isUndetached)
00063 start_undetached();
00064 else
00065 start();
00066 }
00067
00068 CMapWorkerThread(CMap_ptr cmap, Image<float> &dst, Image<byte> &img, PyramidType ptype,
00069 double ori, double weight, int &frame, std::vector<float> &bias, bool isUndetached) :
00070 th_dst(dst), th_ptype(ptype), th_ori(ori), th_weight(weight), th_frame(frame) {
00071
00072 CMapObj = cmap;
00073 th_img1 = img;
00074
00075 th_bias = new CMap::BiasSeq;
00076 th_bias->length(bias.size());
00077 for(uint i=0; i<bias.size(); i++)
00078 (*th_bias)[i] = bias[i];
00079
00080
00081 LDEBUG("Starting thread for with %i %g %g\n", ptype, ori, weight);
00082
00083 runBias = true;
00084 if (isUndetached)
00085 start_undetached();
00086 else
00087 start();
00088 }
00089
00090 ~CMapWorkerThread(){}
00091
00092 private:
00093 Image<byte> th_img1;
00094 Image<byte> th_img2;
00095 Image<float> &th_dst;
00096 CMap::BiasSeq *th_bias;
00097
00098 PyramidType th_ptype;
00099 double th_ori;
00100 double th_weight;
00101 int &th_frame;
00102 CMap_ptr CMapObj;
00103 bool runBias;
00104
00105 void* run_undetached(void *ptr){
00106 LDEBUG("Start undetached");
00107 processCMap();
00108
00109 return NULL;
00110 }
00111
00112 void run(void *ptr){
00113 LDEBUG("Start detached");
00114 processCMap();
00115 }
00116
00117 void processCMap (){
00118
00119
00120 ImageOrb *imgOrb;
00121
00122 if (runBias){
00123 LDEBUG("Biasing cmap");
00124
00125
00126 imgOrb = CMapObj->computeBiasCMAP(*image2Orb(th_img1),
00127 th_ptype, th_ori, th_weight, *th_bias);
00128 } else {
00129 LDEBUG("Unbias CMap");
00130 imgOrb = CMapObj->computeCMAP(*image2Orb(th_img1),
00131 th_ptype, th_ori, th_weight);
00132 }
00133
00134
00135 Image<byte> tmp;
00136 orb2Image(*imgOrb, tmp);
00137 delete imgOrb;
00138
00139
00140
00141 CMapWorkerThreadMutex.lock();
00142 th_dst = tmp;
00143 CMapWorkerThreadMutex.unlock();
00144 th_frame++;
00145 }
00146
00147
00148 };
00149
00150
00151 class CMapThreads {
00152 public:
00153 CMapThreads(CORBA::ORB_ptr orb)
00154 {
00155 nCmapObj = 0;
00156 initObject(orb);
00157 }
00158
00159
00160
00161 CMapWorkerThread* newth(Image<float> &dst, Image<byte> &img, PyramidType ptype,
00162 double ori, double weight, int &frame, bool isUndetached = false)
00163 {
00164 CMap_ptr CMap = getCmapRef();
00165 return new CMapWorkerThread(CMap, dst, img, ptype, ori, weight,frame, isUndetached);
00166 }
00167
00168 CMapWorkerThread* newth(Image<float> &dst, Image<byte> &img, PyramidType ptype,
00169 double ori, double weight, int &frame, std::vector<float> &bias,
00170 bool isUndetached = false)
00171 {
00172 CMap_ptr CMap = getCmapRef();
00173 return new CMapWorkerThread(CMap, dst, img, ptype, ori, weight,frame, bias, isUndetached);
00174 }
00175
00176 std::vector<float> &getBias(Image<float> &dst, Image<byte> &img, PyramidType ptype,
00177 double ori, double weight, int &frame, Point2D<int> &loc)
00178 {
00179 Point2DOrb locOrb;
00180 locOrb.i = loc.i; locOrb.j = loc.j;
00181
00182 CMap_ptr CMap = getCmapRef();
00183 CMap::BiasSeq *bias = CMap->getBiasCMAP(*image2Orb(img),
00184 ptype, ori, weight, locOrb);
00185 std::vector<float> *retBias = new std::vector<float>(bias->length());
00186
00187 for (uint i=0; i<bias->length(); i++){
00188 (*retBias)[i] = (*bias)[i];
00189 }
00190
00191 return *retBias;
00192
00193 }
00194
00195 private:
00196
00197
00198 int nCmapObj;
00199 CORBA::Object_ptr CMap_ref[100];
00200
00201
00202
00203 CMap_var getCmapRef(){
00204 static int current_obj = 0;
00205
00206
00207 current_obj = (current_obj+1)%nCmapObj;
00208
00209 LDEBUG("Using cmap object number %i\n", current_obj);
00210
00211 return CMap::_narrow(CMap_ref[current_obj]);
00212 }
00213
00214 void initObject(CORBA::ORB_ptr orb) {
00215 LDEBUG("Initalizing CMap object");
00216 if (!getMultiObjectRef(orb, "test.saliency", CMap_ref, nCmapObj)){
00217 LFATAL("Can not find any object to bind with");
00218 }
00219
00220 }
00221 };
00222
00223 #endif
00224