Exemple #1
0
int luaopen_libcutorch(lua_State *L)
{
  lua_newtable(L);
  luaL_setfuncs(L, cutorch_stuff__, 0);

  THCState* state = (THCState*)malloc(sizeof(THCState));
  THCudaInit(state);

  /* Register torch.CudaHostAllocator. */
  luaT_pushudata(L, state->cudaHostAllocator, "torch.Allocator");
  lua_setfield(L, -2, "CudaHostAllocator");

#ifdef USE_MAGMA
  THCMagma_init(state);
  lua_pushboolean(L, 1);
  lua_setfield(L, -2, "magma");
#endif

  cutorch_CudaStorage_init(L);
  cutorch_CudaTensor_init(L);
  cutorch_CudaTensorMath_init(L);
  cutorch_CudaTensorOperator_init(L);

  /* Store state in cutorch table. */
  lua_pushlightuserdata(L, state);
  lua_setfield(L, -2, "_state");

  return 1;
}
Exemple #2
0
DLL_EXPORT int luaopen_libcutorch(lua_State *L)
{
  lua_newtable(L);
  luaL_register(L, NULL, cutorch_stuff__);

  THCudaInit();

  cutorch_CudaStorage_init(L);
  cutorch_CudaTensor_init(L);
  cutorch_CudaTensorMath_init(L);

  return 1;
}
int main(int argc, char** argv)
{
  THCState *state = (THCState*)malloc(sizeof(THCState));
  THCudaInit(state);

  if(argc < 3)
  {
    std::cout << "arguments: [network] [image1] [image2]\n";
    return 1;
  }

  const char *network_path = argv[1];
  auto net = loadNetwork(state, network_path);

  // load the images
  cv::Mat ima = cv::imread(argv[2]);
  cv::Mat imb = cv::imread(argv[3]);

  if(ima.empty() || imb.empty())
  {
    std::cout << "images not found\n";
    return 1;
  }

  cv::Mat ima_gray, imb_gray;
  cv::cvtColor(ima, ima_gray, cv::COLOR_BGR2GRAY);
  cv::cvtColor(imb, imb_gray, cv::COLOR_BGR2GRAY);

  // Here we set min_area parameter to a bigger value, like that minimal size
  // of a patch will be around 11x11, because the network was trained on bigger patches
  // this parameter is important in practice
  cv::Ptr<cv::MSER> detector = cv::MSER::create(5, 620);
  std::vector<cv::KeyPoint> kpa, kpb;
  detector->detect(ima_gray, kpa);
  detector->detect(imb_gray, kpb);
  std::cout << "image A MSER points detected: " << kpa.size() << std::endl;
  std::cout << "image B MSER points detected: " << kpb.size() << std::endl;

  std::vector<cv::Mat> patches_a, patches_b;
  extractPatches(ima_gray, kpa, patches_a);
  extractPatches(imb_gray, kpb, patches_b);

  cv::Mat descriptors_a, descriptors_b;
  extractDescriptors(state, net, patches_a, descriptors_a);
  extractDescriptors(state, net, patches_b, descriptors_b);

  cv::FlannBasedMatcher matcher;
  std::vector<cv::DMatch> matches;
  matcher.match( descriptors_a, descriptors_b, matches );

  double max_dist = 0; double min_dist = 100;

  //-- Quick calculation of max and min distances between keypoints
  for( int i = 0; i < descriptors_a.rows; i++ )
  { double dist = matches[i].distance;
    if( dist < min_dist ) min_dist = dist;
    if( dist > max_dist ) max_dist = dist;
  }

  printf("-- Max dist : %f \n", max_dist );
  printf("-- Min dist : %f \n", min_dist );


  std::vector<cv::DMatch> good_matches;
  for( int i = 0; i < descriptors_a.rows; i++ )
  { if( matches[i].distance <= std::max(4*min_dist, 0.02) )
    { good_matches.push_back( matches[i]); }
  }

  //-- Draw only "good" matches
  float f = 0.25;
  cv::resize(ima, ima, cv::Size(), f, f);
  cv::resize(imb, imb, cv::Size(), f, f);
  for(auto &it: kpa) { it.pt *= f; it.size *= f; }
  for(auto &it: kpb) { it.pt *= f; it.size *= f; }
  cv::Mat img_matches;
  cv::drawMatches( ima, kpa, imb, kpb,
               good_matches, img_matches, cv::Scalar::all(-1), cv::Scalar::all(-1),
               std::vector<char>(), cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

  for(auto &it : kpa)
    cv::circle(ima, cv::Point(it.pt.x, it.pt.y), it.size, cv::Scalar(255,255,0));
  for(auto &it : kpb)
    cv::circle(imb, cv::Point(it.pt.x, it.pt.y), it.size, cv::Scalar(255,255,0));

  cv::imshow("matches", img_matches);
  //cv::imshow("keypoints image 1", ima);
  //cv::imshow("keypoints image 2", imb);
  cv::waitKey();
  THCudaShutdown(state);

  return 0;
}