图像的透视变换(opencv2实现)
示例代码
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <cv.h>
using namespace std;
using namespace cv;
Point2f center(0,0);
Point2f computeIntersect(Vec4i a, Vec4i b)
{
int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3], x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];
// float denom;
if (float d = ((float)(x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4))) //若d=0,则直线AB与CD平行或重合
{
Point2f pt;
pt.x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d; //求两条直线的交点x坐标
pt.y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d; //求两条直线的交点x坐标
return pt;
}
else
return Point2f(-1, -1);
}
void sortCorners(std::vector<Point2f>& corners,Point2f center)
{
vector<Point2f> top, bot;
for (int i = 0; i < corners.size(); i++)
{
if (corners[i].y < center.y)
top.push_back(corners[i]);
else
bot.push_back(corners[i]);
}
corners.clear();
if (top.size() == 2 && bot.size() == 2){
Point2f tl = top[0].x > top[1].x ? top[1] : top[0];
Point2f tr = top[0].x > top[1].x ? top[0] : top[1];
Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0];
Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1];
corners.push_back(tl);
corners.push_back(tr);
corners.push_back(br);
corners.push_back(bl);
}
}
int main()
{
Mat srcImage = imread("E:\\图片\\image.jpg");//原图
if (srcImage.empty())
return -1;
Mat grayImage; //灰度图
cvtColor(srcImage, grayImage, CV_BGR2GRAY); //转化为灰度图
blur(grayImage, grayImage, Size(3, 3)); //滤波
Canny(grayImage, grayImage, 100, 100, 3);
vector<Vec4i> lines; //直线输出向量
HoughLinesP(grayImage, lines, 1, CV_PI/180, 70, 30, 10); //霍夫直线检测
// 延伸直线
for (int i = 0; i < lines.size(); i++)
{
Vec4i v = lines[i];
lines[i][0] = 0;
lines[i][1] = ((float)v[1] - v[3]) / (v[0] - v[2]) * -v[0] + v[1];
lines[i][2] = srcImage.cols;
lines[i][3] = ((float)v[1] - v[3]) / (v[0] - v[2]) * (srcImage.cols - v[2]) + v[3];
}
vector<Point2f> corners;
for (int i = 0; i < lines.size(); i++)
{
for (int j = i+1; j < lines.size(); j++)
{
Point2f pt = computeIntersect(lines[i], lines[j]);//调用自定义函数求相交直线的交点
if (pt.x >= 0 && pt.y >= 0)
corners.push_back(pt);
}
}
vector<Point2f> approx;
approxPolyDP(Mat(corners), approx, arcLength(Mat(corners), true) * 0.02, true);//approxPolyDP //arcLength计算轮廓周长或曲线长度
if (approx.size() != 4)
{
cout << "The object is not quadrilateral!" << std::endl;
return -1;
}
// Get mass center
for (int i = 0; i < corners.size(); i++)
center += corners[i];
center *= (1. / corners.size());
sortCorners(corners, center); //调用自定义函数
if (corners.size() == 0){
cout << "The corners were not sorted correctly!" << std::endl;
return -1;
}
Mat dst = srcImage.clone();
// Draw lines
for (int i = 0; i < lines.size(); i++)
{
Vec4i v = lines[i];
line(dst, Point(v[0], v[1]), Point(v[2], v[3]), CV_RGB(0,255,0));
}
// Draw corner points
circle(dst, corners[0], 3, CV_RGB(255,0,0), 2);
circle(dst, corners[1], 3, CV_RGB(0,255,0), 2);
circle(dst, corners[2], 3, CV_RGB(0,0,255), 2);
circle(dst, corners[3], 3, CV_RGB(255,255,255), 2);
// Draw mass center
circle(dst, center, 3, CV_RGB(255,255,0), 2);
Mat quad = Mat::zeros(300, 220, CV_8UC3);
vector<Point2f> quad_pts;
quad_pts.push_back(Point2f(0, 0));
quad_pts.push_back(Point2f(quad.cols, 0));
quad_pts.push_back(Point2f(quad.cols, quad.rows));
quad_pts.push_back(Point2f(0, quad.rows));
Mat transmtx = getPerspectiveTransform(corners, quad_pts); //getPerspectiveTransform由四对点计算透射变换
warpPerspective(srcImage, quad, transmtx, quad.size()); //warpPerspective对图像进行透视变换
imshow("image", dst);
imshow("quadrilateral", quad);
waitKey();
return 0;
}
运行结果