1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/opencv.hpp> using namespace cv; using namespace std;
Mat frame, frame_thresholded; int rgb_slider = 0, low_slider = 30, high_slider = 100; int low_r = 30, low_g = 30, low_b = 30, high_r = 100, high_g = 100, high_b = 100;
void on_rgb_trackbar(int, void *) { switch (rgb_slider) { case 0: setTrackbarPos("Low threshold", "Segmentation", low_r); setTrackbarPos("High threshold", "Segmentation", high_r); break; case 1: setTrackbarPos("Low threshold", "Segmentation", low_g); setTrackbarPos("High threshold", "Segmentation", high_g); break; case 2: setTrackbarPos("Low threshold", "Segmentation", low_b); setTrackbarPos("High threshold", "Segmentation", high_b); break; } }
void on_low_thresh_trackbar(int, void *) { switch (rgb_slider) { case 0: low_r = min(high_slider - 1, low_slider); setTrackbarPos("Low threshold", "Segmentation", low_r); break; case 1: low_g = min(high_slider - 1, low_slider); setTrackbarPos("Low threshold", "Segmentation", low_g); break; case 2: low_b = min(high_slider - 1, low_slider); setTrackbarPos("Low threshold", "Segmentation", low_b); break; } }
void on_high_thresh_trackbar(int, void *) { switch (rgb_slider) { case 0: high_r = max(low_slider + 1, high_slider); setTrackbarPos("High threshold", "Segmentation", high_r); break; case 1: high_g = max(low_slider + 1, high_slider); setTrackbarPos("High threshold", "Segmentation", high_g); break; case 2: high_b = max(low_slider + 1, high_slider); setTrackbarPos("High threshold", "Segmentation", high_b); break; } }
int main() { VideoCapture cap(0);
if (!cap.isOpened()) { cout << "Capture could not be opened succesfully" << endl; return -1; }
namedWindow("Video"); namedWindow("Segmentation");
createTrackbar("0. R\n1. G\n2.B", "Segmentation", &rgb_slider, 2, on_rgb_trackbar); createTrackbar("Low threshold", "Segmentation", &low_slider, 255, on_low_thresh_trackbar); createTrackbar("High threshold", "Segmentation", &high_slider, 255, on_high_thresh_trackbar);
double time = 0; unsigned int frames = 0; while (char(waitKey(1)) != 'q' && cap.isOpened()) { frames++; double t0 = getTickCount(); cap >> frame; if (frame.empty()) { cout << "Video over" << endl; break; }
inRange(frame, Scalar(low_b, low_g, low_r), Scalar(high_b, high_g, high_r), frame_thresholded);
imshow("Video", frame); imshow("Segmentation", frame_thresholded); time += (getTickCount() - t0) / getTickFrequency(); cout << frames / time << " fps" << endl; }
return 0; }
|