void sample_random_partrect(boost::variate_generator<boost::mt19937, boost::uniform_real<> > &gen_01,
                              double min_scale, double max_scale,
                              double min_rot, double max_rot, 
                              double min_pos_x, double max_pos_x,
                              double min_pos_y, double max_pos_y,
                              int rect_width, int rect_height, 
                              PartBBox &partrect, double &scale, double &rot)
  {
    scale = min_scale + (max_scale - min_scale)*gen_01();
    rot = min_rot + (max_rot - min_rot)*gen_01();

    partrect.part_pos(0) = min_pos_x + (max_pos_x - min_pos_x)*gen_01();
    partrect.part_pos(1) = min_pos_y + (max_pos_y - min_pos_y)*gen_01();

    //cout << alpha << ", " << scale << ", " << partrect.part_pos(0) <<  ", " << partrect.part_pos(1) << endl;

    partrect.part_x_axis(0) = cos(rot);
    partrect.part_x_axis(1) = sin(rot);

    partrect.part_y_axis(0) = -partrect.part_x_axis(1);
    partrect.part_y_axis(1) = partrect.part_x_axis(0);

    rect_width = (int)(rect_width*scale);
    rect_height = (int)(rect_height*scale);

    partrect.min_proj_x = -floor(rect_width/2.0);
    partrect.max_proj_x = partrect.min_proj_x + rect_width - 1;
  
    partrect.min_proj_y = -floor(rect_height/2.0);
    partrect.max_proj_y = partrect.min_proj_y + rect_height - 1;
  }
Esempio n. 2
0
  void compute_boosting_score_factor(const PartApp &part_app, int pidx, kma::ImageContent *kmaimg, 
                                     const vector<PartBBox> &part_samples, const vector<double> &vect_scales,
                                     Factor1d &factor)
  {
    assert(part_samples.size() == vect_scales.size());

    factor.varidx = pidx;

    /** add border */
    int added_border = 0;
    kma::ImageContent *kmaimg_border = add_image_border2(kmaimg, 50, added_border);

    cout << "added_border: " << added_border << endl;

    AdaBoostClassifier abc;
    part_app.loadClassifier(abc, pidx);

    int num_samples = part_samples.size();
    
    factor.fval.resize(boost::extents[num_samples]);

    for (int sampidx = 0; sampidx < num_samples; ++sampidx) {

      PartBBox bbox = part_samples[sampidx];
      /** add offset to position of bounding box */
      bbox.part_pos(0) += added_border;
      bbox.part_pos(1) += added_border;

      /** comput features */
      vector<float> all_features;
      PartBBox adjusted_rect;

//       bool bres = part_detect::compute_part_bbox_features_scale(part_app.m_abc_param, part_app.m_window_param, 
// 								bbox, kmaimg_border, pidx, 1,
// 								all_features, adjusted_rect);

//      assert(vect_scales[sampidx] == 1.0);

      bool bres = part_detect::compute_part_bbox_features_scale(part_app.m_abc_param, part_app.m_window_param, 
                                                                bbox, kmaimg_border, pidx, vect_scales[sampidx], 
                                                                all_features, adjusted_rect);

      factor.fval[sampidx] = part_app.m_abc_param.min_score();

      /** evaluate classifier */      
      if (bres) {
        float score = abc.evaluateFeaturePoint(all_features, true);

        if (score > part_app.m_abc_param.min_score())
          factor.fval[sampidx] = score;          
      }
      else {
        cout << "warning: failed to compute features" << endl;
      }
      
    }// samples

    delete kmaimg_border;
  }
Esempio n. 3
0
/** 
    coloridx: 0 - yellow, 1 - red, 2 - green, 3 - blue, if < 0 - only position without bounding box is drawn
 */
void draw_bbox(QPainter &painter, const PartBBox &part_bbox, int coloridx, int pen_width)
{    

  if (coloridx >= 0) {
    painter.setPen(Qt::yellow);
  
    int marker_radius = 3;
    int part_axis_length = 10;

    painter.drawEllipse(QRect((int)(part_bbox.part_pos(0) - marker_radius), (int)(part_bbox.part_pos(1) - marker_radius), 
			      2*marker_radius, 2*marker_radius));

    boost_math::double_vector v(2);
    v = part_bbox.part_pos + part_axis_length * part_bbox.part_x_axis;
    painter.drawLine((int)part_bbox.part_pos(0), (int)part_bbox.part_pos(1), (int)v(0), (int)v(1));

    painter.setPen(Qt::red);
    v = part_bbox.part_pos + part_axis_length * part_bbox.part_y_axis;
    painter.drawLine((int)part_bbox.part_pos(0), (int)part_bbox.part_pos(1), (int)v(0), (int)v(1));
    painter.setPen(Qt::yellow);

    QPen pen;

    if (coloridx == 0) 
      pen.setColor(Qt::yellow);
    else if (coloridx == 1)
      pen.setColor(Qt::red);
    else if (coloridx == 2)
      pen.setColor(Qt::green);
    else if (coloridx == 3)
      pen.setColor(Qt::blue);
    else
      pen.setColor(Qt::black);

    pen.setJoinStyle(Qt::RoundJoin);
    pen.setWidth(pen_width);

    painter.setPen(pen);

    QPolygonF polygon;
    get_part_polygon(part_bbox, polygon);
    painter.drawPolygon(polygon);
  }
  else {

    painter.setPen(Qt::yellow);

    if (coloridx == -1) 
      painter.setPen(Qt::yellow);
    else if (coloridx == -2)
      painter.setPen(Qt::red);
    else if (coloridx == -3)
      painter.setPen(Qt::green);
    else
      painter.setPen(Qt::blue);

    int x = part_bbox.part_pos(0);
    int y = part_bbox.part_pos(1);
    
    painter.drawLine(x-1, y, x+1, y);
    painter.drawLine(x, y-1, x, y+1);
  }

}
Esempio n. 4
0
  double eval_joint_factor_debug(const Joint &joint, const PartBBox& bbox_parent, const PartBBox &bbox_child, 
				 const double parent_scale, const double child_scale, 
				 double_vector &joint_pos_offset, double &joint_rot_offset, const double rot_step_size)
  {

    double rot_sigma = joint.rot_sigma;
    assert(rot_sigma >= 0);

    double child_rot = atan2(bbox_child.part_x_axis(1), bbox_child.part_x_axis(0));
    double parent_rot = atan2(bbox_parent.part_x_axis(1), bbox_parent.part_x_axis(0));
    cout << "child_rot = " << child_rot << endl;
    cout << "parent_rot = " << parent_rot << endl;
    cout << "rot_step_size = " << rot_step_size << endl;
    
    double_matrix Tgc = prod(hc::get_rotation_matrix(child_rot), hc::get_scaling_matrix(child_scale));
    double_vector offset_c_10 = hc::get_vector(joint.offset_c(0), joint.offset_c(1));
    double_vector offset_g_10 = prod(Tgc, offset_c_10);
    double_matrix Tjoint_c = hc::get_translation_matrix(offset_g_10(0), offset_g_10(1));
    double_matrix Tjoint2_c = prod(Tjoint_c,boost_math::identity_double_matrix(3));
    double x1_c, y1_c;
    hc::map_point(Tjoint2_c, bbox_child.part_pos(0), bbox_child.part_pos(1), x1_c, y1_c);
    
    double_matrix Tgp = prod(hc::get_rotation_matrix(parent_rot), hc::get_scaling_matrix(parent_scale));
    double_vector offset_p_01 = hc::get_vector(joint.offset_p(0), joint.offset_p(1));
    double_vector offset_g_01 = prod(Tgp, offset_p_01);
    double_matrix Tjoint_p = hc::get_translation_matrix(offset_g_01(0), offset_g_01(1));
    double_matrix Tjoint2_p = prod(Tjoint_p,boost_math::identity_double_matrix(3));
    double x1_p, y1_p;
    hc::map_point(Tjoint2_p, bbox_parent.part_pos(0), bbox_parent.part_pos(1), x1_p, y1_p);
    
//     assert(child_scale == 1.0);
//     assert(parent_scale == 1.0);

    double_vector joint_pos_child = bbox_child.part_pos + 
      child_scale * bbox_child.part_x_axis * joint.offset_c(0) + 
      child_scale * bbox_child.part_y_axis * joint.offset_c(1);

    double_vector joint_pos_parent = bbox_parent.part_pos + 
      parent_scale * bbox_parent.part_x_axis * joint.offset_p(0) + 
      parent_scale * bbox_parent.part_y_axis * joint.offset_p(1);
    
    cout << "round part positions" << endl;
    joint_pos_child(0) = round(joint_pos_child(0));
    joint_pos_child(1) = round(joint_pos_child(1));
    joint_pos_parent(0) = round(joint_pos_parent(0));
    joint_pos_parent(1) = round(joint_pos_parent(1));    
    
    joint_pos_offset = joint_pos_child - joint_pos_parent;
    
    cout << round(y1_c) << " " << round(x1_c) << endl;
    cout << joint_pos_child(1) << " " << joint_pos_child(0) << endl;
    cout << endl;
    cout << round(y1_p) << " " << round(x1_p) << endl;
    cout << joint_pos_parent(1) << " " << joint_pos_parent(0) << endl;
    
    double rot_mean = joint.rot_mean;
    if (rot_step_size > 0 && rot_step_size <= M_PI){
      cout << "round joint mean" << endl;
      rot_mean = boost_math::round(joint.rot_mean / rot_step_size) * rot_step_size;
    }
    joint_rot_offset = (child_rot - parent_rot) - rot_mean;
    /*
    while (joint_rot_offset < -M_PI)
      joint_rot_offset += M_PI;

    while (joint_rot_offset > M_PI)
      joint_rot_offset -= M_PI;
    
    assert(joint_rot_offset >= -M_PI && joint_rot_offset <= M_PI);
    */
    double detC1 = joint.detC;
    assert(detC1 > 0);

    double avg_scale = 0.5*(parent_scale + child_scale);
    //double avg_scale = std::max(parent_scale,child_scale);
    double_matrix inv_scaleC1 = joint.invC / square(avg_scale);
    
    //double d_pos = inner_prod(joint_pos_offset, ublas::prod<double_vector>(invC1, joint_pos_offset));
    double d_pos = inner_prod(joint_pos_offset, ublas::prod<double_vector>(inv_scaleC1, joint_pos_offset));
    double d_rot = square(joint_rot_offset) / square(rot_sigma);

    double p_joint;
      

    p_joint = exp(-0.5*(d_pos + d_rot)) / (2*M_PI*sqrt(2*M_PI) * sqrt(detC1) * rot_sigma);

    //assert(p_joint >= 0);
    return p_joint;
  }
Esempio n. 5
0
  double eval_joint_factor(const Joint &joint, const PartBBox& bbox_parent, const PartBBox &bbox_child, 
			   const double parent_scale, const double child_scale, 
			   double_vector &joint_pos_offset, double &joint_rot_offset, const double rot_step_size)
  {

    double rot_sigma = joint.rot_sigma;
    assert(rot_sigma >= 0);
    /*
    if (rot_sigma <= 0) {
      rot_sigma = 5*M_PI / 180.0;
    }
    */
    double child_rot = atan2(bbox_child.part_x_axis(1), bbox_child.part_x_axis(0));
    double parent_rot = atan2(bbox_parent.part_x_axis(1), bbox_parent.part_x_axis(0));

//     assert(child_scale == 1.0);
//     assert(parent_scale == 1.0);

    double_vector joint_pos_child = bbox_child.part_pos + 
      child_scale * bbox_child.part_x_axis * joint.offset_c(0) + 
      child_scale * bbox_child.part_y_axis * joint.offset_c(1);

    double_vector joint_pos_parent = bbox_parent.part_pos + 
      parent_scale * bbox_parent.part_x_axis * joint.offset_p(0) + 
      parent_scale * bbox_parent.part_y_axis * joint.offset_p(1);
    
    /* need to discretize the values as work on integer grid */
    joint_pos_child(0) = round(joint_pos_child(0));
    joint_pos_child(1) = round(joint_pos_child(1));
    joint_pos_parent(0) = round(joint_pos_parent(0));
    joint_pos_parent(1) = round(joint_pos_parent(1));    
    
    joint_pos_offset = joint_pos_child - joint_pos_parent;
    
    /* discretize rotation mean */
    double rot_mean = joint.rot_mean;
    if (rot_step_size > 0 && rot_step_size <= M_PI){
      rot_mean = boost_math::round(joint.rot_mean / rot_step_size) * rot_step_size;
    }
    joint_rot_offset = (child_rot - parent_rot) - rot_mean;
    
    /*
    while (joint_rot_offset < -M_PI)
      joint_rot_offset += M_PI;

    while (joint_rot_offset > M_PI)
      joint_rot_offset -= M_PI;
    */
    /*
    while (joint_rot_offset < -M_PI)
      joint_rot_offset += 2*M_PI;

    while (joint_rot_offset > M_PI)
      joint_rot_offset -= 2*M_PI;
    */
    /*
    while (joint_rot_offset < -2*M_PI)
      joint_rot_offset += 2*M_PI;

    while (joint_rot_offset > 2*M_PI)
      joint_rot_offset -= 2*M_PI;
    */
    /*
    while (joint_rot_offset < 0)
      joint_rot_offset += 2*M_PI;

    while (joint_rot_offset > 2*M_PI)
      joint_rot_offset -= 2*M_PI;
    */
    
    //assert(joint_rot_offset >= -M_PI && joint_rot_offset <= M_PI);
    // no wraparound
    
    //assert(joint_rot_offset >= -2*M_PI && joint_rot_offset <= 2*M_PI);
    
    /**
       how to estimate the vairance for parts at different scales ?
       
       MA: for now use the average scale
     */
    
    double detC1 = joint.detC;
    assert(detC1 > 0);
    
    double avg_scale = 0.5*(parent_scale + child_scale);
    //double avg_scale = std::max(parent_scale,child_scale);
    double_matrix inv_scaleC1 = joint.invC / square(avg_scale);

    //double d_pos = inner_prod(joint_pos_offset, ublas::prod<double_vector>(invC1, joint_pos_offset));
    double d_pos = inner_prod(joint_pos_offset, ublas::prod<double_vector>(inv_scaleC1, joint_pos_offset));
    double d_rot = square(joint_rot_offset) / square(rot_sigma);

    double p_joint;
    
    /** 
	MA: this is supposed to be comparable accross scales 

	the normalization constant does not depend on scales since we have implicitly rescaled positions to training scale
     */
    p_joint = exp(-0.5*(d_pos + d_rot)) / (2*M_PI*sqrt(2*M_PI) * sqrt(detC1) * rot_sigma);
    /*
    cout << "d_pos: " << d_pos << endl;  
    cout << p_joint << endl;
    cout << detC1 << endl;
    cout << rot_sigma << endl;
    */
    //p_joint = exp(-(d_pos + d_rot)); // (2*M_PI*sqrt(2*M_PI) * sqrt(detC1) * rot_sigma);
    /*
     cout << "rot_sigma: "  << rot_sigma << endl;
     cout << "joint.C:" << endl;
     boost_math::print_matrix(joint.C);
     //cout << "invC:" << endl; 
     //boost_math::print_matrix(invC1);
     cout << "d_pos: " << d_pos << endl;  
     cout << "d_rot: " << d_rot << endl; 
     cout << "p_joint: "  << p_joint << endl;
    */
    
    //assert(p_joint >= 0);
    return p_joint;
  }