deriche_demo.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. *
  6. *
  7. * License Agreement
  8. * For Open Source Computer Vision Library
  9. * (3 - clause BSD License)
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met :
  13. *
  14. * *Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and / or other materials provided with the distribution.
  20. *
  21. * * Neither the names of the copyright holders nor the names of the contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  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. #include <opencv2/core.hpp>
  37. #include <opencv2/core/utility.hpp>
  38. #include <opencv2/highgui.hpp>
  39. #include <opencv2/ximgproc.hpp>
  40. #include "opencv2/ximgproc/deriche_filter.hpp"
  41. using namespace cv;
  42. using namespace cv::ximgproc;
  43. #include <iostream>
  44. using namespace std;
  45. int alDerive=100;
  46. int alMean=100;
  47. Ptr<Mat> img;
  48. const string & winName = "Gradient Modulus";
  49. static void DisplayImage(Mat x,string s)
  50. {
  51. vector<Mat> sx;
  52. split(x, sx);
  53. vector<double> minVal(3), maxVal(3);
  54. for (size_t i = 0; i < sx.size(); i++)
  55. {
  56. minMaxLoc(sx[i], &minVal[i], &maxVal[i]);
  57. }
  58. maxVal[0] = *max_element(maxVal.begin(), maxVal.end());
  59. minVal[0] = *min_element(minVal.begin(), minVal.end());
  60. Mat uc;
  61. x.convertTo(uc, CV_8U,255/(maxVal[0]-minVal[0]),-255*minVal[0]/(maxVal[0]-minVal[0]));
  62. imshow(s, uc);
  63. }
  64. /**
  65. * @function DericheFilter
  66. * @brief Trackbar callback
  67. */
  68. static void DericheFilter(int, void*)
  69. {
  70. Mat dst;
  71. double d=alDerive/100.0,m=alMean/100.0;
  72. Mat rx,ry;
  73. GradientDericheX(*img.get(),rx,d,m);
  74. GradientDericheY(*img.get(),ry,d,m);
  75. DisplayImage(rx, "Gx");
  76. DisplayImage(ry, "Gy");
  77. add(rx.mul(rx),ry.mul(ry),dst);
  78. sqrt(dst,dst);
  79. DisplayImage(dst, winName );
  80. }
  81. int main(int argc, char* argv[])
  82. {
  83. Mat *m=new Mat;
  84. cv::CommandLineParser parser(argc, argv, "{help h | | show help message}{@input | | input image}");
  85. if (parser.has("help"))
  86. {
  87. parser.printMessage();
  88. return -1;
  89. }
  90. string input_image = parser.get<string>("@input");
  91. if (input_image.empty())
  92. {
  93. parser.printMessage();
  94. parser.printErrors();
  95. return -2;
  96. }
  97. if (argc==2)
  98. *m = imread(input_image);
  99. if (m->empty())
  100. {
  101. cout << "File not found or empty image\n";
  102. return -3;
  103. }
  104. imshow("Original", *m);
  105. img =Ptr<Mat>(m);
  106. namedWindow( winName, WINDOW_AUTOSIZE );
  107. /// Create a Trackbar for user to enter threshold
  108. createTrackbar( "Derive:",winName, &alDerive, 400, DericheFilter );
  109. createTrackbar( "Mean:", winName, &alMean, 400, DericheFilter );
  110. DericheFilter(0,NULL);
  111. waitKey();
  112. return 0;
  113. }