|
| 1 | +#include "opencv2/imgproc.hpp" |
| 2 | +#include "opencv2/highgui.hpp" |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <stdlib.h> |
| 6 | + |
| 7 | +using namespace std; |
| 8 | +using namespace cv; |
| 9 | + |
| 10 | +/** Function Headers */ |
| 11 | +void on_low_r_thresh_trackbar(int, void *); |
| 12 | +void on_high_r_thresh_trackbar(int, void *); |
| 13 | +void on_low_g_thresh_trackbar(int, void *); |
| 14 | +void on_high_g_thresh_trackbar(int, void *); |
| 15 | +void on_low_b_thresh_trackbar(int, void *); |
| 16 | +void on_high_b_thresh_trackbar(int, void *); |
| 17 | + |
| 18 | +/** Global Variables */ |
| 19 | +int low_r=30, low_g=30, low_b=30; |
| 20 | +int high_r=100, high_g=100, high_b=100; |
| 21 | + |
| 22 | +/** @function main */ |
| 23 | +int main() |
| 24 | +{ |
| 25 | + //! [mat] |
| 26 | + Mat frame, frame_threshold; |
| 27 | + //! [mat] |
| 28 | + //! [cap] |
| 29 | + VideoCapture cap(0); |
| 30 | + //! [cap] |
| 31 | + //! [window] |
| 32 | + namedWindow("Video Capture", WINDOW_NORMAL); |
| 33 | + namedWindow("Object Detection", WINDOW_NORMAL); |
| 34 | + //! [window] |
| 35 | + //! [trackbar] |
| 36 | + //-- Trackbars to set thresholds for RGB values |
| 37 | + createTrackbar("Low R","Object Detection", &low_r, 255, on_low_r_thresh_trackbar); |
| 38 | + createTrackbar("High R","Object Detection", &high_r, 255, on_high_r_thresh_trackbar); |
| 39 | + createTrackbar("Low G","Object Detection", &low_g, 255, on_low_g_thresh_trackbar); |
| 40 | + createTrackbar("High G","Object Detection", &high_g, 255, on_high_g_thresh_trackbar); |
| 41 | + createTrackbar("Low B","Object Detection", &low_b, 255, on_low_b_thresh_trackbar); |
| 42 | + createTrackbar("High B","Object Detection", &high_b, 255, on_high_b_thresh_trackbar); |
| 43 | + //! [trackbar] |
| 44 | + while(char(waitKey(1))!='q'){ |
| 45 | + //! [while] |
| 46 | + cap>>frame; |
| 47 | + if(frame.empty()) |
| 48 | + break; |
| 49 | + //-- Detect the object based on RGB Range Values |
| 50 | + inRange(frame,Scalar(low_b,low_g,low_r), Scalar(high_b,high_g,high_r),frame_threshold); |
| 51 | + //! [while] |
| 52 | + //! [show] |
| 53 | + //-- Show the frames |
| 54 | + imshow("Video Capture",frame); |
| 55 | + imshow("Object Detection",frame_threshold); |
| 56 | + //! [show] |
| 57 | + } |
| 58 | + return 0; |
| 59 | +} |
| 60 | +//! [low] |
| 61 | +/** @function on_low_r_thresh_trackbar */ |
| 62 | +void on_low_r_thresh_trackbar(int, void *) |
| 63 | +{ |
| 64 | + low_r = min(high_r-1, low_r); |
| 65 | + setTrackbarPos("Low R","Object Detection", low_r); |
| 66 | +} |
| 67 | +//! [low] |
| 68 | +//! [high] |
| 69 | +/** @function on_high_r_thresh_trackbar */ |
| 70 | +void on_high_r_thresh_trackbar(int, void *) |
| 71 | +{ |
| 72 | + high_r = max(high_r, low_r+1); |
| 73 | + setTrackbarPos("High R", "Object Detection", high_r); |
| 74 | +} |
| 75 | +//![high] |
| 76 | +/** @function on_low_g_thresh_trackbar */ |
| 77 | +void on_low_g_thresh_trackbar(int, void *) |
| 78 | +{ |
| 79 | + low_g = min(high_g-1, low_g); |
| 80 | + setTrackbarPos("Low G","Object Detection", low_g); |
| 81 | +} |
| 82 | + |
| 83 | +/** @function on_high_g_thresh_trackbar */ |
| 84 | +void on_high_g_thresh_trackbar(int, void *) |
| 85 | +{ |
| 86 | + high_g = max(high_g, low_g+1); |
| 87 | + setTrackbarPos("High G", "Object Detection", high_g); |
| 88 | +} |
| 89 | + |
| 90 | +/** @function on_low_b_thresh_trackbar */ |
| 91 | +void on_low_b_thresh_trackbar(int, void *) |
| 92 | +{ |
| 93 | + low_b= min(high_b-1, low_b); |
| 94 | + setTrackbarPos("Low B","Object Detection", low_b); |
| 95 | +} |
| 96 | + |
| 97 | +/** @function on_high_b_thresh_trackbar */ |
| 98 | +void on_high_b_thresh_trackbar(int, void *) |
| 99 | +{ |
| 100 | + high_b = max(high_b, low_b+1); |
| 101 | + setTrackbarPos("High B", "Object Detection", high_b); |
| 102 | +} |
0 commit comments