Exemplo n.º 1
0
int
run_program(int argc, char **argv)
{
  CommandLineArgument<std::string> image_pathname;
  CommandLineArgument<std::string> points_pathname;
  CommandLineArgument<std::string> output_pathname;

  int radius = 5;
  cv::Scalar colour(0,0,255);
  int thickness = 1;
  int line_type = 8;
  int shift = 0;
  double wait_time;

  for (int i = 1; i < argc; i++) {
    std::string argument(argv[i]);

    if (argument == "--help") {
      print_usage();
      return 0;
    } else if (argument == "--radius") {
      radius = get_argument<int>(&i, argc, argv);
    } else if (argument == "--colour") {
      colour[2] = get_argument<int>(&i, argc, argv);
      colour[1] = get_argument<int>(&i, argc, argv);
      colour[0] = get_argument<int>(&i, argc, argv);
    } else if (argument == "--wait-time") {
      wait_time = get_argument<double>(&i, argc, argv);
    } else if (!assign_argument(argument, image_pathname, points_pathname, output_pathname)) {
      throw make_runtime_error("Do not know how to process argument '%s'",
			       argument.c_str());
    }
  }

  if (!have_arguments_p(image_pathname, points_pathname)) {
    print_usage();
    return 0;
  }

  cv::Mat img = cv::imread(image_pathname->c_str());
  if ((img.rows == 0) || (img.cols == 0))
    throw make_runtime_error("Unable to load image at path '%s'", image_pathname->c_str());

  std::vector<cv::Point_<double> > pts = load_points(points_pathname->c_str());

  for (size_t i = 0; i < pts.size(); i++) {
    cv::circle(img, pts[i], radius, colour, thickness, line_type, shift);
  }

  if (have_argument_p(output_pathname)) {
    cv::imwrite(output_pathname->c_str(), img);
  } else {
    cv::imshow("Image", img);
    cv::waitKey(1000*wait_time);
  }
  
  return 0;
}
Exemplo n.º 2
0
int
run_program(int argc, char **argv)
{
  CommandLineArgument<std::string> input_pathnames_filename;
  CommandLineArgument<std::string> output_pathnames_filename;

  CommandLineArgument<std::string> new_directory;
  CommandLineArgument<std::string> new_type;

  for (int i = 1; i < argc; i++) {
    std::string argument = argv[i];

    if (argument == "--help") {
      print_usage();
      return 0;
    } else if (argument == "--directory") {
      new_directory = get_argument(&i, argc, argv);
    } else if (argument == "--type") {
      new_type = get_argument(&i, argc, argv);
    } else if (!assign_argument(argument, input_pathnames_filename, output_pathnames_filename)) {
      throw make_runtime_error("Unable to process argument '%s'", argument.c_str());
    }
  }

  if (!have_arguments_p(input_pathnames_filename, output_pathnames_filename)) {
    print_usage();
    return -1;
  }

  std::list<std::string> input_pathnames;
  if (*input_pathnames_filename == "-")
    input_pathnames = read_list(std::cin);
  else
    input_pathnames = read_list(input_pathnames_filename->c_str());

  std::ostream *out;
  std::ofstream out_f;
  if (*output_pathnames_filename == "-")
    out = &std::cout;
  else {
    out_f.open(output_pathnames_filename->c_str(), std::ios::out | std::ios::trunc);
    if (!out_f.is_open()) 
      throw make_runtime_error("Failed to open file '%s'", output_pathnames_filename->c_str());
    out = &out_f;
  }

  if (have_arguments_p(new_directory, new_type)) 
    output_new_pathnames(input_pathnames, *out, DirectoryAndType(*new_directory, *new_type));
  else if (have_argument_p(new_directory))
    output_new_pathnames(input_pathnames, *out, DirectoryOnly(*new_directory));
  else if (have_argument_p(new_type))
    output_new_pathnames(input_pathnames, *out, TypeOnly(*new_type));

  return 0;
}
Exemplo n.º 3
0
  void perform(const std::string &command, const std::list<std::string> &arguments) {
    
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
    
    if(!CreateProcess(NULL, "", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)){ //child process
      char *argv[arguments.size() + 2];
      argv[0] = (char *)command.c_str();
      argv[arguments.size() + 1] = 0;

      std::list<std::string>::const_iterator it = arguments.begin();
      for (size_t i = 0; i < arguments.size(); i++) {
        argv[i + 1] = (char *)it->c_str();
        it++;
      }

      int rv = _execvp(command.c_str(), argv);
      if (rv == -1) 
        throw make_runtime_error("Unable to create new process: %s.", strerror(errno));
    }
    else { // parent process
      throw make_runtime_error("CreateProcess failed (%d).\n", GetLastError() );
      return;
    }
    
    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
  }
Exemplo n.º 4
0
int
run_program(int argc, char **argv)
{
  CommandLineArgument<std::string> model_pathname;

  for (int i = 1; i < argc; i++) {
    std::string argument(argv[i]);

    if (argument == "--help") {
      print_usage();
      return 0;
    } else if (!assign_argument(argument, model_pathname)) {
      throw make_runtime_error("Do not know how to process argument '%s'",
			       argument.c_str());
    }
  }

  if (!have_argument_p(model_pathname))
    model_pathname = AVATAR::DefaultAvatarModelPathname();

  if (!file_exists_p(model_pathname->c_str()))
    throw make_runtime_error("Model file '%s' does not exist",
			     model_pathname->c_str());

  AVATAR::Avatar *avatar = AVATAR::LoadAvatar(model_pathname->c_str());
  if (avatar->numberOfAvatars() == 0)
    return 0;

  print_controls();

  const int minimum_avatar = 0;
  const int maximum_avatar = avatar->numberOfAvatars() - 1;
  int index = 0;
  bool quit_application_p = false;

  printf("Number of avatars: %d\n", avatar->numberOfAvatars());
  show_avatar(avatar, index);

  while (!quit_application_p) {    
    char ch = cv::waitKey(10);
    switch (ch) {
    case 'a':      
    case 2:
      index = std::max(minimum_avatar, index - 1);
      show_avatar(avatar, index);
      break;
    case 'd':
    case 3:
      index = std::min(maximum_avatar, index + 1);
      show_avatar(avatar, index);
      break;
    case 'q':
    case 27:
      quit_application_p = true;
      break;
    case -1:
      break;
    default:
      break;
    }
  }
  
  printf("\n");
  delete avatar;
  
  return 0;
}
Exemplo n.º 5
0
int
run_program(int argc, char **argv)
{
  CommandLineArgument<std::string> points_pathname;

  Configuration cfg;
  cfg.width = 640;
  cfg.height = 480;
  cfg.window_title = "";
  cfg.circle_radius = 2;
  cfg.circle_thickness = 1;
  cfg.circle_linetype = 8;
  cfg.circle_shift = 0;
  cfg.circle_colour = cv::Scalar(0, 0, 255);
  
  for (int i = 1; i < argc; i++) {
    std::string argument(argv[i]);
    if (argument == "--help") {
      print_usage();
      return 0;
    } else if (argument == "--width") {
      cfg.width = get_argument<int>(&i, argc, argv);
    } else if (argument == "--height") {
      cfg.height = get_argument<int>(&i, argc, argv);
    } else if (argument == "--window-title") {
      cfg.window_title = get_argument(&i, argc, argv);
    } else if (argument == "--circle-radius") {
      cfg.circle_radius = get_argument<int>(&i, argc, argv);
    } else if (!assign_argument(argument, points_pathname)) {
      throw make_runtime_error("Unable to process argument: %s\n", argument.c_str());
    }
  }

  if (!have_argument_p(points_pathname)) {
    print_usage();
    return -1;
  }

  if (cfg.window_title == "") 
    cfg.window_title = points_pathname->c_str();

  std::vector<cv::Point3_<double> > shape3D;
  try {
    shape3D = load_points3(points_pathname->c_str());
  } catch (std::exception &e) {
    throw make_runtime_error("Encountered error when reading file '%s': %s", points_pathname->c_str(), e.what());
  }

  cv::Point3_<double> mean = calculate_mean(shape3D);
  for (size_t i = 0; i < shape3D.size(); i++) {
    shape3D[i] -= mean;
  }

  cv::Mat_<double> projection = cv::Mat_<double>::eye(3,4);
  if (cfg.height < cfg.width) {
    projection(0,3) = double(cfg.width - cfg.height) / 2.0;
  } else {
    projection(1,3) = double(cfg.height - cfg.width) / 2.0;
  }

  cv::Mat_<double> starting_A = calculate_similarity_transform(shape3D, cfg.height, cfg.width);
  cv::Mat_<double> A(starting_A.clone());
  
  bool quit = false;
  while (!quit) {
    int key = cv::waitKey(1);
    switch (key) {
    case 27:
      quit = true;
      break;
    case 'z':
      A = A * scaling_matrix(1.1);
      break;
    case 'x':
      A = A * scaling_matrix(0.9);
      break;
    case 'a':
      A = A * rotation_about_y_axis(-0.1);
      break;
    case 'd':
      A = A * rotation_about_y_axis(0.1);
      break;
    case 'w':
      A = A * rotation_about_x_axis(-0.1);
      break;
    case 's':
      A = A * rotation_about_x_axis(0.1);
      break;
    case 'q':
      A = A * rotation_about_z_axis(0.1);
      break;
    case 'e':
      A = A * rotation_about_z_axis(-0.1);
      break;
    case 'r':
      A = starting_A.clone();
      break;
    }

    display_points(cfg, projection * A, shape3D);
  }

  return 0;
}
Exemplo n.º 6
0
int
run_program(int argc, char **argv)
{
  CommandLineArgument<int> number_of_list_files;
  int next_command_line_argument = 0;
  bool echo_commands = false;

  for (int i = 1; ((i < argc) && (!have_argument_p(number_of_list_files))); i++) {
    std::string argument(argv[i]);
    if (argument == "--help") {
      print_usage();
      return 0;
    } else if (argument == "--echo") {
      echo_commands = true;
    } else if (!have_argument_p(number_of_list_files)) {
      number_of_list_files = argument;
      next_command_line_argument = i + 1;
    }
  }

  if (!have_argument_p(number_of_list_files)) {
    print_usage();
    return -1;
  }
  
  if (*number_of_list_files == 0)
    return 0;

  const int minimum_number_of_arguments = 0
    + *number_of_list_files
    + 1 // the command;
    + 0;

  if ((argc - next_command_line_argument) < minimum_number_of_arguments)
    throw make_runtime_error("Not enough arguments available for processing.\n");

  std::list<std::string> standard_input_list;
  bool have_read_standard_input_list = false;

  std::vector<std::string> list_filenames(*number_of_list_files);
  std::vector<std::list<std::string> > lists(*number_of_list_files);  
  for (int i = 0; i < *number_of_list_files; i++) {    
    int argument_index = i + next_command_line_argument;
    std::string list_file = argv[argument_index];
    std::list<std::string> l;
    
    if ((list_file == "-") && (have_read_standard_input_list)) {
      l = standard_input_list;
    } else if ((list_file == "-")) {
      standard_input_list = read_list(std::cin);
      have_read_standard_input_list = true;
      l = standard_input_list;
    } else if (!file_exists_p(list_file)) {
      throw make_runtime_error("List file %s does not exist.", list_file.c_str());
    } else {
      l = read_list(list_file.c_str());
    }

    list_filenames[i] = list_file;
    lists[i] = l;
  }
  next_command_line_argument += *number_of_list_files;

  // read the command and its options.
  std::string command(argv[next_command_line_argument]);
  std::list<std::string> command_arguments;
  std::copy(argv + next_command_line_argument + 1, argv + argc, std::back_inserter(command_arguments));

  // check all lists are the same size.
  for (size_t i = 0; i < lists.size(); i++) {
    if (lists[i].size() != lists[0].size())
      throw make_runtime_error("The number of entires in list %s (%d) differs to %s (%d).", 
			       list_filenames[i].c_str(),
			       lists[i].size(),
			       list_filenames[0].c_str(),
			       lists[0].size());
  }

  EchoRunner echo_runner(std::cout);
  ForkRunner fork_runner;

  Runner *runner = 0;
  if (echo_commands) 
    runner = &echo_runner;  
  else
    runner = &fork_runner;

  assert(runner);
  std::vector<std::list<std::string>::const_iterator> iterators(*number_of_list_files);
  for (int i = 0; i < *number_of_list_files; i++)
    iterators[i] = lists[i].begin();

  for (size_t i = 0; i < lists[0].size(); i++) {
    std::string invocation_command = command;
    std::list<std::string> invocation_arguments;
    std::copy(command_arguments.begin(), command_arguments.end(), std::back_inserter(invocation_arguments));
    for (size_t j = 0; j < iterators.size(); j++) {
      invocation_arguments.push_back(*iterators[j]);
      iterators[j]++;
    }

    runner->perform(invocation_command, invocation_arguments);
  }

  return 0;
}