由于要研究图像的监测相关知识,所以考虑利用相关知识实现图像内部物体的监测。C++代码如下:
#include <stdio.h>
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/features2d.hpp"
using namespace cv;
int main(int argc, char** argv);
void readme();
/* @function main */
int main(int argc, char** argv)
{
Mat img_object = imread("F:\\mo.jpg", IMREAD_GRAYSCALE); //模板
Mat img_scene = imread("F:\\moss.jpg", IMREAD_GRAYSCALE); //场景
//resize的目的是因为是利用手机摄像头取得模板,导致图片的像素过大,为了可以显示
//如果想要调整图片的像素,可以去windows自带的画图工具调整(这样可以加快匹配的速度)
resize(img_object, img_object, Size(400,400));
resize(img_scene, img_scene, Size(800,600));
if (!img_object.data || !img_scene.data)
{
std::cout << " --(!) Error reading images " << std::endl;
return -1;
}
Ptr<ORB> detector = ORB::create();
std::vector<KeyPoint> keypoints_object, keypoints_scene;
Mat descriptors_object, descriptors_scene;
detector->detectAndCompute(img_object, Mat(), keypoints_object, descriptors_object);
detector->detectAndCompute(img_scene, Mat(), keypoints_scene, descriptors_scene);
BFMatcher 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);
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),
std::vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
std::vector<Point2f> obj;
std::vector<Point2f> scene;
for (size_t i = 0; i < good_matches.size(); i++)
{
//-- Get the keypoints from the good matches
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, RANSAC);
if (H.empty())
//因为是特征点随机监测,如果特征点不丰富或者角度有问题,会出现Homography为空的现象
{
std::cout << "yesssssssssssss" << std::endl;
std::cout << H.type() << std::endl;
}
std::vector<cv::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<cv::Point2f> scene_corners(4);
perspectiveTransform(obj_corners, scene_corners, H);
std::cout << scene_corners[0] << std::endl;
std::cout << scene_corners[1] << std::endl;
std::cout << scene_corners[2] << std::endl;
std::cout << scene_corners[3] << std::endl;
std::cout << Point2f(img_object.cols, 0) << std::endl;
//这里因为需要知道中间的计算结果,所以讲匹配图和最后需要的结果图都显示出来,关闭掉第一个以后,打开第二张图
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);
waitKey(0);
imshow("Good Matches & Object detection", img_scene);
waitKey(0);
return 0;
}
这个算法是依据特征点匹配来实现的,带来的问题有:
(1)在计算homography时,可能会出现为空的状况;
(2)需要尽量选择特征点比较丰富的图片作为模板,否则会出现画出的线不匹配的状况。
匹配的中间结果截图:
最后算法结果: