1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include <opencv2/opencv.hpp> #include <iostream>
using namespace cv; using namespace std; int main(int argc, char* argv[]) { if (argc != 2) { std::cout << "eg. " << argv[0] << " video" << std::endl; return 0; }
VideoCapture cap_bg(2); VideoCapture cap_show(argv[1]); if (!cap_show.isOpened()) { std::cout << "open video failed!" << std::endl; return 0; }
Point2f srcPoints[4]; Point2f dstPoints[4];
Mat frame_bg; Mat frame_show; Mat dst_warp; QRCodeDetector qrcodedetector; vector<Point> points; int i = 0; bool start_flag[2] = {false, false}; bool start_end[2] = {false, false}; while(1) { cap_bg >> frame_bg; if(frame_bg.empty()) break;
std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now(); bool has_code = qrcodedetector.detect(frame_bg, points); if (has_code) { cap_show >> frame_show; if (frame_show.empty()) continue;
start_flag[1] = true; bool tmp = start_flag[0]; start_flag[0] = start_flag[1]; start_flag[1] = tmp;
if (start_flag[0] == true && start_flag[1] == true) { srcPoints[0] = Point2f(0, 0); srcPoints[1] = Point2f(frame_show.cols, 0); srcPoints[2] = Point2f(0, frame_show.rows); srcPoints[3] = Point2f(frame_show.cols, frame_show.rows); dstPoints[0] = Point2f(points[0].x, points[0].y); dstPoints[1] = Point2f(points[1].x, points[1].y); dstPoints[2] = Point2f(points[3].x, points[3].y); dstPoints[3] = Point2f(points[2].x, points[2].y);
Mat M1 = getPerspectiveTransform(srcPoints, dstPoints); warpPerspective(frame_show, dst_warp, M1, frame_bg.size());
vector<cv::Point> point_area; point_area.push_back(points[0]); point_area.push_back(points[1]); point_area.push_back(points[2]); point_area.push_back(points[3]); vector<vector<Point>> point_areas; point_areas.push_back(point_area);
Mat mask(dst_warp.size(), CV_8UC1, Scalar(0)); polylines(mask, point_area, true, Scalar(255), 1); fillPoly(mask, point_areas, cv::Scalar(255)); dst_warp.copyTo(frame_bg, mask); } } else { start_flag[0] == false; start_flag[1] == false; } imshow("frame_bg_origin", frame_bg);
std::chrono::steady_clock::time_point end_time = std::chrono::steady_clock::now(); std::chrono::milliseconds cost_time = std::chrono::duration_cast<std::chrono::milliseconds>( end_time - start_time); int time_tmp = cost_time.count(); int delay = (time_tmp < 33) ? (33 - time_tmp) : 1; std::cout << "total time: " << time_tmp << "ms, " << "delay time: " << delay << "ms" << std::endl; if (waitKey(delay) == 'q') { break; } } return 0; }
|