Threshold_inRange.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "opencv2/imgproc.hpp"
  2. #include "opencv2/highgui.hpp"
  3. #include "opencv2/videoio.hpp"
  4. #include <iostream>
  5. using namespace cv;
  6. /** Global Variables */
  7. const int max_value_H = 360/2;
  8. const int max_value = 255;
  9. const String window_capture_name = "Video Capture";
  10. const String window_detection_name = "Object Detection";
  11. int low_H = 0, low_S = 0, low_V = 0;
  12. int high_H = max_value_H, high_S = max_value, high_V = max_value;
  13. //! [low]
  14. static void on_low_H_thresh_trackbar(int, void *)
  15. {
  16. low_H = min(high_H-1, low_H);
  17. setTrackbarPos("Low H", window_detection_name, low_H);
  18. }
  19. //! [low]
  20. //! [high]
  21. static void on_high_H_thresh_trackbar(int, void *)
  22. {
  23. high_H = max(high_H, low_H+1);
  24. setTrackbarPos("High H", window_detection_name, high_H);
  25. }
  26. //! [high]
  27. static void on_low_S_thresh_trackbar(int, void *)
  28. {
  29. low_S = min(high_S-1, low_S);
  30. setTrackbarPos("Low S", window_detection_name, low_S);
  31. }
  32. static void on_high_S_thresh_trackbar(int, void *)
  33. {
  34. high_S = max(high_S, low_S+1);
  35. setTrackbarPos("High S", window_detection_name, high_S);
  36. }
  37. static void on_low_V_thresh_trackbar(int, void *)
  38. {
  39. low_V = min(high_V-1, low_V);
  40. setTrackbarPos("Low V", window_detection_name, low_V);
  41. }
  42. static void on_high_V_thresh_trackbar(int, void *)
  43. {
  44. high_V = max(high_V, low_V+1);
  45. setTrackbarPos("High V", window_detection_name, high_V);
  46. }
  47. int main(int argc, char* argv[])
  48. {
  49. //! [cap]
  50. VideoCapture cap(argc > 1 ? atoi(argv[1]) : 0);
  51. //! [cap]
  52. //! [window]
  53. namedWindow(window_capture_name);
  54. namedWindow(window_detection_name);
  55. //! [window]
  56. //! [trackbar]
  57. // Trackbars to set thresholds for HSV values
  58. createTrackbar("Low H", window_detection_name, &low_H, max_value_H, on_low_H_thresh_trackbar);
  59. createTrackbar("High H", window_detection_name, &high_H, max_value_H, on_high_H_thresh_trackbar);
  60. createTrackbar("Low S", window_detection_name, &low_S, max_value, on_low_S_thresh_trackbar);
  61. createTrackbar("High S", window_detection_name, &high_S, max_value, on_high_S_thresh_trackbar);
  62. createTrackbar("Low V", window_detection_name, &low_V, max_value, on_low_V_thresh_trackbar);
  63. createTrackbar("High V", window_detection_name, &high_V, max_value, on_high_V_thresh_trackbar);
  64. //! [trackbar]
  65. Mat frame, frame_HSV, frame_threshold;
  66. while (true) {
  67. //! [while]
  68. cap >> frame;
  69. if(frame.empty())
  70. {
  71. break;
  72. }
  73. // Convert from BGR to HSV colorspace
  74. cvtColor(frame, frame_HSV, COLOR_BGR2HSV);
  75. // Detect the object based on HSV Range Values
  76. inRange(frame_HSV, Scalar(low_H, low_S, low_V), Scalar(high_H, high_S, high_V), frame_threshold);
  77. //! [while]
  78. //! [show]
  79. // Show the frames
  80. imshow(window_capture_name, frame);
  81. imshow(window_detection_name, frame_threshold);
  82. //! [show]
  83. char key = (char) waitKey(30);
  84. if (key == 'q' || key == 27)
  85. {
  86. break;
  87. }
  88. }
  89. return 0;
  90. }