Example #1
0
Marker::Marker() {
    id = -1;
    ssize = -1;
    for (int i = 0; i < 3; i++)
        Tvec(i) = Rvec(i) = -999999;
}
Example #2
0
Marker::Marker(const std::vector<cv::Point2f>& corners, int _id) : std::vector<cv::Point2f>(corners) {
    id = _id;
    ssize = -1;
    for (int i = 0; i < 3; i++)
        Tvec(i) = Rvec(i) = -999999;
}
int main(int argc, char **argv) {
  // Define the display
  size_t width = 640, height = 480;

  // the model name can be specified on the command line.
  std::string file_name(argv[1]), file_ext = file_name.substr(file_name.size() - 3, file_name.npos);

  cv::Rect rect;
  Renderer2d render(file_name, 0.14);
  double focal_length_x = 525, focal_length_y = 525;
  render.set_parameters(width, height, focal_length_x, focal_length_y);

  cv::Ptr<cv::linemod::Detector> detector_ptr = cv::linemod::getDefaultLINE();

  // Loop over a few views in front of the pattern
  float xy_lim = 0.5;
  for (float x = -xy_lim; x < xy_lim; x += 0.1)
    for (float y = -xy_lim; y < xy_lim; y += 0.1)
      for (float z = 0.6; z < 0.7; z += 0.1) {
        cv::Vec3f up(0, z, -y);
        up = up / norm(up);
        // Rotate the vector
        for (float theta = -10; theta < 20; theta += 10) {
          cv::Vec3f Rvec(x, y, z);
          Rvec = (theta * CV_PI / 180) * Rvec / norm(Rvec);
          cv::Matx33f R;
          cv::Rodrigues(Rvec, R);
          cv::Vec3f up_rotated = R * up;
          render.lookAt(0., y, z, up_rotated(0), up_rotated(1), up_rotated(2));
          cv::Mat img, depth, mask;
          render.render(img, depth, mask, rect);

          std::vector<cv::Mat> sources(1);
          sources[0] = img;
          //sources[1] = depth;

          detector_ptr->addTemplate(sources, "object1", mask);

//          cv::imshow("img", img);
//          cv::imshow("depth", depth);
//          cv::imshow("mask", mask);
//          cv::waitKey(0);
        }
      }

  detector_ptr->writeClasses(file_name + std::string("_templates.yaml"));

  cv::VideoCapture cap(0);
  cv::Mat img;
  int num_modalities = (int) detector_ptr->getModalities().size();
  cv::namedWindow("result");
  while (true) {
    cap >> img;

    std::vector<cv::Mat> sources(1, img);
    std::vector<cv::linemod::Match> matches;
    detector_ptr->match(sources, 93, matches);

    for (size_t i = 0; i < matches.size(); ++i) {
      const cv::linemod::Match & match = matches[i];
      const std::vector<cv::linemod::Template>& templates = detector_ptr->getTemplates(match.class_id,
          match.template_id);

      drawResponse(templates, num_modalities, img, cv::Point(match.x, match.y), detector_ptr->getT(0));
    };
    cv::imshow("result", img);
    cv::waitKey(5);
  }

  return 0;
}