LineFitting.C
Go to the documentation of this file.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 LineFitting_C_DEFINED
00039 #define LineFitting_C_DEFINED
00040
00041 #include "Image/OpenCVUtil.H"
00042 #include "plugins/SceneUnderstanding/LineFitting.H"
00043 #include "plugins/SceneUnderstanding/V2.H"
00044
00045 #include "Image/DrawOps.H"
00046 #include "Raster/Raster.H"
00047 #include "Simulation/SimEventQueue.H"
00048 #include "GUI/DebugWin.H"
00049 #include "plugins/SceneUnderstanding/LFLineFitter/LFLineFitter.h"
00050
00051
00052 const ModelOptionCateg MOC_LineFitting = {
00053 MOC_SORTPRI_3, "LineFitting-Related Options" };
00054
00055
00056 const ModelOptionDef OPT_LineFittingShowDebug =
00057 { MODOPT_ARG(bool), "LineFittingShowDebug", &MOC_LineFitting, OPTEXP_CORE,
00058 "Show debug img",
00059 "linefitting-debug", '\0', "<true|false>", "false" };
00060
00061
00062 SIMMODULEINSTFUNC(LineFitting);
00063
00064
00065
00066 LineFitting::LineFitting(OptionManager& mgr, const std::string& descrName,
00067 const std::string& tagName) :
00068 SimModule(mgr, descrName, tagName),
00069 SIMCALLBACK_INIT(SimEventV1Output),
00070 SIMCALLBACK_INIT(SimEventInputFrame),
00071 SIMCALLBACK_INIT(SimEventSaveOutput),
00072 itsShowDebug(&OPT_LineFittingShowDebug, this)
00073 {
00074 }
00075
00076
00077 LineFitting::~LineFitting()
00078 {
00079 }
00080
00081
00082 void LineFitting::onSimEventV1Output(SimEventQueue& q,
00083 rutz::shared_ptr<SimEventV1Output>& e)
00084 {
00085
00086
00087
00088 }
00089
00090
00091
00092 void LineFitting::onSimEventInputFrame(SimEventQueue& q,
00093 rutz::shared_ptr<SimEventInputFrame>& e)
00094 {
00095
00096 GenericFrame frame = e->frame();
00097
00098 itsInImage = frame.asRgb();
00099
00100 evolve(q);
00101 }
00102
00103 void LineFitting::evolve(SimEventQueue& q)
00104 {
00105
00106 Image<float> edges = luminance(itsInImage);
00107 itsLines = FitLine(edges);
00108
00109 q.post(rutz::make_shared(new SimEventV2Output(this, itsLines, itsInImage.getDims())));
00110
00111 }
00112
00113
00114 std::vector<V2::LineSegment> LineFitting::FitLine(const Image<float>& edges)
00115 {
00116 std::map<int,Point2D<int> > edgesMap;
00117
00118 LFLineFitter lf;
00119 lf.Configure("para_line_fitter.txt");
00120 lf.Init();
00121 lf.FitLine(img2ipl(edges));
00122
00123 int nLines = lf.rNLineSegments();
00124 LFLineSegment* lineSegmentMap = lf.rOutputEdgeMap();
00125
00126 LFLineSegment* lines = new LFLineSegment[nLines];
00127
00128 std::vector<V2::LineSegment> lineSegments;
00129 for (int i=0 ; i<nLines ; i++)
00130 {
00131 lines[i] = lineSegmentMap[i];
00132 lineSegments.push_back(V2::LineSegment(
00133 Point2D<float>(lines[i].sx_, lines[i].sy_),
00134 Point2D<float>(lines[i].ex_, lines[i].ey_)));
00135 }
00136
00137 return lineSegments;
00138
00139
00140 }
00141
00142
00143
00144 void LineFitting::onSimEventSaveOutput(SimEventQueue& q,
00145 rutz::shared_ptr<SimEventSaveOutput>& e)
00146 {
00147 if (itsShowDebug.getVal())
00148 {
00149
00150
00151 nub::ref<FrameOstream> ofs =
00152 dynamic_cast<const SimModuleSaveInfo&>(e->sinfo()).ofs;
00153 Layout<PixRGB<byte> > disp = getDebugImage();
00154 if (disp.initialized())
00155 ofs->writeRgbLayout(disp, "LineFitting", FrameInfo("LineFitting", SRC_POS));
00156 }
00157 }
00158
00159
00160 Layout<PixRGB<byte> > LineFitting::getDebugImage()
00161 {
00162
00163 Layout<PixRGB<byte> > disp;
00164
00165 Image<PixRGB<byte> > tmp = itsInImage;
00166 for(uint i=0; i<itsLines.size(); i++)
00167 drawLine(tmp, (Point2D<int>)itsLines[i].p1, (Point2D<int>)itsLines[i].p2, PixRGB<byte>(0,255,0));
00168
00169 disp = hcat(itsInImage, tmp);
00170
00171 usleep(10000);
00172 return disp;
00173
00174
00175 }
00176
00177 #endif