float rectOverlap(rect_t a, rect_t b) { float sa = rectArea(a), sb = rectArea(b); if (sa <= 0.0f || sb <= 0.0f) return 0.0f; float lx = max(a.x, b.x); float ly = max(a.y, b.y); float rx = min(a.x + a.width, b.x + b.width); float ry = min(a.y + a.height, b.y + b.height); return max(rx - lx, 0.0f) * max(ry - ly, 0.0f); }
void MT::fast_train(Warp warp) { //build the fine template on uniform 5x5 grid rect_t rect = window(warp.t); float fast_stride = sqrt(rectArea(rect) / fast_n); feature.set_cell(fast_stride); int W = int(rect.width * 0.5f / fast_stride); int H = int(rect.height * 0.5f / fast_stride); int ox = int(rect.width * 0.5f + 0.5f); int oy = int(rect.height * 0.5f + 0.5f); int stride = int(fast_stride + 0.5f); fast_samples.clear(); for (int y = 0; y <= 2 * H; ++y) for (int x = 0; x <= 2 * W; ++x) fast_samples.push_back(Vector2i(ox + (x - W) * stride, oy + (y - H) * stride)); //compute the features on the grid fast_model = MatrixXf::Zero(8, fast_samples.size()); int x = int(rect.x + 0.5f); int y = int(rect.y + 0.5f); for (int i = 0; i < fast_samples.size(); ++i) { int tx = x + fast_samples[i].x(); int ty = y + fast_samples[i].y(); memcpy(fast_model.col(i).data(), feature.cell_hist(tx, ty), 8 * sizeof(float)); } }
Vector3f MT::locate(rect_t rect) { float scale = sqrt(window_width * window_height / rectArea(rect)); float x = rect.x + rect.width * 0.5f - warp.c.x(); float y = rect.y + rect.height * 0.5f - warp.c.y(); return scale * Vector3f(x, y, warp.f); }
float MT::evaluate(Warp warp) { rect_t rect = window(warp.t); float fine_cell = sqrt(rectArea(rect) / cell_n); feature.set_cell(fine_cell); feature.set_step(1); float E = 0.0f; for (int i = 0; i < fine_samples.size(); ++i) { Vector2f p = warp.transform2(fine_samples[i]); Vector32f F; feature.descriptor4(p.x(), p.y(), F.data()); E = E + sigmoid((F - fine_model.col(i)).squaredNorm()); } return E / fine_samples.size(); }
Warp MT::fine_test(Warp warp) { ++number_MLK; rect_t rect = window(warp.t); float fine_cell = sqrt(rectArea(rect) / cell_n); feature.set_cell(fine_cell); //use different interpolation step to accelerate the convergence for (auto fine_step : fine_steps) { if (fine_step > 2.0f * fine_cell) continue; feature.set_step(fine_step); if (log != NULL) (*log) << "\tcell = " << fine_cell << " step = " << fine_step << " : "; warp = Lucas_Kanade(warp); } return warp; }
void WindowManager::Render() { // setup orthogonal projection glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); Size s = m_spRootWindow->GetSize(); gluOrtho2D(0, s.Width(), s.Height(), 0); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); // set states glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_LIGHTING); // set up texturing glEnable(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glColor3ub(255, 255, 255); // render root window; all child windows are rendered recursively Rect rectArea(Point(0,0), m_spRootWindow->GetSize()); m_spRootWindow->Render(rectArea); // restore previous projection glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); glDisable(GL_TEXTURE_2D); }
Vector3f MT::fast_test(Warp warp) { //locate the searching area rect_t rect = window(warp.t); float fast_stride = sqrt(rectArea(rect) / fast_n); feature.set_cell(fast_stride); rect_t region = window(warp.t / (1.0f + padding)); float minminx = -rect.width * 0.5f; float minminy = -rect.height * 0.5f; float maxmaxx = image_width + rect.width * 0.5f; float maxmaxy = image_height + rect.height * 0.5f; int minx = int(max(region.x, minminx) + 0.5f); int miny = int(max(region.y, minminy) + 0.5f); int maxx = int(min(region.x + region.width, maxmaxx) - rect.width + 0.5f); int maxy = int(min(region.y + region.height, maxmaxy) - rect.height + 0.5f); //use slide window to find the best match float best_score = 0.0f; Vector3f best_translate = warp.t; MatrixXf model(8, fast_samples.size()); for (int y = miny; y <= maxy; y += fast_step) for (int x = minx; x <= maxx; x += fast_step) { for (int i = 0; i < fast_samples.size(); ++i) { int tx = x + fast_samples[i].x(); int ty = y + fast_samples[i].y(); memcpy(model.col(i).data(), feature.cell_hist(tx, ty), 8 * sizeof(float)); } float S = model.squaredNorm(); float score = S < 1.0f ? 0.0f : model.cwiseProduct(fast_model).sum() / sqrt(S); if (score > best_score) { rect_t best_rect = rect; best_rect.x = float(x); best_rect.y = float(y); best_translate = locate(best_rect); best_score = score; } } return best_translate; }
void MT::fine_train(Warp warp) { rect_t rect = window(warp.t); float fine_cell = sqrt(rectArea(rect) / cell_n); feature.set_cell(fine_cell); feature.set_step(1); //compute the features on the grid MatrixXf model(32, fine_samples.size()); for (int i = 0; i < fine_samples.size(); ++i) { Vector2f p = warp.transform2(fine_samples[i]); feature.descriptor4(p.x(), p.y(), model.col(i).data()); } //update using a moving averaging manner if (N == 0) { N = 1; fine_model = model; } else { ++N; fine_model = (float(N - 1) / N) * fine_model + (1.0f / N) * model; } }