computeSaliency.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include <opencv2/core/utility.hpp>
  42. #include <opencv2/saliency.hpp>
  43. #include <opencv2/highgui.hpp>
  44. #include <iostream>
  45. using namespace std;
  46. using namespace cv;
  47. using namespace saliency;
  48. static const char* keys =
  49. { "{@saliency_algorithm | | Saliency algorithm <saliencyAlgorithmType.[saliencyAlgorithmTypeSubType]> }"
  50. "{@video_name | | video name }"
  51. "{@start_frame |1| Start frame }"
  52. "{@training_path |ObjectnessTrainedModel| Path of the folder containing the trained files}" };
  53. static void help()
  54. {
  55. cout << "\nThis example shows the functionality of \"Saliency \""
  56. "Call:\n"
  57. "./example_saliency_computeSaliency <saliencyAlgorithmSubType> <video_name> <start_frame> \n"
  58. << endl;
  59. }
  60. int main( int argc, char** argv )
  61. {
  62. CommandLineParser parser( argc, argv, keys );
  63. String saliency_algorithm = parser.get<String>( 0 );
  64. String video_name = parser.get<String>( 1 );
  65. int start_frame = parser.get<int>( 2 );
  66. String training_path = parser.get<String>( 3 );
  67. if( saliency_algorithm.empty() || video_name.empty() )
  68. {
  69. help();
  70. return -1;
  71. }
  72. //open the capture
  73. VideoCapture cap;
  74. cap.open( video_name );
  75. cap.set( CAP_PROP_POS_FRAMES, start_frame );
  76. if( !cap.isOpened() )
  77. {
  78. help();
  79. cout << "***Could not initialize capturing...***\n";
  80. cout << "Current parameter's value: \n";
  81. parser.printMessage();
  82. return -1;
  83. }
  84. Mat frame;
  85. //instantiates the specific Saliency
  86. Ptr<Saliency> saliencyAlgorithm;
  87. Mat binaryMap;
  88. Mat image;
  89. cap >> frame;
  90. if( frame.empty() )
  91. {
  92. return 0;
  93. }
  94. frame.copyTo( image );
  95. if( saliency_algorithm.find( "SPECTRAL_RESIDUAL" ) == 0 )
  96. {
  97. Mat saliencyMap;
  98. saliencyAlgorithm = StaticSaliencySpectralResidual::create();
  99. if( saliencyAlgorithm->computeSaliency( image, saliencyMap ) )
  100. {
  101. StaticSaliencySpectralResidual spec;
  102. spec.computeBinaryMap( saliencyMap, binaryMap );
  103. imshow( "Saliency Map", saliencyMap );
  104. imshow( "Original Image", image );
  105. imshow( "Binary Map", binaryMap );
  106. waitKey( 0 );
  107. }
  108. }
  109. else if( saliency_algorithm.find( "FINE_GRAINED" ) == 0 )
  110. {
  111. Mat saliencyMap;
  112. saliencyAlgorithm = StaticSaliencyFineGrained::create();
  113. if( saliencyAlgorithm->computeSaliency( image, saliencyMap ) )
  114. {
  115. imshow( "Saliency Map", saliencyMap );
  116. imshow( "Original Image", image );
  117. waitKey( 0 );
  118. }
  119. }
  120. else if( saliency_algorithm.find( "BING" ) == 0 )
  121. {
  122. if( training_path.empty() )
  123. {
  124. cout << "Path of trained files missing! " << endl;
  125. return -1;
  126. }
  127. else
  128. {
  129. saliencyAlgorithm = ObjectnessBING::create();
  130. vector<Vec4i> saliencyMap;
  131. saliencyAlgorithm.dynamicCast<ObjectnessBING>()->setTrainingPath( training_path );
  132. saliencyAlgorithm.dynamicCast<ObjectnessBING>()->setBBResDir( "Results" );
  133. if( saliencyAlgorithm->computeSaliency( image, saliencyMap ) )
  134. {
  135. int ndet = int(saliencyMap.size());
  136. std::cout << "Objectness done " << ndet << std::endl;
  137. // The result are sorted by objectness. We only use the first maxd boxes here.
  138. int maxd = 7, step = 255 / maxd, jitter=9; // jitter to separate single rects
  139. Mat draw = image.clone();
  140. for (int i = 0; i < std::min(maxd, ndet); i++) {
  141. Vec4i bb = saliencyMap[i];
  142. Scalar col = Scalar(((i*step)%255), 50, 255-((i*step)%255));
  143. Point off(theRNG().uniform(-jitter,jitter), theRNG().uniform(-jitter,jitter));
  144. rectangle(draw, Point(bb[0]+off.x, bb[1]+off.y), Point(bb[2]+off.x, bb[3]+off.y), col, 2);
  145. rectangle(draw, Rect(20, 20+i*10, 10,10), col, -1); // mini temperature scale
  146. }
  147. imshow("BING", draw);
  148. waitKey();
  149. }
  150. else
  151. {
  152. std::cout << "No saliency found for " << video_name << std::endl;
  153. }
  154. }
  155. }
  156. else if( saliency_algorithm.find( "BinWangApr2014" ) == 0 )
  157. {
  158. saliencyAlgorithm = MotionSaliencyBinWangApr2014::create();
  159. saliencyAlgorithm.dynamicCast<MotionSaliencyBinWangApr2014>()->setImagesize( image.cols, image.rows );
  160. saliencyAlgorithm.dynamicCast<MotionSaliencyBinWangApr2014>()->init();
  161. bool paused = false;
  162. for ( ;; )
  163. {
  164. if( !paused )
  165. {
  166. cap >> frame;
  167. if( frame.empty() )
  168. {
  169. return 0;
  170. }
  171. cvtColor( frame, frame, COLOR_BGR2GRAY );
  172. Mat saliencyMap;
  173. saliencyAlgorithm->computeSaliency( frame, saliencyMap );
  174. imshow( "image", frame );
  175. imshow( "saliencyMap", saliencyMap * 255 );
  176. }
  177. char c = (char) waitKey( 2 );
  178. if( c == 'q' )
  179. break;
  180. if( c == 'p' )
  181. paused = !paused;
  182. }
  183. }
  184. return 0;
  185. }