pct_webcam.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. By downloading, copying, installing or using the software you agree to this license.
  3. If you do not agree to this license, do not download, install,
  4. copy or use the software.
  5. License Agreement
  6. For Open Source Computer Vision Library
  7. (3-clause BSD License)
  8. Copyright (C) 2000-2016, Intel Corporation, all rights reserved.
  9. Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
  10. Copyright (C) 2009-2016, NVIDIA Corporation, all rights reserved.
  11. Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
  12. Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved.
  13. Copyright (C) 2015-2016, Itseez Inc., all rights reserved.
  14. Third party copyrights are property of their respective owners.
  15. Redistribution and use in source and binary forms, with or without modification,
  16. are permitted provided that the following conditions are met:
  17. * Redistributions of source code must retain the above copyright notice,
  18. this list of conditions and the following disclaimer.
  19. * Redistributions in binary form must reproduce the above copyright notice,
  20. this list of conditions and the following disclaimer in the documentation
  21. and/or other materials provided with the distribution.
  22. * Neither the names of the copyright holders nor the names of the contributors
  23. may be used to endorse or promote products derived from this software
  24. without specific prior written permission.
  25. This software is provided by the copyright holders and contributors "as is" and
  26. any express or implied warranties, including, but not limited to, the implied
  27. warranties of merchantability and fitness for a particular purpose are disclaimed.
  28. In no event shall copyright holders or contributors be liable for any direct,
  29. indirect, incidental, special, exemplary, or consequential damages
  30. (including, but not limited to, procurement of substitute goods or services;
  31. loss of use, data, or profits; or business interruption) however caused
  32. and on any theory of liability, whether in contract, strict liability,
  33. or tort (including negligence or otherwise) arising in any way out of
  34. the use of this software, even if advised of the possibility of such damage.
  35. */
  36. /*
  37. Contributed by Gregor Kovalcik <gregor dot kovalcik at gmail dot com>
  38. based on code provided by Martin Krulis, Jakub Lokoc and Tomas Skopal.
  39. References:
  40. Martin Krulis, Jakub Lokoc, Tomas Skopal.
  41. Efficient Extraction of Clustering-Based Feature Signatures Using GPU Architectures.
  42. Multimedia tools and applications, 75(13), pp.: 8071–8103, Springer, ISSN: 1380-7501, 2016
  43. Christian Beecks, Merih Seran Uysal, Thomas Seidl.
  44. Signature quadratic form distance.
  45. In Proceedings of the ACM International Conference on Image and Video Retrieval, pages 438-445.
  46. ACM, 2010.
  47. */
  48. #include <opencv2/core.hpp>
  49. #include <opencv2/highgui.hpp>
  50. #include <opencv2/xfeatures2d.hpp>
  51. #include <iostream>
  52. #include <string>
  53. using namespace std;
  54. using namespace cv;
  55. using namespace xfeatures2d;
  56. void printHelpMessage(void);
  57. void printHelpMessage(void)
  58. {
  59. cout << "Example of the PCTSignatures algorithm.\n\n"
  60. "This program computes and visualizes position-color-texture signatures\n"
  61. "using images from webcam if available.\n\n"
  62. "Usage:\n"
  63. "pct_webcam [sample_count] [seed_count]\n"
  64. "Note: sample_count must be greater or equal to seed_count.";
  65. }
  66. /** @brief
  67. Example of the PCTSignatures algorithm.
  68. This program computes and visualizes position-color-texture signatures
  69. of images taken from webcam if available.
  70. */
  71. int main(int argc, char** argv)
  72. {
  73. // define variables
  74. Mat frame, signature, result;
  75. int initSampleCount = 2000;
  76. int initSeedCount = 400;
  77. int grayscaleBitsPerPixel = 4;
  78. // parse for help argument
  79. {
  80. for (int i = 1; i < argc; i++)
  81. {
  82. if ((string)argv[i] == "-h" || (string)argv[i] == "--help")
  83. {
  84. printHelpMessage();
  85. return 0;
  86. }
  87. }
  88. }
  89. // parse optional arguments
  90. if (argc > 1) // sample count
  91. {
  92. initSampleCount = atoi(argv[1]);
  93. if (initSampleCount <= 0)
  94. {
  95. cerr << "Sample count have to be a positive integer: " << argv[1] << endl;
  96. return 1;
  97. }
  98. initSeedCount = (int)floor(static_cast<float>(initSampleCount / 4));
  99. initSeedCount = std::max(1, initSeedCount); // fallback if sample count == 1
  100. }
  101. if (argc > 2) // seed count
  102. {
  103. initSeedCount = atoi(argv[2]);
  104. if (initSeedCount <= 0)
  105. {
  106. cerr << "Seed count have to be a positive integer: " << argv[2] << endl;
  107. return 1;
  108. }
  109. if (initSeedCount > initSampleCount)
  110. {
  111. cerr << "Seed count have to be lower or equal to sample count!" << endl;
  112. return 1;
  113. }
  114. }
  115. // create algorithm
  116. Ptr<PCTSignatures> pctSignatures = PCTSignatures::create(initSampleCount, initSeedCount, PCTSignatures::UNIFORM);
  117. pctSignatures->setGrayscaleBits(grayscaleBitsPerPixel);
  118. // open video capture device
  119. VideoCapture videoCapture;
  120. if (!videoCapture.open(0))
  121. {
  122. cerr << "Unable to open the first video capture device with ID = 0!" << endl;
  123. return 1;
  124. }
  125. // Create windows for display.
  126. namedWindow("Source", WINDOW_AUTOSIZE);
  127. namedWindow("Result", WINDOW_AUTOSIZE);
  128. // run drawing loop
  129. for (;;)
  130. {
  131. videoCapture >> frame;
  132. if (frame.empty()) break; // end of video stream
  133. pctSignatures->computeSignature(frame, signature);
  134. PCTSignatures::drawSignature(Mat::zeros(frame.size(), frame.type()), signature, result);
  135. imshow("Source", frame); // Show our images inside the windows.
  136. imshow("Result", result);
  137. if (waitKey(1) == 27) break; // stop videocapturing by pressing ESC
  138. }
  139. return 0;
  140. }