vector<Rect> CharacterSegmenter::combineCloseBoxes( vector<Rect> charBoxes, float biggestCharWidth) { vector<Rect> newCharBoxes; for (int i = 0; i < charBoxes.size(); i++) { if (i == charBoxes.size() - 1) { newCharBoxes.push_back(charBoxes[i]); break; } float bigWidth = (charBoxes[i + 1].x + charBoxes[i + 1].width - charBoxes[i].x); float w1Diff = abs(charBoxes[i].width - biggestCharWidth); float w2Diff = abs(charBoxes[i + 1].width - biggestCharWidth); float bigDiff = abs(bigWidth - biggestCharWidth); bigDiff *= 1.3; // Make it a little harder to merge boxes. if (bigDiff < w1Diff && bigDiff < w2Diff) { Rect bigRect(charBoxes[i].x, charBoxes[i].y, bigWidth, charBoxes[i].height); newCharBoxes.push_back(bigRect); if (this->config->debugCharSegmenter) { for (int z = 0; z < charAnalysis->thresholds.size(); z++) { Point center(bigRect.x + bigRect.width / 2, bigRect.y + bigRect.height / 2); RotatedRect rrect(center, Size2f(bigRect.width, bigRect.height + (bigRect.height / 2)), 0); ellipse(imgDbgCleanStages[z], rrect, Scalar(0,255,0), 1); } cout << "Merging 2 boxes -- " << i << " and " << i + 1 << endl; } i++; } else { newCharBoxes.push_back(charBoxes[i]); } } return newCharBoxes; }
/* * orientation := (x1, y1, x2, y2)^T * * */ bool Keyframe::extract_part(cv::Mat& dst, cv::Point centre, cv::Vec4i orientation, int window_size) { int align_x = 0; int align_y = 0; cv::Mat frame = keyframe_; if (centre.x < window_size/2 || centre.x > keyframe_.cols - window_size/2 || centre.y < window_size/2 || centre.y > keyframe_.rows - window_size/2) { align_x = window_size; align_y = window_size; frame = cv::Mat(cv::Size(frame.cols + 2*align_x, frame.rows + 2*align_y), CV_32F, 0.05); cv::Mat roi = frame(cv::Rect(align_x, align_y, keyframe_.cols, keyframe_.rows)); keyframe_.copyTo(roi); } // Rotating the ROI so that the direction points downwards float dy = orientation[1] - orientation[3]; float dx = orientation[0] - orientation[2]; float angle = -acos(dy/sqrt(dy*dy + dx*dx)) * 180.f/M_PI; if (dx < 0) { angle = abs(angle); } centre.x += align_x; centre.y += align_y; cv::RotatedRect rrect(centre, cv::Size(window_size, window_size), angle); // try cv::Mat roi = frame; // get the rotation matrix cv::Mat R = cv::getRotationMatrix2D(rrect.center, angle, 1.0); // perform the affine transformation cv::Mat roi_rotated; warpAffine(roi, roi_rotated, R, roi.size(), cv::INTER_CUBIC); // crop the resulting image cv::getRectSubPix(roi_rotated, rrect.size, rrect.center, dst); return true; };
void Display::impl::draw() { SDL_PumpEvents(); pixfmt pixf(rbwindow); pixfmt pixf_pre(rbwindow); base_renderer rb(pixf); base_renderer rb_pre(pixf_pre); solid_renderer rs(rb); renderer_bin rbin(rb); agg::scanline_u8 sl; agg::rasterizer_scanline_aa<> pf; if (fondo) SDL_BlitSurface(fondo.get(), 0, window.get(), 0); else rb.clear(agg::rgba(0, 0, 0)); agg::glyph_rendering gren = agg::glyph_ren_agg_gray8; if(m_feng.load_font("timesi.ttf", 0, gren)) { m_feng.hinting(1); m_feng.flip_y(1); for (impl::it_tsprites it = textSprites.begin() ,end = textSprites.end() ;it!= end ;++it) { TextSprite::impl* tsimpl = *it; if (tsimpl->visible) { m_feng.height(tsimpl->size); m_feng.width(tsimpl->size); m_contour.width(-tsimpl->size * tsimpl->size * 0.05); double x = tsimpl->x; double y0 = tsimpl->y + tsimpl->size; double y = y0; for (std::string::iterator letra = tsimpl->caption.begin(), last = tsimpl->caption.end(); letra != last; ++letra) { const agg::glyph_cache* glyph = m_fman.glyph(*letra); if (glyph) { m_fman.init_embedded_adaptors(glyph, x, y); rs.color(agg::rgba8(0, 0, 0)); agg::render_scanlines(m_fman.gray8_adaptor(), m_fman.gray8_scanline(), rs); x += glyph->advance_x; y += glyph->advance_y; } } } } } for (impl::it_sprites it = sprites.begin() ,end = sprites.end() ;it!= end ;++it) { Sprite::impl* simpl = *it; if (simpl->visible) { agg::rendering_buffer buffer((unsigned char*)simpl->surface->pixels, simpl->surface->w, simpl->surface->h, simpl->surface->pitch); interpolator_type interpolator; span_alloc_type sa; span_gen_type sg(sa, buffer, agg::rgba_pre(0, 0, 0, 0), interpolator); renderer_type ri; agg::rounded_rect rec(0, 0, buffer.width(), buffer.height(), 0); agg::trans_affine src_mtx; src_mtx *= agg::trans_affine_translation(simpl->x, simpl->y); agg::trans_affine img_mtx = src_mtx; img_mtx.invert(); interpolator.transformer(img_mtx); ri.attach(rb_pre, sg); agg::rounded_rect rrect(0, 0, buffer.width(), buffer.height(), 0); agg::conv_transform<agg::rounded_rect> tr(rrect, src_mtx); pf.add_path(tr); agg::render_scanlines(pf, sl, ri); } } SDL_BlitSurface(window.get(), 0, screen, 0); SDL_Flip(screen); }