前言
在日常生活中,我们接触的照片经常会因为角度或者方向的问题,而导致图像中的文字倾斜或者角度偏转。透视变换(Perspective Transformation)可以将图片进行校正。也可以通过透视变换进行图像的平面识别;
函数
- findHomography()
------>发现两个平面的透视变幻,生成透视变换矩阵。计算多个二维点对之间的最优单映射变换矩阵 H(3行x3列) ,使用最小均方误差或者RANSAC方法
Mat cv::findHomography ( InputArray srcPoints,
InputArray dstPoints,
int method = 0,
double ransacReprojThreshold = 3,
OutputArray mask = noArray(),
const int maxIters = 2000,
const double confidence = 0.995
)
参数详解:
srcPoints | 源平面中点的坐标矩阵,可以是CV_32FC2类型,也可以是vector类型 |
---|---|
dstPoints | 目标平面中点的坐标矩阵,可以是CV_32FC2类型,也可以是vector类型 |
method | 计算单应矩阵所使用的方法。不同的方法对应不同的参数,具体如下: |
- 0 - 利用所有点的常规方法
- RANSAC - RANSAC-基于RANSAC的鲁棒算法
- LMEDS - 最小中值鲁棒算法
- RHO - PROSAC-基于PROSAC的鲁棒算法
ransacReprojThreshold | 将点对视为内点的最大允许重投影错误阈值(仅用于RANSAC和RHO方法)。通常在1~10的范围内 |
---|---|
mask | 可选输出掩码矩阵,通常由鲁棒算法(RANSAC或LMEDS)设置。 请注意,输入掩码矩阵是不需要设置的。 |
maxIters | RANSAC算法的最大迭代次数,默认值为2000。 |
confidence | 可信度值,取值范围为0到1. |
- perspectiveTransform()
------------>执行矢量的透视矩阵变换。
void perspectiveTransform(
InputArray src, //输入双通道或三通道浮点数组/图像
OutputArray dst, //输出与src相同大小和类型的数组/图像
InputArray m //3x3或4x4浮点转换矩阵
);
平面识别
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
int main(int argc, char** argv) {
Mat img1 = imread("E:/tuku/nihuai1.jpg", IMREAD_GRAYSCALE);
Mat img2 = imread("E:/tuku/nihuai2.jpg", IMREAD_GRAYSCALE);
if (!img1.data || !img2.data) {
return -1;
}
imshow("object image", img1);
imshow("object in scene", img2);
//-----------------FLANN特征检测--------------
//surf featurs extraction
int minHessian = 400;
Ptr<SURF> detector = SURF::create(minHessian);
vector<KeyPoint> keypoints_obj;
vector<KeyPoint> keypoints_scene;
Mat descriptor_obj, descriptor_scene;
detector->detectAndCompute(img1, Mat(), keypoints_obj, descriptor_obj);
detector->detectAndCompute(img2, Mat(), keypoints_scene, descriptor_scene);
// matching
FlannBasedMatcher matcher;
vector<DMatch> matches;
matcher.match(descriptor_obj, descriptor_scene, matches);
// find good matched points
double minDist = 1000;
double maxDist = 0;
for (int i = 0; i < descriptor_obj.rows; i++) {
double dist = matches[i].distance;
if (dist > maxDist) {
maxDist = dist;
}
if (dist < minDist) {
minDist = dist;
}
}
printf("max distance : %f\n", maxDist);
printf("min distance : %f\n", minDist);
vector<DMatch> goodMatches;
for (int i = 0; i < descriptor_obj.rows; i++) {
double dist = matches[i].distance;
if (dist < max(3 * minDist, 0.02)) {
goodMatches.push_back(matches[i]);
//push_back c++中函数在vector类中作用为在vector尾部加入一个数据
//在string中作用是字符串之后插入一个字符。
}
}
Mat matchesImg;
drawMatches(img1, keypoints_obj, img2, keypoints_scene, goodMatches, matchesImg, Scalar::all(-1),
Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS
);
//----------------平面识别---------------------------
vector<Point2f> obj;
vector<Point2f> objInScene;
for (size_t t = 0; t < goodMatches.size(); t++) {
obj.push_back(keypoints_obj[goodMatches[t].queryIdx].pt);
objInScene.push_back(keypoints_scene[goodMatches[t].trainIdx].pt);
// queryIdx:是测试图像(源图像)的特征点描述符(descriptor)的下标,同时也是描述符对应特征点(keypoint)的下标。
//trainIdx:是样本图像(目标图像)的特征点描述符的下标,同样也是相应的特征点的下标。
//DMathch类型中queryIdx是指match中第一个数组的索引,keyPoint类型中pt指的是当前点坐标
}
Mat H = findHomography(obj, objInScene, RANSAC);
vector<Point2f> obj_corners(4);
vector<Point2f> scene_corners(4);
obj_corners[0] = Point(0, 0);
obj_corners[1] = Point(img1.cols, 0);
obj_corners[2] = Point(img1.cols, img1.rows);
obj_corners[3] = Point(0, img1.rows);
perspectiveTransform(obj_corners, scene_corners, H);
// draw line
line(matchesImg, scene_corners[0] + Point2f(img1.cols, 0), scene_corners[1] + Point2f(img1.cols, 0), Scalar(0, 0, 255), 2, 8, 0);
line(matchesImg, scene_corners[1] + Point2f(img1.cols, 0), scene_corners[2] + Point2f(img1.cols, 0), Scalar(0, 0, 255), 2, 8, 0);
line(matchesImg, scene_corners[2] + Point2f(img1.cols, 0), scene_corners[3] + Point2f(img1.cols, 0), Scalar(0, 0, 255), 2, 8, 0);
line(matchesImg, scene_corners[3] + Point2f(img1.cols, 0), scene_corners[0] + Point2f(img1.cols, 0), Scalar(0, 0, 255), 2, 8, 0);
Mat dst;
cvtColor(img2, dst, COLOR_GRAY2BGR);
line(dst, scene_corners[0], scene_corners[1], Scalar(0, 0, 255), 2, 8, 0);
line(dst, scene_corners[1], scene_corners[2], Scalar(0, 0, 255), 2, 8, 0);
line(dst, scene_corners[2], scene_corners[3], Scalar(0, 0, 255), 2, 8, 0);
line(dst, scene_corners[3], scene_corners[0], Scalar(0, 0, 255), 2, 8, 0);
imshow("find known object demo", matchesImg);
imshow("Draw object", dst);
waitKey(0);
return 0;
}
效果图
来源:https://blog.csdn.net/CSDNjpl/article/details/99547962