Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. /*
  2. https://docs.opencv.org/3.0-beta/modules/videoio/doc/reading_and_writing_video.html
  3. */
  4.  
  5. #include "stdafx.h"
  6. /*
  7. https://docs.opencv.org/4.0.0/dd/de7/group__videoio.html
  8. */
  9.  
  10. #include <opencv2/opencv.hpp>
  11. #include <iostream>
  12. #include "opencv2/highgui/highgui.hpp"
  13. #include "opencv2/imgproc/imgproc.hpp"
  14. #include <time.h>
  15. using namespace cv;
  16. using namespace std;
  17.  
  18. double alpha; /**< Simple contrast control */
  19. int beta; /**< Simple brightness control */
  20.  
  21. Mat3b getMean(const vector<Mat3b>& images);
  22.  
  23. int main()
  24. {
  25. VideoCapture cap(0);
  26. if (!cap.isOpened()) {
  27. return -1;
  28. }
  29. Size videoSize = Size((int)cap.get(CAP_PROP_FRAME_WIDTH), (int)cap.get(CAP_PROP_FRAME_HEIGHT));
  30. namedWindow("Recording", WINDOW_NORMAL);
  31.  
  32. time_t ori;
  33. ori = time(NULL);
  34.  
  35. Mat mean_10sec;
  36. Mat diffFrame;
  37. Mat blurFrame;
  38. Mat threFrame;
  39. vector<Mat3b> images;
  40. while (true) {
  41. time_t cur;
  42. cur = time(NULL);
  43. Mat frame;
  44. cap >> frame;
  45. int key = waitKey(1);
  46. if (!frame.empty()) {
  47.  
  48. images.push_back(frame);
  49. cout << "size: " << images.size() << endl;
  50. if (cur - ori == 1) {
  51. Mat3b meanImage_10sec = getMean(images);
  52. mean_10sec = meanImage_10sec;
  53. imshow("10sec mean", mean_10sec);
  54. }
  55. if (cur - ori > 1) {
  56. //absdiff(frame, mean_10sec, diffFrame);
  57. diffFrame = mean_10sec - frame;
  58. imshow("Diff", diffFrame);
  59. medianBlur(diffFrame, blurFrame, 5);
  60. imshow("Blur", blurFrame);
  61. cvtColor(blurFrame, blurFrame, COLOR_BGR2GRAY);
  62. threshold(blurFrame, threFrame, 150, 255, THRESH_OTSU);
  63. imshow("thre", threFrame);
  64. }
  65. if (key == 27) {
  66. cout << "close" << endl;
  67. break;
  68. }
  69. imshow("Recording", frame);
  70. }
  71. }
  72.  
  73.  
  74. return 0;
  75. }
  76.  
  77. Mat3b getMean(const vector<Mat3b>& images)
  78. {
  79. if (images.empty()) return Mat3b();
  80.  
  81. // Create a 0 initialized image to use as accumulator
  82. Mat m(images[0].rows, images[0].cols, CV_64FC3);
  83. m.setTo(Scalar(0, 0, 0, 0));
  84.  
  85. // Use a temp image to hold the conversion of each input image to CV_64FC3
  86. // This will be allocated just the first time, since all your images have
  87. // the same size.
  88. Mat temp;
  89. for (int i = 0; i < images.size(); ++i)
  90. {
  91. // Convert the input images to CV_64FC3 ...
  92. images[i].convertTo(temp, CV_64FC3);
  93.  
  94. // ... so you can accumulate
  95. m += temp;
  96. }
  97.  
  98. // Convert back to CV_8UC3 type, applying the division to get the actual mean
  99. m.convertTo(m, CV_8U, 1. / images.size());
  100. return m;
  101. }
  102.  
  103. //y2fkvhk8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement