Threshold.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @file Threshold.cpp
  3. * @brief Sample code that shows how to use the diverse threshold options offered by OpenCV
  4. * @author OpenCV team
  5. */
  6. #include "opencv2/imgproc.hpp"
  7. #include "opencv2/imgcodecs.hpp"
  8. #include "opencv2/highgui.hpp"
  9. #include <iostream>
  10. using namespace cv;
  11. using std::cout;
  12. /// Global variables
  13. int threshold_value = 0;
  14. int threshold_type = 3;
  15. int const max_value = 255;
  16. int const max_type = 4;
  17. int const max_binary_value = 255;
  18. Mat src, src_gray, dst;
  19. const char* window_name = "Threshold Demo";
  20. const char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
  21. const char* trackbar_value = "Value";
  22. //![Threshold_Demo]
  23. /**
  24. * @function Threshold_Demo
  25. */
  26. static void Threshold_Demo( int, void* )
  27. {
  28. /* 0: Binary
  29. 1: Binary Inverted
  30. 2: Threshold Truncated
  31. 3: Threshold to Zero
  32. 4: Threshold to Zero Inverted
  33. */
  34. threshold( src_gray, dst, threshold_value, max_binary_value, threshold_type );
  35. imshow( window_name, dst );
  36. }
  37. //![Threshold_Demo]
  38. /**
  39. * @function main
  40. */
  41. int main( int argc, char** argv )
  42. {
  43. //! [load]
  44. String imageName("stuff.jpg"); // by default
  45. if (argc > 1)
  46. {
  47. imageName = argv[1];
  48. }
  49. src = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
  50. if (src.empty())
  51. {
  52. cout << "Cannot read the image: " << imageName << std::endl;
  53. return -1;
  54. }
  55. cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to Gray
  56. //! [load]
  57. //! [window]
  58. namedWindow( window_name, WINDOW_AUTOSIZE ); // Create a window to display results
  59. //! [window]
  60. //! [trackbar]
  61. createTrackbar( trackbar_type,
  62. window_name, &threshold_type,
  63. max_type, Threshold_Demo ); // Create a Trackbar to choose type of Threshold
  64. createTrackbar( trackbar_value,
  65. window_name, &threshold_value,
  66. max_value, Threshold_Demo ); // Create a Trackbar to choose Threshold value
  67. //! [trackbar]
  68. Threshold_Demo( 0, 0 ); // Call the function to initialize
  69. /// Wait until the user finishes the program
  70. waitKey();
  71. return 0;
  72. }