Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include "opencv2/highgui/highgui.hpp"
  3. #include "opencv2/imgproc/imgproc.hpp"
  4.  
  5. using namespace cv;
  6. using namespace std;
  7.  
  8. int returnBiggest(vector<vector<Point>> counters);
  9.  
  10. int main(int argc, char** argv)
  11. {
  12. VideoCapture cap(0);
  13. if (!cap.isOpened()) { return -1; }
  14. namedWindow("frame", 1);
  15. Mat frame, newFrame;
  16. vector<vector<Point>> contours;
  17. vector<Vec4i> hierarchy;
  18. while (true)
  19. {
  20. cap >> frame;
  21. flip(frame, frame, 1);
  22. imshow("frame", frame);
  23.  
  24. cvtColor(frame, newFrame, CV_RGB2HSV);
  25. inRange(newFrame, Scalar(40, 45, 90), Scalar(85, 100, 160), newFrame);
  26. findContours(newFrame, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE, Point(0, 0));
  27. imshow("newFrame", newFrame);
  28.  
  29. // contours.at(returnBiggest(contours));
  30.  
  31. if (waitKey(30) == 'q') { break; }
  32. }
  33. return 0;
  34. }
  35.  
  36. int returnBiggest(vector<vector<Point>> counters)
  37. {
  38. int largest_area = 0;
  39. int largest_contour_index = 0;
  40. int i = 0;
  41. for (i = 0; i < counters.size(); i++) // iterate through each contour.
  42. {
  43. double a = contourArea(counters[i], false); // Find the area of contour
  44. if (a > largest_area)
  45. {
  46. largest_area = a;
  47. largest_contour_index = i; //Store the index of largest contour
  48. }
  49. }
  50. return largest_contour_index;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement