直接上代码,要注意有一个模板图片才行
#include <iostream> #include <opencv2\core\core.hpp> #include <opencv2\imgproc\imgproc.hpp> #include <opencv2\nonfree\features2d.hpp> #include <opencv2\highgui\highgui.hpp> #include <opencv2\calib3d\calib3d.hpp> using namespace cv; int _sift(Mat &img_object, Mat &img_scene); int main(int argc, char *argv[]) { //读取视频 VideoCapture vc; vc.open(0); double rate = vc.get(CV_CAP_PROP_FPS);//帧率 int delay = 1000 / rate; Mat img = imread("C:\\Users\\billlab\\Desktop\\template.png", CV_LOAD_IMAGE_COLOR);//模版图像 //待写入的视频 // VideoWriter vw; // vw.open("C:\\Users\\billlab\\Desktop\\sift.avi", // (int)vc.get(CV_CAP_PROP_FOURCC), // 也可设为CV_FOURCC_PROMPT,在运行时选取 // (double)vc.get(CV_CAP_PROP_FPS), // 视频帧率 // cv::Size((int)vc.get(CV_CAP_PROP_FRAME_WIDTH), (int)vc.get(CV_CAP_PROP_FRAME_HEIGHT)), // 视频大小 // true); // 是否输出彩色视频 namedWindow("1"); if (vc.isOpened()) { while (1) { Mat frame; //原始图像每5帧图像取1帧进行处理 for (int i = 0; i < 5; i++) { vc.read(frame); } if (frame.empty()) { break; } _sift(img, frame); imshow("1", frame); //vw << frame; waitKey(1); } } vc.release(); return 0; } int _sift(Mat &img_object, Mat &img_scene) { //Mat img_object = imread("1.jpg", CV_LOAD_IMAGE_COLOR); //Mat img_scene = imread("2.jpg", CV_LOAD_IMAGE_COLOR); double t = (double)getTickCount(); if (!img_object.data || !img_scene.data) { std::cout << "Error reading images!" << std::endl; return -1; } //检测SIFT特征点 int minHeassian = 400; SiftFeatureDetector detector(minHeassian); std::vector<KeyPoint> keypoints_object, keypoints_scene; detector.detect(img_object, keypoints_object); detector.detect(img_scene, keypoints_scene); //计算特征向量 SiftDescriptorExtractor extractor; Mat descriptors_object, descriptors_scene; extractor.compute(img_object, keypoints_object, descriptors_object); extractor.compute(img_scene, keypoints_scene, descriptors_scene); //利用FLANN匹配算法匹配特征描述向量 FlannBasedMatcher matcher; std::vector<DMatch> matches; matcher.match(descriptors_object, descriptors_scene, matches); double max_dist = 0; double min_dist = 100; //快速计算特征点之间的最大和最小距离 for (int i = 0; i < descriptors_object.rows; i++) { double dist = matches[i].distance; if (dist < min_dist) min_dist = dist; if (dist > max_dist) max_dist = dist; } printf("---Max dist: %f \n", max_dist); printf("---Min dist: %f \n", min_dist); //只画出好的匹配点(匹配特征点之间距离小于3*min_dist) std::vector<DMatch> good_matches; for (int i = 0; i < descriptors_object.rows; i++) { if (matches[i].distance < 3 * min_dist) good_matches.push_back(matches[i]); } Mat img_matches; drawMatches(img_object, keypoints_object, img_scene, keypoints_scene, good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); //定位物体/ std::vector<Point2f> obj; std::vector<Point2f> scene; for (int i = 0; i < good_matches.size(); i++) { //从好的匹配中获取特征点 obj.push_back(keypoints_object[good_matches[i].queryIdx].pt); scene.push_back(keypoints_scene[good_matches[i].trainIdx].pt); } //找出匹配特征点之间的变换 Mat H = findHomography(obj, scene, CV_RANSAC); //得到image_1的角点(需要寻找的物体) std::vector<Point2f> obj_corners(4); obj_corners[0] = cvPoint(0, 0); obj_corners[1] = cvPoint(img_object.cols, 0); obj_corners[2] = cvPoint(img_object.cols, img_object.rows); obj_corners[3] = cvPoint(0, img_object.rows); std::vector<Point2f> scene_corners(4); //匹配四个角点 perspectiveTransform(obj_corners, scene_corners, H); //画出匹配的物体两个匹配的图片 //line(img_matches, scene_corners[0] + Point2f(img_object.cols, 0), scene_corners[1] + Point2f(img_object.cols, 0), Scalar(0,255,0), 4); //line(img_matches, scene_corners[1] + Point2f(img_object.cols, 0), scene_corners[2] + Point2f(img_object.cols, 0), Scalar(0,255,0), 4); //line(img_matches, scene_corners[2] + Point2f(img_object.cols, 0), scene_corners[3] + Point2f(img_object.cols, 0), Scalar(0,255,0), 4); //line(img_matches, scene_corners[3] + Point2f(img_object.cols, 0), scene_corners[0] + Point2f(img_object.cols, 0), Scalar(0,255,0), 4); //匹配之后画出匹配图形的轮廓 line(img_scene, scene_corners[0], scene_corners[1], Scalar(0, 255, 0), 4); line(img_scene, scene_corners[1], scene_corners[2], Scalar(0, 255, 0), 4); line(img_scene, scene_corners[2], scene_corners[3], Scalar(0, 255, 0), 4); line(img_scene, scene_corners[3], scene_corners[0], Scalar(0, 255, 0), 4); //imshow("Good Matches & Object detection", img_matches); //imshow("识别图像", img_scene); t = 1000 * ((double)getTickCount() - t) / getTickFrequency(); std::cout << "the time is :" << t << std::endl; //waitKey(0); }
时间: 2024-12-22 11:48:58