random_pattern_generator.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "opencv2/ccalib/randpattern.hpp"
  2. using namespace cv;
  3. const char * usage =
  4. "\n example command line for generating a random pattern. \n"
  5. " random_patterng_generator -iw 600 -ih 850 pattern.png\n"
  6. "\n";
  7. static void help()
  8. {
  9. printf("\n This is a sample for generating a random pattern that can be used for calibration.\n"
  10. "Usage: random_patterng_generator\n"
  11. " -iw <image_width> # the width of pattern image\n"
  12. " -ih <image_height> # the height of pattern image\n"
  13. " filename # the filename for pattern image \n"
  14. );
  15. printf("\n %s", usage);
  16. }
  17. int main(int argc, char** argv)
  18. {
  19. const char* filename = 0;
  20. Mat pattern;
  21. int width = 0, height = 0;
  22. if(argc < 2)
  23. {
  24. help();
  25. return 1;
  26. }
  27. for (int i = 1; i < argc; ++i)
  28. {
  29. const char* s = argv[i];
  30. if(strcmp(s, "-iw") == 0)
  31. {
  32. if(sscanf(argv[++i], "%d", &width) != 1 || width <= 0)
  33. return fprintf( stderr, "Invalid pattern image width\n"), -1;
  34. }
  35. else if(strcmp(s, "-ih") == 0)
  36. {
  37. if(sscanf(argv[++i], "%d", &height) != 1 || height <= 0)
  38. return fprintf( stderr, "Invalid pattern image height\n"), -1;
  39. }
  40. else if( s[0] != '-')
  41. {
  42. filename = s;
  43. }
  44. else
  45. {
  46. return fprintf( stderr, "Unknown option %s\n", s ), -1;
  47. }
  48. }
  49. randpattern::RandomPatternGenerator generator(width, height);
  50. generator.generatePattern();
  51. pattern = generator.getPattern();
  52. imwrite(filename, pattern);
  53. }