segment_objects.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "opencv2/imgproc.hpp"
  2. #include "opencv2/videoio.hpp"
  3. #include "opencv2/highgui.hpp"
  4. #include "opencv2/video/background_segm.hpp"
  5. #include <stdio.h>
  6. #include <string>
  7. using namespace std;
  8. using namespace cv;
  9. static void help(char** argv)
  10. {
  11. printf("\n"
  12. "This program demonstrated a simple method of connected components clean up of background subtraction\n"
  13. "When the program starts, it begins learning the background.\n"
  14. "You can toggle background learning on and off by hitting the space bar.\n"
  15. "Call\n"
  16. "%s [video file, else it reads camera 0]\n\n", argv[0]);
  17. }
  18. static void refineSegments(const Mat& img, Mat& mask, Mat& dst)
  19. {
  20. int niters = 3;
  21. vector<vector<Point> > contours;
  22. vector<Vec4i> hierarchy;
  23. Mat temp;
  24. dilate(mask, temp, Mat(), Point(-1,-1), niters);
  25. erode(temp, temp, Mat(), Point(-1,-1), niters*2);
  26. dilate(temp, temp, Mat(), Point(-1,-1), niters);
  27. findContours( temp, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE );
  28. dst = Mat::zeros(img.size(), CV_8UC3);
  29. if( contours.size() == 0 )
  30. return;
  31. // iterate through all the top-level contours,
  32. // draw each connected component with its own random color
  33. int idx = 0, largestComp = 0;
  34. double maxArea = 0;
  35. for( ; idx >= 0; idx = hierarchy[idx][0] )
  36. {
  37. const vector<Point>& c = contours[idx];
  38. double area = fabs(contourArea(Mat(c)));
  39. if( area > maxArea )
  40. {
  41. maxArea = area;
  42. largestComp = idx;
  43. }
  44. }
  45. Scalar color( 0, 0, 255 );
  46. drawContours( dst, contours, largestComp, color, FILLED, LINE_8, hierarchy );
  47. }
  48. int main(int argc, char** argv)
  49. {
  50. VideoCapture cap;
  51. bool update_bg_model = true;
  52. CommandLineParser parser(argc, argv, "{help h||}{@input||}");
  53. if (parser.has("help"))
  54. {
  55. help(argv);
  56. return 0;
  57. }
  58. string input = parser.get<std::string>("@input");
  59. if (input.empty())
  60. cap.open(0);
  61. else
  62. cap.open(samples::findFileOrKeep(input));
  63. if( !cap.isOpened() )
  64. {
  65. printf("\nCan not open camera or video file\n");
  66. return -1;
  67. }
  68. Mat tmp_frame, bgmask, out_frame;
  69. cap >> tmp_frame;
  70. if(tmp_frame.empty())
  71. {
  72. printf("can not read data from the video source\n");
  73. return -1;
  74. }
  75. namedWindow("video", 1);
  76. namedWindow("segmented", 1);
  77. Ptr<BackgroundSubtractorMOG2> bgsubtractor=createBackgroundSubtractorMOG2();
  78. bgsubtractor->setVarThreshold(10);
  79. for(;;)
  80. {
  81. cap >> tmp_frame;
  82. if( tmp_frame.empty() )
  83. break;
  84. bgsubtractor->apply(tmp_frame, bgmask, update_bg_model ? -1 : 0);
  85. refineSegments(tmp_frame, bgmask, out_frame);
  86. imshow("video", tmp_frame);
  87. imshow("segmented", out_frame);
  88. char keycode = (char)waitKey(30);
  89. if( keycode == 27 )
  90. break;
  91. if( keycode == ' ' )
  92. {
  93. update_bg_model = !update_bg_model;
  94. printf("Learn background is in state = %d\n",update_bg_model);
  95. }
  96. }
  97. return 0;
  98. }