实现目标
鼠标选中4个目标边界点,计算转换矩阵,对视频进行透视变换。
程序
#include<opencv2/opencv.hpp> #include<opencv2/highgui/highgui.hpp> #include<opencv2/core/core.hpp> #include<opencv2/imgproc/imgproc.hpp> #include <iostream> using namespace cv; using namespace std; void onMouse(int event, int x, int y, int flags, void *utsc); Point2f srcTri[4], dstTri[4]; int clickTimes = 0;//点击次数 Mat imageWarp; Mat frame; int main() { VideoCapture video("H:\\test.mp4"); if (!video.isOpened()) return -1; video.set(CV_CAP_PROP_FRAME_WIDTH, 1280); video.set(CV_CAP_PROP_FRAME_HEIGHT, 720); while (1) { int frameCount = video.get(CV_CAP_PROP_FRAME_COUNT);//获取帧数 double FPS = video.get(CV_CAP_PROP_FPS);//获取FPS for (int i = 0; i < frameCount; i++) { video.read(frame); if (frame.empty())//异常检测 { cout << "frame is empty!" << endl; break; } setMouseCallback("Source Image", onMouse); imshow("Source Image", frame); } } return 0; } void onMouse(int event, int x, int y, int flags, void *utsc) { if (event == CV_EVENT_LBUTTONUP)//响应鼠标左键抬起事件 { circle(frame, Point(x, y), 2.5, Scalar(0, 0, 255), 2.5);//标记选中点 imshow("Source Image", frame); srcTri[clickTimes].x = x; srcTri[clickTimes].y = y; cout << "x: " << x << " y: " << y << endl; clickTimes++; } if (clickTimes == 4) { //注意点的顺序:左上,右上,左下,右下 dstTri[0].x = 0; dstTri[0].y = 0; dstTri[1].x = frame.rows - 1; dstTri[1].y = 0; dstTri[2].x = 0; dstTri[2].y = frame.cols - 161; dstTri[3].x = frame.rows - 1; dstTri[3].y = frame.cols - 161; Mat transform = Mat::zeros(3, 3, CV_32FC1);//透视变换矩阵 transform = getPerspectiveTransform(srcTri, dstTri);//获取透视变换矩阵 warpPerspective(frame, imageWarp, transform, Size(frame.rows, frame.cols - 160));//透视变换 imshow("After WarpPerspecttive", imageWarp); } }