Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <filesystem>
  4. #include <opencv2/world.hpp>
  5. #include <opencv2/highgui/highgui.hpp>
  6. #include <opencv2/imgproc/imgproc.hpp>
  7. #include <PixelClassifier.h>
  8. #include <opencv2/flann/miniflann.hpp>
  9. #include <opencv2/features2d.hpp>
  10. #include <opencv2/video.hpp>
  11. #include <Utils.h>
  12. using namespace cv;
  13. using namespace std;
  14.  
  15. PixelClassifier classifier;
  16.  
  17. Mat overlay(Mat back, Mat mask) {
  18. Mat result = mask.clone();
  19. cvtColor(result,result, CV_GRAY2BGR);
  20. cvtColor(result,result, CV_BGR2HSV);
  21.  
  22. for (auto j = 0; j < result.rows; ++j) {
  23. for (auto i = 0; i < result.cols; ++i) {
  24. auto & pix = result.at<Vec3b>(j,i);
  25. if(pix.val[2] > 200) {
  26. pix.val[1] = 255;
  27. pix.val[0] = 125;
  28. }
  29. }
  30. }
  31. cvtColor(result,result, CV_HSV2BGR);
  32. addWeighted(back, 0.9, result, 0.9, 0.0, result);
  33. return result;
  34. }
  35.  
  36. Mat overlay(Mat back, Mat mask, Mat mask2) {
  37. Mat result = mask.clone();
  38. cvtColor(result,result, CV_GRAY2BGR);
  39. cvtColor(result,result, CV_BGR2HSV);
  40.  
  41. for (auto j = 0; j < result.rows; ++j) {
  42. for (auto i = 0; i < result.cols; ++i) {
  43. auto & pix = result.at<Vec3b>(j,i);
  44. if(mask2.at<uchar>(j,i) > 200) {
  45. pix.val[1] = 255;
  46. pix.val[0] = 185;
  47. } else if(pix.val[2] > 200) {
  48. pix.val[1] = 255;
  49. pix.val[0] = 125;
  50. }
  51. }
  52. }
  53. cvtColor(result,result, CV_HSV2BGR);
  54. addWeighted(back, 0.9, result, 0.9, 0.0, result);
  55. return result;
  56. }
  57.  
  58.  
  59. float alpha = 2;
  60. float beta = 1;
  61.  
  62. void pumpContrast(Mat & image) {
  63. for( int y = 0; y < image.rows; y++ ) {
  64. for( int x = 0; x < image.cols; x++ ) {
  65. for( int c = 0; c < 3; c++ ) {
  66. image.at<Vec3b>(y,x)[c] =
  67. saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
  68. }
  69. }
  70. }
  71. }
  72.  
  73. int main (){
  74. auto back = imread("./back.png", CV_LOAD_IMAGE_ANYCOLOR);
  75. classifier.SetBack(back);
  76. auto size = Size(10,10);
  77.  
  78. blur( back, back, size);
  79. back = classifier.equalizeIntensity(back);
  80.  
  81. auto t1 = imread("./out00441.png", CV_LOAD_IMAGE_ANYCOLOR);
  82. auto t2 = imread("./out00442.png", CV_LOAD_IMAGE_ANYCOLOR);
  83. auto bname = "bgs";
  84. namedWindow( bname, CV_WINDOW_FREERATIO );
  85. resizeWindow(bname, 700, 600);
  86. Mat fgm, current, bg;
  87. {
  88. ScopedTimer t("bgs");
  89. auto bs = createBackgroundSubtractorMOG2(300, 10, true);
  90.  
  91.  
  92. string dir = "../video";
  93. int counter = 0;
  94. tr2::sys::directory_iterator it(dir), end;
  95. for (; it!= end; ++it) {
  96. if(++counter == 1) {
  97. bs->apply(back, fgm, 0.9);
  98. }
  99.  
  100. auto path = dir + "/" + it->path();
  101. ScopedTimer tt(path);
  102. current = imread(path, CV_LOAD_IMAGE_ANYCOLOR);
  103. blur( current, current, size);
  104. current = classifier.equalizeIntensity(current);
  105.  
  106. bs->apply(current, fgm, 0.01);
  107. blur( fgm, fgm, size);
  108. threshold(fgm, fgm, 220, 255, CV_THRESH_TOZERO);
  109. auto blobs = classifier.findBigBlobs(fgm,100);
  110. imshow(bname, overlay(current, fgm, blobs));
  111. waitKey(1);
  112. }
  113. bs->getBackgroundImage(bg);
  114. }
  115. cout << "fc" << fgm.channels() << " " << current.channels() << endl;
  116. imshow("bg", bg);
  117.  
  118. auto b1 = classifier.Detect(t1);
  119. auto b2 = classifier.Detect(t2);
  120.  
  121. Mat result;
  122. hconcat(overlay(t1, b1), overlay(t2, b2), result);
  123. auto name = "Tracking Test";
  124. namedWindow( name, CV_WINDOW_FREERATIO );
  125. resizeWindow(name, 700, 600);
  126.  
  127. //imshow(name, result);
  128. auto aka = AKAZE::create();
  129. FlannBasedMatcher matcher(new cv::flann::LshIndexParams(5, 24, 2));
  130.  
  131. vector<KeyPoint> k1, k2;
  132. Mat d1, d2;
  133. vector< vector<DMatch> > nn_matches;
  134.  
  135. {
  136. ScopedTimer t("features");
  137. aka->detectAndCompute(t1, b1, k1, d1);
  138. aka->detectAndCompute(t2, b2, k2, d2);
  139. }
  140.  
  141. {
  142. ScopedTimer t("matches");
  143. matcher.knnMatch(d1, d2, nn_matches, 2);
  144. }
  145.  
  146.  
  147. Mat res;
  148. drawMatches(t1, k1, t2, k2, nn_matches, res);
  149. addWeighted(result, 0.9, res, 0.9, 0.0, result);
  150.  
  151. imshow(name, result);
  152.  
  153. waitKey();
  154. cin.get();
  155. return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement