void TrainDataGUI::randomNegativeSample() { unsigned int seed = (unsigned int)time(NULL); cv::RNG rng(seed); while((int)negSample.size() < kNeg){ Rect newRect; while(true){ int x = rng.uniform(0, static_cast<int>(image.cols * downsample- sizeNeg)); int y = rng.uniform(0, static_cast<int>(image.rows * downsample - sizeNeg)); newRect = cv::Rect(cv::Point(x,y), cv::Size(sizeNeg, sizeNeg)); bool nointersect = true; for(const auto& rec: negSample){ if(rectIntersect(newRect, rec)){ nointersect = false; break; } } if(!nointersect) continue; for(const auto& rec: posSample){ if(rectIntersect(newRect, rec)){ nointersect = false; break; } } if(nointersect) break; } negSample.push_back(newRect); } }
/*---------------------------------------------------------------------------------------------- Render an IPicture using its Render method @param ppic The picture to render @param x Horizontal position of image in hdc @param y Vertical position of image in hdc @param cx Horizontal dimension of destination rectangle @param cy Vertical dimension of destination rectangle @param xSrc Horizontal offset in source picture @param ySrc Vertical offset in source picture @param cxSrc Amount to copy horizontally in source picture @param cySrc Amount to copy vertically in source picture @param prcWBounds Pointer to position of destination for a metafile hdc (Not currently used on Linux) ----------------------------------------------------------------------------------------------*/ HRESULT VwGraphicsCairo::RenderPicture(IPicture * ppic, int x, int y, int cx, int cy, OLE_XPOS_HIMETRIC xSrc, OLE_YPOS_HIMETRIC ySrc, OLE_XSIZE_HIMETRIC cxSrc, OLE_YSIZE_HIMETRIC cySrc, LPCRECT prcWBounds) { #if DEBUG // TODO: represent Picture some how. #endif BEGIN_COM_METHOD; /* TODO: * Use all the co-ordinates (yup, all 12) */ CheckDc(); //Cairo::Context ctxt(m_cr); // Exit if picture is outside of clipping rectangle RECT rectClip; MyGetClipRect(&rectClip); RECT rectDraw; rectDraw.left = x; rectDraw.right = x + cx; rectDraw.top = y; rectDraw.bottom = y + cy; if(!rectIntersect(&rectClip, &rectDraw)) { return S_OK; } ppic->Render(m_hdc, x, y, cx, cy, xSrc, ySrc, cxSrc, cySrc, prcWBounds); END_COM_METHOD(g_fact, IID_IVwGraphics); }
boxf* partition (cell* cells, int ncells, int* nrects, boxf bb) { int nsegs = 4*(ncells+1); segment_t* segs = N_GNEW(nsegs+1, segment_t); int* permute = N_NEW(nsegs+1, int); int hd_size, vd_size; int i, j, cnt = 0; boxf* rs; int ntraps = TRSIZE(nsegs); trap_t* trs = N_GNEW(ntraps, trap_t); boxf* hor_decomp = N_NEW(ntraps, boxf); boxf* vert_decomp = N_NEW(ntraps, boxf); int nt; /* fprintf (stderr, "cells = %d segs = %d traps = %d\n", ncells, nsegs, ntraps); */ genSegments (cells, ncells, bb, segs, 0); #if 0 fprintf (stderr, "%d\n\n", ncells+1); for (i = 1; i<= nsegs; i++) { if (i%4 == 1) fprintf(stderr, "4\n"); fprintf (stderr, "%f %f\n", segs[i].v0.x, segs[i].v0.y); if (i%4 == 0) fprintf(stderr, "\n"); } #endif srand48(173); generateRandomOrdering (nsegs, permute); nt = construct_trapezoids(nsegs, segs, permute, ntraps, trs); /* fprintf (stderr, "hor traps = %d\n", nt); */ hd_size = monotonate_trapezoids (nsegs, segs, trs, 0, hor_decomp); genSegments (cells, ncells, bb, segs, 1); generateRandomOrdering (nsegs, permute); nt = construct_trapezoids(nsegs, segs, permute, ntraps, trs); /* fprintf (stderr, "ver traps = %d\n", nt); */ vd_size = monotonate_trapezoids (nsegs, segs, trs, 1, vert_decomp); rs = N_NEW (hd_size*vd_size, boxf); for (i=0; i<vd_size; i++) for (j=0; j<hd_size; j++) if (rectIntersect(&rs[cnt], &vert_decomp[i], &hor_decomp[j])) cnt++; rs = RALLOC (cnt, rs, boxf); free (segs); free (permute); free (trs); free (hor_decomp); free (vert_decomp); *nrects = cnt; return rs; }
DetectionSet StackedModel::detect_occ2dpm(const ImRGBZ& im, DetectionFilter filter) const { DetectionFilter part_fitler = filter; part_fitler.nmax = numeric_limits<int>::max(); part_fitler.thresh = -inf; DetectionSet occ_dets = occ_model->detect(im,part_fitler); DetectionSet dpm_dets = dpm_model->detect(im,part_fitler); DetectionSet merged_dets; for(int iter = 0; iter < occ_dets.size(); ++iter) { DetectorResult occ_det = occ_dets[iter]; auto occFeatFn = occ_det->feature; DetectorResult dpm_det = nearest_neighbour(dpm_dets,occ_det->BB); decltype(occFeatFn) dpmFeatFn; if(rectIntersect(occ_det->BB,dpm_det->BB) > BaselineModel_ExternalKITTI::BB_OL_THRESH) { occ_det->resp = dpm_det->resp + learner->getB(); //occ_det->resp = ;//dpm_platt->prob(dpm_det->resp) * occ_platt->prob(occ_det->resp); dpmFeatFn = dpm_det->feature; } else { occ_det->resp = -inf; //dot(dpm_model->getW(),BaselineModel_ExternalKITTI::minFeat()) + learner->getB(); dpmFeatFn = []() { return vector<double> {BaselineModel_ExternalKITTI::minFeat()}; }; } occ_det->feature = [this,occFeatFn,dpmFeatFn]() { map<string,SparseVector> feats{ {"dpm_model",dpmFeatFn()}, {"occ_model",occFeatFn()}}; return feat_interpreation->pack(feats); }; merged_dets.push_back(occ_det); } log_once(printfpp("merged %d of %d dets",(int)merged_dets.size(),(int)occ_dets.size())); //filter.apply(merged_dets); return merged_dets; }
/*---------------------------------------------------------------------------------------------- Draw a rectangle, filled with the current background color ENHANCE: should we outline it in the foreground color? ----------------------------------------------------------------------------------------------*/ HRESULT VwGraphicsCairo::DrawRectangle(int xLeft, int yTop, int xRight, int yBottom) { #if DEBUG if (m_loggingFile != NULL) { fprintf(m_loggingFile, "DrawRectangle %p %d %d %d %d\n", this, xLeft, yTop, xRight, yBottom); fflush(m_loggingFile); } #endif BEGIN_COM_METHOD; // Trivially exit if the color is set to transparent if(m_backgroundColor.m_transparent) { return S_OK; } // See if rectangle is outside clipping box RECT rectClip; MyGetClipRect(&rectClip); RECT rectDraw; rectDraw.left = xLeft; rectDraw.right = xRight; rectDraw.top = yTop; rectDraw.bottom = yBottom; if(!rectIntersect(&rectClip, &rectDraw)) { return S_OK; } CheckDc(); // Only draw rectangles thats in the clipping region, by setting a cairo clipping region. m_ctxt->reset_clip(); m_ctxt->rectangle(rectClip.left, rectClip.top, rectClip.right - rectClip.left, rectClip.bottom - rectClip.top); m_ctxt->clip(); SetCairoColor(m_ctxt, &m_backgroundColor); m_ctxt->rectangle(xLeft, yTop, xRight - xLeft, yBottom - yTop); m_ctxt->fill(); // Undo the cairo clipping region. m_ctxt->reset_clip(); END_COM_METHOD(g_fact, IID_IVwGraphics); }
void eval_pose_estimation(Model&model, string test_dir, Scores&scores) { log_file << "+eval_pose" << test_dir << endl; vector<string> test_stems = allStems(test_dir,".gz",POSE); active_worker_thread.V(); #pragma omp parallel for for(int iter = 0; iter < test_stems.size(); iter++) { active_worker_thread.P(); // load test example shared_ptr<MetaData> metadata = metadata_build(test_dir + test_stems[iter],true); shared_ptr<ImRGBZ> im = metadata->load_im(); Rect bb_gt = metadata->get_positives()["HandBB"]; assert(metadata->get_positives().size() == 1); // detect with the model. DetectionFilter filter(-inf,numeric_limits<int>::infinity()); filter.supress_feature = g_supress_feature; DetectionSet detections = model.detect(*im,filter); // find the best detection within the specified range. Detection best_pose_estimate; best_pose_estimate.resp = -inf; for(auto && current_detecetion : detections) { bool ol = rectIntersect(current_detecetion->BB,bb_gt) > .5; if(ol && current_detecetion->resp > best_pose_estimate.resp) best_pose_estimate = *current_detecetion; } // score poses log_file << "gt_pose: " << metadata->get_pose_name() << " est_pose: " << best_pose_estimate.pose << endl; if(best_pose_estimate.pose == metadata->get_pose_name()) scores.pose_correct++; else scores.pose_incorrect++; active_worker_thread.V(); } active_worker_thread.P(); log_file << "-eval_pose" << test_dir << endl; }
/// /// SECTION: Pedestrian Model /// DetectionSet StackedModel::detect_dpm2occ(const ImRGBZ& im, DetectionFilter filter) const { DetectionFilter part_fitler = filter; part_fitler.nmax = numeric_limits<int>::max(); part_fitler.thresh = -inf; DetectionSet occ_dets = occ_model->detect(im,part_fitler); DetectionSet dpm_dets = dpm_model->detect(im,part_fitler); DetectionSet merged_dets; for(int iter = 0; iter < dpm_dets.size(); ++iter) { DetectorResult dpm_det = dpm_dets[iter]; DetectorResult new_det; //DetectorResult occ_det = nearest_neighbour(occ_dets,dpm_det->BB); for(DetectorResult occ_det : occ_dets) { double sf = std::sqrt(occ_det->BB.area()/dpm_det->BB.area()); if( occ_det->real >= .5 && rectIntersect(occ_det->BB,dpm_det->BB) > BaselineModel_ExternalKITTI::BB_OL_THRESH && clamp<double>(.5,sf,2) == sf) { // update the resp double new_resp = dpm_det->resp; //dpm_platt->prob();// * occ_platt->prob(occ_det->resp); if(!new_det or new_det->resp < new_resp) { new_det.reset(new Detection(*dpm_det)); new_det->resp = new_resp; } } if(new_det) merged_dets.push_back(new_det); } } log_once(printfpp("merged %d of %d dets",(int)merged_dets.size(),(int)dpm_dets.size())); //filter.apply(merged_dets); log_once(printfpp("filt 2 %d of %d dets",(int)merged_dets.size(),(int)dpm_dets.size())); return merged_dets; }
/*---------------------------------------------------------------------------------------------- Invert the specified rectangle. ----------------------------------------------------------------------------------------------*/ HRESULT VwGraphicsCairo::InvertRect(int xLeft, int yTop, int xRight, int yBottom) { #if DEBUG if (m_loggingFile != NULL) { fprintf(m_loggingFile, "InvertRect %p %d %d %d %d\n", this, xLeft, yTop, xRight, yBottom); fflush(m_loggingFile); } #endif BEGIN_COM_METHOD; /* * TODO: * Make this work * Cairo OPERATOR_XOR doesn't seem to work * * Dispite what 'some' documentation may say - reading up on the operators suggests * that it doesn't do what you may expect (ie work like a bitwise XOR) * * in Addition to this given the way we are currently paint the background, * (as a normal rectangle which we draw first) the background would also * interfere with XOR operation. * The internet says that there is no easy way for inverting text using cairo. * The suggested why is using paths (ie remembering when we have drawn the text) * however given the views framework this is likley to be infesible/hugly complicated. * * Therefore given the need to have a carret that stands out more I have special cased it * with a HACK that displays the carrat black when its 2 pixcels wide or smaller and * alpha blends it when its not. */ CheckDc(); // See if rectangle is outside clipping box RECT rectClip; MyGetClipRect(&rectClip); RECT rectDraw; rectDraw.left = xLeft; rectDraw.right = xRight; rectDraw.top = yTop; rectDraw.bottom = yBottom; if(!rectIntersect(&rectClip, &rectDraw)) { return S_OK; } // Only draw rectangles thats in the clipping region, by setting a cairo clipping region. m_ctxt->reset_clip(); m_ctxt->rectangle(rectClip.left, rectClip.top, rectClip.right - rectClip.left, rectClip.bottom - rectClip.top); m_ctxt->clip(); if (xRight - xLeft <= 2) m_ctxt->set_source_rgba(0.0, 0.0, 0.0, 1.0); else m_ctxt->set_source_rgba(0.5, 0.5, 1.0, 0.4); m_ctxt->rectangle(xLeft, yTop, xRight - xLeft, yBottom - yTop); m_ctxt->fill(); // Undo the cairo clipping region. m_ctxt->reset_clip(); END_COM_METHOD(g_fact, IID_IVwGraphics); }
/*---------------------------------------------------------------------------------------------- Draw a polygon. ----------------------------------------------------------------------------------------------*/ HRESULT VwGraphicsCairo::DrawPolygon(int cVertices, POINT prgvpnt[]) { #if DEBUG if (m_loggingFile != NULL) { fprintf(m_loggingFile, "DrawPolygon %p %d", this, cVertices); for (int i = 0; i < cVertices; i++) { fprintf(m_loggingFile, "%d %d", prgvpnt[i].x, prgvpnt[i].y); } fprintf(m_loggingFile, "\n"); fflush(m_loggingFile); } #endif BEGIN_COM_METHOD; // Trivially exit if the color is set to transparent if(m_backgroundColor.m_transparent) return S_OK; RECT rectBounds; rectBounds.left = prgvpnt[0].x; rectBounds.top = prgvpnt[0].y; rectBounds.right = rectBounds.left; rectBounds.bottom = rectBounds.top; // determine a bounding rectangle for (int i = 0; i < cVertices; i++) { int x = prgvpnt[i].x; int y = prgvpnt[i].y; if (x < rectBounds.left) rectBounds.left = x; if (x > rectBounds.right) rectBounds.right = x; if (y < rectBounds.top) rectBounds.top = y; if (y > rectBounds.bottom) rectBounds.bottom = y; } // check whether the bounding rectangle is visible RECT rectClip; MyGetClipRect(&rectClip); if(!rectIntersect(&rectClip, &rectBounds)) { return S_OK; } // Solid, in background color CheckDc(); //Cairo::Context ctxt(m_cr); SetCairoColor(m_ctxt, &m_backgroundColor); m_ctxt->move_to(prgvpnt[cVertices - 1].x, prgvpnt[cVertices - 1].y); for ( int i = 0; i < cVertices; i++) { m_ctxt->line_to(prgvpnt[i].x, prgvpnt[i].y); } m_ctxt->fill(); END_COM_METHOD(g_fact, IID_IVwGraphics); }