pct_signatures.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 computing and visualizing\n"
  60. "image signature for one image, or comparing multiple images with the first\n"
  61. "image using the signature quadratic form distance.\n\n"
  62. "Usage: pct_signatures ImageToProcessAndDisplay\n"
  63. "or: pct_signatures ReferenceImage [ImagesToCompareWithTheReferenceImage]\n\n"
  64. "The program has 2 modes:\n"
  65. "- single argument: program computes and visualizes the image signature\n"
  66. "- multiple arguments: program compares the first image to the others\n"
  67. " using pct signatures and signature quadratic form distance (SQFD)";
  68. }
  69. /** @brief
  70. Example of the PCTSignatures algorithm.
  71. The program has 2 modes:
  72. - single argument mode, where the program computes and visualizes the image signature
  73. - multiple argument mode, where the program compares the first image to the others
  74. using signatures and signature quadratic form distance (SQFD)
  75. */
  76. int main(int argc, char** argv)
  77. {
  78. if (argc < 2) // Check arguments
  79. {
  80. printHelpMessage();
  81. return 1;
  82. }
  83. Mat source;
  84. source = imread(argv[1]); // Read the file
  85. if (!source.data) // Check for invalid input
  86. {
  87. cerr << "Could not open or find the image: " << argv[1];
  88. return -1;
  89. }
  90. Mat signature, result; // define variables
  91. int initSampleCount = 2000;
  92. int initSeedCount = 400;
  93. int grayscaleBitsPerPixel = 4;
  94. vector<Point2f> initPoints;
  95. namedWindow("Source", WINDOW_AUTOSIZE); // Create windows for display.
  96. namedWindow("Result", WINDOW_AUTOSIZE);
  97. // create the algorithm
  98. PCTSignatures::generateInitPoints(initPoints, initSampleCount, PCTSignatures::UNIFORM);
  99. Ptr<PCTSignatures> pctSignatures = PCTSignatures::create(initPoints, initSeedCount);
  100. pctSignatures->setGrayscaleBits(grayscaleBitsPerPixel);
  101. // compute and visualize the first image
  102. double start = (double)getTickCount();
  103. pctSignatures->computeSignature(source, signature);
  104. double end = (double)getTickCount();
  105. cout << "Signature of the reference image computed in " << (end - start) / (getTickFrequency() * 1.0f) << " seconds." << endl;
  106. PCTSignatures::drawSignature(source, signature, result);
  107. imshow("Source", source); // show the result
  108. imshow("Result", result);
  109. if (argc == 2) // single image -> finish right after the visualization
  110. {
  111. waitKey(0); // Wait for user input
  112. return 0;
  113. }
  114. // multiple images -> compare to the first one
  115. else
  116. {
  117. vector<Mat> images;
  118. vector<Mat> signatures;
  119. vector<float> distances;
  120. for (int i = 2; i < argc; i++)
  121. {
  122. Mat image = imread(argv[i]);
  123. if (!source.data) // Check for invalid input
  124. {
  125. cerr << "Could not open or find the image: " << argv[i] << std::endl;
  126. return 1;
  127. }
  128. images.push_back(image);
  129. }
  130. pctSignatures->computeSignatures(images, signatures);
  131. Ptr<PCTSignaturesSQFD> pctSQFD = PCTSignaturesSQFD::create();
  132. pctSQFD->computeQuadraticFormDistances(signature, signatures, distances);
  133. for (int i = 0; i < (int)(distances.size()); i++)
  134. {
  135. cout << "Image: " << argv[i + 2] << ", similarity: " << distances[i] << endl;
  136. }
  137. waitKey(0); // Wait for user input
  138. }
  139. return 0;
  140. }