Advertisement
dan-masek

Untitled

Jul 21st, 2017
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <opencv2/opencv.hpp>
  2. #include <opencv2/highgui/highgui.hpp>
  3.  
  4. // NB: Don't use this, see https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
  5. // using namespace std;
  6. // using namespace cv;
  7.  
  8. void process_contour(cv::Mat& frame, std::vector<cv::Point> const& contour)
  9. {
  10.     cv::Scalar TRIANGLE_COLOR(255, 0, 0);
  11.     cv::Scalar QUADRILATERAL_COLOR(0, 255, 0);
  12.     cv::Scalar HEPTAGON_COLOR(0, 0, 255);
  13.  
  14.     cv::Scalar colour;
  15.     if (contour.size() == 3) {
  16.         colour = TRIANGLE_COLOR;
  17.     } else if (contour.size() == 4) {
  18.         colour = QUADRILATERAL_COLOR;
  19.     } else if (contour.size() == 7) {
  20.         colour = HEPTAGON_COLOR;
  21.     } else {
  22.         return;
  23.     }
  24.  
  25.     cv::Point const* points(&contour[0]);
  26.     int n_points(static_cast<int>(contour.size()));
  27.  
  28.     polylines(frame, &points, &n_points, 1, true, colour, 4);
  29. }
  30.  
  31. void process_frame(cv::Mat const& frame, cv::Mat& result_frame)
  32. {
  33.     cv::Mat feedGrayScale;
  34.     cv::cvtColor(frame, feedGrayScale, cv::COLOR_BGR2GRAY);
  35.  
  36.     frame.copyTo(result_frame);
  37.  
  38.     //thresholding the grayscale image to get better results
  39.     cv::threshold(feedGrayScale, feedGrayScale, 128, 255, cv::THRESH_BINARY);
  40.  
  41.     std::vector<std::vector<cv::Point> > contours;
  42.     std::vector<cv::Vec4i> hierarchy;
  43.     cv::findContours(feedGrayScale, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
  44.     for (size_t k(0); k < contours.size(); ++k) {
  45.         std::vector<cv::Point> approx_contour;
  46.         cv::approxPolyDP(cv::Mat(contours[k]), approx_contour, 3, true);
  47.         process_contour(result_frame, approx_contour);
  48.     }
  49. }
  50.  
  51. int main()
  52. {
  53.     cv::VideoCapture cap(0); // open the video camera no. 0
  54.  
  55.     if (!cap.isOpened())  // if not success, exit program
  56.     {
  57.         std::cout << "Cannot open the video cam\n";
  58.         return -1;
  59.     }
  60.        
  61.     cv::namedWindow("Original", CV_WINDOW_AUTOSIZE);
  62.     cv::namedWindow("Tracked", CV_WINDOW_AUTOSIZE);
  63.  
  64.     // Process frames from the video stream...
  65.     for(;;) {
  66.         cv::Mat frame, result_frame;
  67.  
  68.         // read a new frame from video
  69.         if (!cap.read(frame)) {
  70.             std::cout << "Cannot read a frame from video stream\n";
  71.             break;
  72.         }
  73.  
  74.         process_frame(frame, result_frame);
  75.  
  76.         cv::imshow("Original", frame);
  77.         cv::imshow("Tracked", result_frame);
  78.         if (cv::waitKey(20) == 27) { // Quit on ESC
  79.             break;
  80.         }
  81.     }
  82.  
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement