paillou_demo.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/paillou_filter.hpp"
  41. using namespace cv;
  42. using namespace cv::ximgproc;
  43. #include <iostream>
  44. using namespace std;
  45. int aa = 100, ww = 10;
  46. const char* window_name = "Gradient Modulus";
  47. static void DisplayImage(Mat x,string s)
  48. {
  49. vector<Mat> sx;
  50. split(x, sx);
  51. vector<double> minVal(3), maxVal(3);
  52. for (int i = 0; i < static_cast<int>(sx.size()); i++)
  53. {
  54. minMaxLoc(sx[i], &minVal[i], &maxVal[i]);
  55. }
  56. maxVal[0] = *max_element(maxVal.begin(), maxVal.end());
  57. minVal[0] = *min_element(minVal.begin(), minVal.end());
  58. Mat uc;
  59. x.convertTo(uc, CV_8U,255/(maxVal[0]-minVal[0]),-255*minVal[0]/(maxVal[0]-minVal[0]));
  60. imshow(s, uc);
  61. }
  62. /**
  63. * @function paillouFilter
  64. * @brief Trackbar callback
  65. */
  66. static void PaillouFilter(int, void*pm)
  67. {
  68. Mat img = *((Mat*)pm);
  69. Mat dst;
  70. double a=aa/100.0, w=ww/100.0;
  71. Mat rx,ry;
  72. GradientPaillouX(img, rx, a, w);
  73. GradientPaillouY(img, ry, a, w);
  74. DisplayImage(rx, "Gx");
  75. DisplayImage(ry, "Gy");
  76. add(rx.mul(rx), ry.mul(ry), dst);
  77. sqrt(dst, dst);
  78. DisplayImage(dst, window_name );
  79. }
  80. int main(int argc, char* argv[])
  81. {
  82. if (argc < 2)
  83. {
  84. cout << "usage: paillou_demo [image]" << endl;
  85. return 1;
  86. }
  87. Mat img = imread(argv[1]);
  88. if (img.empty())
  89. {
  90. cout << "File not found or empty image\n";
  91. return 1;
  92. }
  93. imshow("Original",img);
  94. namedWindow( window_name, WINDOW_AUTOSIZE );
  95. /// Create a Trackbar for user to enter threshold
  96. createTrackbar( "a:",window_name, &aa, 400, PaillouFilter, &img );
  97. createTrackbar( "w:", window_name, &ww, 400, PaillouFilter, &img );
  98. PaillouFilter(0, &img);
  99. waitKey();
  100. return 0;
  101. }