You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
10 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "opticalFlow.h"
  2. // filter parameters
  3. static const int FKERNELSIZE = 7; //7// for gaussian blur
  4. static const float FSTDDEV = 1.5;
  5. static const int FBLOCKSIZE = 51; //47// for adaptive threshold
  6. static const int FC = -5; //-5
  7. opticalFlowDetector::opticalFlowDetector(){
  8. m_iWidth = 500;
  9. m_iHeight = 0;
  10. m_dArea = 20.0;
  11. m_bPIP = false;
  12. type = VA_OPTIC_FLOW;
  13. m_oFilter = new filterImage(FKERNELSIZE, FSTDDEV, FBLOCKSIZE, FC);
  14. m_pDetectionCriteria = new TermCriteria( TermCriteria::COUNT | TermCriteria::EPS, 20, 0.03 );
  15. int tCount = gpu::getCudaEnabledDeviceCount();
  16. if (tCount){
  17. gpu::setDevice(0);
  18. };
  19. };
  20. opticalFlowDetector::opticalFlowDetector(int Width, int Height, double Area ){
  21. m_iWidth = Width;
  22. m_iHeight = Height;
  23. m_dArea = Area;
  24. m_bPIP = false;
  25. type = VA_OPTIC_FLOW;
  26. m_oFilter = new filterImage(FKERNELSIZE, FSTDDEV, FBLOCKSIZE, FC);
  27. m_pDetectionCriteria = new TermCriteria( TermCriteria::COUNT | TermCriteria::EPS, 20, 0.03 );
  28. };
  29. opticalFlowDetector::~opticalFlowDetector(){
  30. delete(m_pDetectionCriteria);
  31. delete(m_oFilter);
  32. };
  33. unsigned int root(unsigned int x){
  34. unsigned int a,b;
  35. b = x;
  36. a = x = 0x3f;
  37. x = b/x;
  38. a = x = (x+a)>>1;
  39. x = b/x;
  40. a = x = (x+a)>>1;
  41. x = b/x;
  42. x = (x+a)>>1;
  43. return(x);
  44. };
  45. bool opticalFlowDetector::m_fImageAnalyzer(Mat &Picture){
  46. if( Picture.empty() ) return false;
  47. m_oFilter->filter(Picture);
  48. if( m_oMat.empty() ){
  49. //goodFeaturesToTrack(InputArray image, OutputArray corners, int maxCorners, double qualityLevel, double minDistance, InputArray mask = noArray(), int blockSize = 3, bool useHarrisDetector = false, double k = 0.04)
  50. //goodFeaturesToTrack(m_oFilter->thresholdFrame(), m_vOldPOI, MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
  51. goodFeaturesToTrack(m_oFilter->gaussianFrame(), m_vOldPOI, MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
  52. //m_oFilter->thresholdFrame().copyTo(m_oMat);
  53. m_oFilter->gaussianFrame().copyTo(m_oMat);
  54. return false;
  55. };
  56. // automatic initialization
  57. //goodFeaturesToTrack( m_tMat, m_vCurrentPOI, MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04 );
  58. //goodFeaturesToTrack( m_oMat, m_vOldPOI, MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04 );
  59. if( !m_vOldPOI.empty() ){
  60. //cornerSubPix(m_oFilter->thresholdFrame(), m_vOldPOI, Size(10, 10), Size(-1, -1), *m_pDetectionCriteria);
  61. cornerSubPix(m_oFilter->gaussianFrame(), m_vOldPOI, Size(10, 10), Size(-1, -1), *m_pDetectionCriteria);
  62. } else {
  63. //goodFeaturesToTrack(m_oFilter->thresholdFrame(), m_vOldPOI, MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
  64. goodFeaturesToTrack(m_oFilter->gaussianFrame(), m_vOldPOI, MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
  65. //m_oFilter->thresholdFrame().copyTo(m_oMat);
  66. m_oFilter->gaussianFrame().copyTo(m_oMat);
  67. return false;
  68. };
  69. //m_nPyramidLevel = buildOpticalFlowPyramid(InputArray img, OutputArrayOfArrays pyramid, Size winSize, int maxLevel, bool withDerivatives=true, int pyrBorder=BORDER_REFLECT_101, int derivBorder=BORDER_CONSTANT, bool tryReuseInputImage=true)
  70. if( !m_vOldPOI.empty() ){
  71. //calcOpticalFlowPyrLK(m_oMat, m_oFilter->thresholdFrame(),
  72. calcOpticalFlowPyrLK(m_oMat, m_oFilter->gaussianFrame(),
  73. m_vOldPOI, m_vCurrentPOI,
  74. m_vStatus, m_vErr,
  75. Size( 21, 21), 3,
  76. *m_pDetectionCriteria,
  77. 0, 0.001 );
  78. // cleanup
  79. size_t i,k;
  80. for( i = k = 0; i < m_vCurrentPOI.size(); i++ ){
  81. if( !m_vStatus[i] ) continue;
  82. m_vCurrentPOI[k++] = m_vCurrentPOI[i];
  83. };
  84. m_vCurrentPOI.resize(k);
  85. m_vOldPOI.clear(); //m_vOldPOI.resize(k);
  86. m_vOldPOI = m_vCurrentPOI;
  87. //m_oFilter->thresholdFrame().copyTo(m_oMat);
  88. if (k < 50){
  89. //goodFeaturesToTrack(m_oFilter->thresholdFrame(), m_vOldPOI, MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
  90. goodFeaturesToTrack(m_oFilter->gaussianFrame(), m_vOldPOI, MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
  91. //m_oFilter->thresholdFrame().copyTo(m_oMat);
  92. m_oFilter->gaussianFrame().copyTo(m_oMat);
  93. return false;
  94. }
  95. return true;
  96. };
  97. return false;
  98. };
  99. /*
  100. Your selection
  101. Flights
  102. TLV - Tel Aviv - Ben Gurion, Israel
  103. Departs: Wed, 28 Sep 2016, 18:40
  104. TBS - Tbilisi - International, Georgia
  105. Arrives: Wed, 28 Sep 2016, 22:15
  106. TBS - Tbilisi - International, Georgia
  107. Departs: Fri, 14 Oct 2016, 07:25
  108. TLV - Tel Aviv - Ben Gurion, Israel
  109. Arrives: Fri, 14 Oct 2016, 09:10
  110. 1 ADULT (12+Yrs): USD 340.00
  111. Surcharges: USD 66.60
  112. Taxes: USD 50.73
  113. Total + Taxes + Surcharges: USD 457.33
  114. Total Price: USD 457.33
  115. */
  116. void opticalFlowDetector::markup( Mat &Picture ){
  117. size_t i;
  118. double pDiff;
  119. m_mBufferProtectorMutex.lock();
  120. for( i = 0; i < m_vCurrentPOI.size(); i++ ){
  121. if ( !m_vStatus[i] ) continue;
  122. if (i >= m_vOldPOI.size() ) break;
  123. pDiff = norm(m_vOldPOI[i] - m_vCurrentPOI[i] );
  124. if( pDiff < 3.0 || m_vErr[i] > 5 ) {
  125. circle( Picture, m_vCurrentPOI[i], 5, Scalar(0,0,255), 1, 8);
  126. } else {
  127. //line( Picture, m_vCurrentPOI[i], m_vOldPOI[i], Scalar( 0, 255, 255 ), 2 );
  128. //circle( Picture, m_vOldPOI[i], pDiff, Scalar(0,255,255), 2, 8);
  129. circle(Picture, m_vCurrentPOI[i], 5, Scalar(0, 255, 0), 1, 8);
  130. };
  131. };
  132. if (m_bPIP){
  133. Mat resizedSrc;
  134. Mat subView;
  135. Mat convertedSrc;
  136. if (!m_oFilter->filterFrame().empty()){
  137. resize(m_oFilter->filterFrame(), resizedSrc, Size(160, 120), 0.0, 0.0, INTER_AREA);
  138. subView = Picture(Rect((Picture.cols - 510), 50 + 120, 160, 120));
  139. cvtColor(resizedSrc, convertedSrc, CV_GRAY2BGR);
  140. convertedSrc.copyTo(subView);
  141. };
  142. if (!m_oFilter->gaussianFrame().empty()){
  143. resize(m_oFilter->gaussianFrame(), resizedSrc, Size(160, 120), 0.0, 0.0, INTER_AREA);
  144. subView = Picture(Rect((Picture.cols - 340), 50 + 120, 160, 120));
  145. cvtColor(resizedSrc, convertedSrc, CV_GRAY2BGR);
  146. convertedSrc.copyTo(subView);
  147. };
  148. if (!m_oFilter->thresholdFrame().empty()){
  149. resize(m_oFilter->thresholdFrame(), resizedSrc, Size(160, 120), 0.0, 0.0, INTER_AREA);
  150. subView = Picture(Rect((Picture.cols - 170), 50 + 120, 160, 120));
  151. cvtColor(resizedSrc, convertedSrc, CV_GRAY2BGR);
  152. convertedSrc.copyTo(subView);
  153. };
  154. };
  155. m_mBufferProtectorMutex.unlock();
  156. };
  157. /*
  158. #include <iostream>
  159. #include <ctype.h>
  160. using namespace cv;
  161. using namespace std;
  162. static void help()
  163. {
  164. // print a welcome message, and the OpenCV version
  165. cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
  166. "Using OpenCV version " << CV_VERSION << endl;
  167. cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
  168. cout << "\nHot keys: \n"
  169. "\tESC - quit the program\n"
  170. "\tr - auto-initialize tracking\n"
  171. "\tc - delete all the points\n"
  172. "\tn - switch the \"night\" mode on/off\n"
  173. "To add/remove a feature point click it\n" << endl;
  174. }
  175. Point2f point;
  176. bool addRemovePt = false;
  177. static void onMouse( int event, int x, int y, int / *flags* /, void* / *param* / )
  178. {
  179. if( event == EVENT_LBUTTONDOWN )
  180. {
  181. point = Point2f((float)x, (float)y);
  182. addRemovePt = true;
  183. }
  184. }
  185. int main( int argc, char** argv )
  186. {
  187. VideoCapture cap;
  188. TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03);
  189. Size subPixWinSize(10,10), winSize(31,31);
  190. const int MAX_COUNT = 500;
  191. bool needToInit = false;
  192. bool nightMode = false;
  193. cv::CommandLineParser parser(argc, argv, "{@input||}{help h||}");
  194. string input = parser.get<string>("@input");
  195. if (parser.has("help"))
  196. {
  197. help();
  198. return 0;
  199. }
  200. if( input.empty() )
  201. cap.open(0);
  202. else if( input.size() == 1 && isdigit(input[0]) )
  203. cap.open(input[0] - '0');
  204. else
  205. cap.open(input);
  206. if( !cap.isOpened() )
  207. {
  208. cout << "Could not initialize capturing...\n";
  209. return 0;
  210. }
  211. namedWindow( "LK Demo", 1 );
  212. setMouseCallback( "LK Demo", onMouse, 0 );
  213. Mat gray, prevGray, image, frame;
  214. vector<Point2f> points[2];
  215. for(;;)
  216. {
  217. cap >> frame;
  218. if( frame.empty() )
  219. break;
  220. frame.copyTo(image);
  221. cvtColor(image, gray, COLOR_BGR2GRAY);
  222. if( nightMode )
  223. image = Scalar::all(0);
  224. if( needToInit )
  225. {
  226. // automatic initialization
  227. goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
  228. cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
  229. addRemovePt = false;
  230. }
  231. else if( !points[0].empty() )
  232. {
  233. vector<uchar> status;
  234. vector<float> err;
  235. if(prevGray.empty())
  236. gray.copyTo(prevGray);
  237. calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
  238. 3, termcrit, 0, 0.001);
  239. size_t i, k;
  240. for( i = k = 0; i < points[1].size(); i++ )
  241. {
  242. if( addRemovePt )
  243. {
  244. if( norm(point - points[1][i]) <= 5 )
  245. {
  246. addRemovePt = false;
  247. continue;
  248. }
  249. }
  250. if( !status[i] )
  251. continue;
  252. points[1][k++] = points[1][i];
  253. circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
  254. }
  255. points[1].resize(k);
  256. }
  257. if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
  258. {
  259. vector<Point2f> tmp;
  260. tmp.push_back(point);
  261. cornerSubPix( gray, tmp, winSize, Size(-1,-1), termcrit);
  262. points[1].push_back(tmp[0]);
  263. addRemovePt = false;
  264. }
  265. needToInit = false;
  266. imshow("LK Demo", image);
  267. char c = (char)waitKey(10);
  268. if( c == 27 )
  269. break;
  270. switch( c )
  271. {
  272. case 'r':
  273. needToInit = true;
  274. break;
  275. case 'c':
  276. points[0].clear();
  277. points[1].clear();
  278. break;
  279. case 'n':
  280. nightMode = !nightMode;
  281. break;
  282. }
  283. std::swap(points[1], points[0]);
  284. cv::swap(prevGray, gray);
  285. }
  286. return 0;
  287. }
  288. */