本文中采用的SIFT提取视频中的和待检测图像特征点,并利用特征点之间的映射关系找到视频中待检测物体的位置,绘制出绿色的边界并显示出来。
检测视频
视频地址
图片:待检测的图片(仙剑奇侠传5的游戏盒子)
程序源代码
#include
#include
#include
#include
#include
#include
using namespace cv;
int _sift(Mat &img_object, Mat &img_scene);
int main(int argc, char *argv[])
{
//读取视频
VideoCapture vc;
vc.open("C:\\Users\\Arthur\\Desktop\\SRC\\video.mp4");
double rate = vc.get(CV_CAP_PROP_FPS);//帧率
int delay = 1000 / rate;
Mat img = imread("C:\\Users\\Arthur\\Desktop\\SRC\\temp.jpg", CV_LOAD_IMAGE_COLOR);//模版图像
VideoWriter vw;
vw.open("C:\\Users\\Arthur\\Desktop\\SRC\\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 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 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 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 (), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
//定位物体/
std::vector obj;
std::vector 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 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 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);
}
为了达到实时处理的效果,可以使用SURF替代SIFT,不过处理效果不如SIFT理想。
本文中的程序采用的是Opencv的2.4.8版本,在2.4.5版本以后可以结合VS2013和Image Watch插件,在调试的时候可以实时显示出图像,方便观测结果,再也不需要用各种imshow()来显示结果。
图片:Image Watch直接显示内存中图片信息
SIFT原理解析可以查看SIFT算法详解 这篇博文