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 #include "Component/ModelComponent.H"
00038 #include "Component/ModelManager.H"
00039 #include "Image/ColorMap.H"
00040 #include "Image/ColorOps.H"
00041 #include "Image/CutPaste.H"
00042 #include "Image/Dims.H"
00043 #include "Image/Image.H"
00044 #include "Image/ShapeOps.H"
00045 #include "Learn/QuadTree.H"
00046 #include "Channels/RawVisualCortex.H"
00047 #include "Raster/GenericFrame.H"
00048 #include "Raster/Raster.H"
00049
00050 #include "GUI/DebugWin.H"
00051
00052 std::vector<byte> perm3Number(int key)
00053 {
00054 key = key % 6;
00055 std::vector<byte> ret;
00056 int rem = key % 3;
00057 ret.push_back(rem);
00058 if(key > 3) {
00059 ret.push_back((rem + 1) % 3);
00060 ret.push_back((rem + 2) % 3);
00061 } else {
00062 ret.push_back((rem + 2) % 3);
00063 ret.push_back((rem + 1) % 3);
00064 }
00065 return ret;
00066 }
00067
00068 QuadNode::NodeState hashNodeState(int key) {
00069 key = key % (30 * 6);
00070 return QuadNode::NodeState(key % 30, perm3Number(key / 30));
00071 }
00072
00073 int submain(const int argc, char** argv)
00074 {
00075 MYLOGVERB = LOG_INFO;
00076
00077 ModelManager manager("Test Quad Tree");
00078
00079 nub::ref<RawVisualCortex> vc(new RawVisualCortex(manager));
00080 manager.addSubComponent(vc);
00081
00082
00083
00084 uint nArgs = 2;
00085 if (manager.parseCommandLine((const int)argc, (const char**)argv, "<image filename>", nArgs, nArgs) == false)
00086 return 1;
00087 vc->setModelParamVal("RawVisualCortexChans",std::string("CIO"));
00088 vc->setModelParamVal("LevelSpec",LevelSpec(0,1,2,3,0),MC_RECURSE);
00089
00090
00091
00092 Dims dims(320,240);
00093
00094
00095 const GenericFrame input = Raster::ReadFrame(manager.getExtraArg(0).c_str());
00096 Image<PixRGB<byte> > im = rescale(input.asRgb(), dims);
00097 Image<uint> cl_im(dims, ZEROS);
00098
00099
00100
00101
00102
00103
00104 QuadTree myQuadTree(1,im);
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 manager.start();
00125
00126
00127 vc->input(InputFrame::fromRgb(&im));
00128
00129 for(uint i = 0; i < vc->numSubmaps(); i++) {
00130 Image<float> im_fl = vc->getSubmap(i);
00131
00132 inplaceNormalize(im_fl,0.0f,255.0f);
00133 LINFO("%s", toStr(vc->getSubmapName(i)).c_str());
00134 Image<PixRGB<byte> > im_out = im_fl;
00135 }
00136
00137 rutz::shared_ptr<QuadNode> root = myQuadTree.getRootNode();
00138 LINFO("caching classifier result");
00139 myQuadTree.cacheClassifierResult();
00140 LINFO("generating proposals");
00141 std::vector<QuadNode::NodeState> pr = myQuadTree.generateProposalsAt(root, manager.getExtraArgAs<double>(1));
00142 LINFO("done; %zu proposals made", pr.size());
00143
00144 uint i,i_max = 0;
00145 for(i = 0; i < pr.size(); i++) {
00146 LINFO("proposal %d: %s -> %f", i, toStr(pr[i]).c_str(),pr[i].E);
00147 if(pr[i].E < pr[i_max].E) i_max = i;
00148 }
00149
00150 if(pr.size() > 0) {
00151 LINFO("proposal with least energy %f: %s", pr[i_max].E, toStr(pr[i_max]).c_str());
00152 root->setState(pr[i_max]);
00153 }
00154 else
00155 {
00156 LINFO("no proposals match!"); return 0;
00157 }
00158
00159
00160 Image<PixRGB<byte> > im_L0, im_L1, im_tot;
00161 im_L0 = root->getColorizedSegImage();
00162 im_L1 = root->getColorizedChildSegImage();
00163
00164 im_tot = concatX(im, concatX(im_L0, im_L1));
00165 drawLine(im_tot, Point2D<int>(dims.w(), 0), Point2D<int>(dims.w(),dims.h()-1),PixRGB<byte>(255,255,255));
00166 drawLine(im_tot, Point2D<int>(2*dims.w(), 0), Point2D<int>(2*dims.w(),dims.h()-1),PixRGB<byte>(255,255,255));
00167
00168
00169 Raster::WriteRGB(im_tot,"result.png");
00170
00171 SHOWIMG(im_tot);
00172
00173
00174
00175
00176 manager.stop();
00177 return 0;
00178 }
00179
00180 extern "C" int main(const int argc, char** argv)
00181 {
00182 try
00183 {
00184 return submain(argc, argv);
00185 }
00186 catch (...)
00187 {
00188 REPORT_CURRENT_EXCEPTION;
00189 }
00190
00191 return 1;
00192 }
00193
00194
00195
00196
00197
00198