Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. std::vector<std::vector<float>> BlockMatching::getOpticalFlowGrayBM(cv::Mat frame1, cv::Mat frame2, std::vector<cv::Point2f> sparsePoints, float thres)
  2. {
  3.     cv::Size blockSize(16, 16);
  4.     cv::Size searchSize(48, 48);
  5.     cv::Mat result;
  6.  
  7.     std::vector<std::vector<float>> opticalFlow;
  8.     cv::Mat scaledFrame1, scaledFrame2;
  9.     cv::cvtColor(frame1, scaledFrame1, CV_BGR2GRAY);
  10.     cv::cvtColor(frame2, scaledFrame2, CV_BGR2GRAY);
  11.     int width = frame1.cols;
  12.     int height = frame1.rows;
  13.     for (unsigned i = 0; i < sparsePoints.size(); i++) {
  14.         cv::Point2i upperBlock, lowerBlock;
  15.         upperBlock.x = std::max(0, (int)sparsePoints[i].x - blockSize.width / 2);
  16.         upperBlock.y = std::max(0, (int)sparsePoints[i].y - blockSize.height / 2);
  17.  
  18.         lowerBlock.x = std::min(width, (int)sparsePoints[i].x + blockSize.width / 2);
  19.         lowerBlock.y = std::min(height, (int)sparsePoints[i].y + blockSize.height / 2);
  20.         cv::Rect blockRect(upperBlock, lowerBlock);
  21.  
  22.         cv::Point2i upperSearch, lowerSearch;
  23.         upperSearch.x = std::max(0, (int)sparsePoints[i].x - searchSize.width / 2);
  24.         upperSearch.y = std::max(0, (int)sparsePoints[i].y - searchSize.height / 2);
  25.  
  26.         lowerSearch.x = std::min(width, (int)sparsePoints[i].x + searchSize.width / 2);
  27.         lowerSearch.y = std::min(height, (int)sparsePoints[i].y + searchSize.height / 2);
  28.         cv::Rect searchRect(upperSearch, lowerSearch);
  29.  
  30.         cv::Mat block, search;
  31.         scaledFrame1(blockRect).copyTo(block);
  32.         scaledFrame2(searchRect).copyTo(search);
  33.  
  34.         int resultCols = search.cols - block.cols + 1;
  35.         int resultRows = search.rows - block.rows + 1;
  36.  
  37.         result.create(resultRows, resultCols, CV_32FC1);
  38.         cv::matchTemplate(search, block, result, CV_TM_SQDIFF);
  39.         cv::normalize(result, result, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
  40.  
  41.         double minVal; double maxVal; cv::Point minLoc; cv::Point maxLoc;
  42.         cv::Point matchLoc;
  43.  
  44.         minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat());
  45.         matchLoc = minLoc;
  46.  
  47.         // std::cout << minVal << "," << minLoc << std::endl;
  48.  
  49.         if (minVal < 0.01) {
  50.         // if ((minVal > 0) && (minVal < thres)) {
  51.             std::vector<float> opticalFlowPair;
  52.             opticalFlowPair.resize(4);
  53.  
  54.             int u = matchLoc.x - (resultCols - 1) / 2;
  55.             int v = matchLoc.y - (resultRows - 1) / 2;
  56.  
  57.             opticalFlowPair[0] = sparsePoints[i].x + u;     // j
  58.             opticalFlowPair[1] = sparsePoints[i].y + v;     // i
  59.             opticalFlowPair[2] = u; // u
  60.             opticalFlowPair[3] = v; // v
  61.  
  62.             opticalFlow.push_back(opticalFlowPair);
  63.             //std::cout << u << "," << v << " : " << minVal << std::endl;
  64.             // cv::rectangle(frame2, cv::Point(opticalFlowPair[0] - block.cols / 2, opticalFlowPair[1] - block.rows / 2),
  65.             //  cv::Point(opticalFlowPair[0] + block.cols / 2, opticalFlowPair[1] + block.rows / 2), cv::Scalar(0,255,0), 2, 8, 0);
  66.             // cv::rectangle(frame2, cv::Point(sparsePoints[i].x - block.cols / 2, sparsePoints[i].y - block.rows / 2),
  67.             //  cv::Point(sparsePoints[i].x + block.cols / 2, sparsePoints[i].y + block.rows / 2), cv::Scalar(0,255,0), 2, 8, 0);
  68.             // cv::rectangle(frame2, upperBlock, lowerBlock, cv::Scalar(0,0,255), 2, 8, 0);
  69.             // cv::rectangle(frame2, upperSearch, lowerSearch, cv::Scalar(0,0,255), 1, 8, 0);
  70.         }
  71.     }
  72.  
  73.     cv::imshow("frame2", frame2);
  74.     // cv::waitKey(0);
  75.  
  76.     return opticalFlow;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement