学更好的别人,
做更好的自己。
——《微卡智享》
本文长度为3879字,预计阅读9分钟
前言
以前的文章《C++ OpenCV之透视变换》介绍过透视变换,当时主要是自己固定的变换坐标点,所以在想可不可以做一个通过轮廓检测后自适应的透视变换,实现的思路通过检测主体的轮廓,使用外接矩形和多边形拟合的四个最边的点进行透视变换。
实现效果
# | 实现思路 |
---|---|
1 | 图像灰度图,高斯滤波、二值化 |
2 | 形态学开操作,Canny边缘检测 |
3 | 查找轮廓,遍历轮廓判断周长大于图像宽度的进行多边形拟合 |
4 |
判断拟合的点大于4个的获取到最小旋转矩形 |
5 |
通过多边形拟合的点计算出离最小旋转矩形最近的4个点 |
6 |
找到轮廓最小外接矩形作为透视变换的坐标 |
7 |
将5、6的步骤两个坐标点计算透视变换矩阵 |
8 |
透视变换 |
重点说明
微卡智享
01
排序旋转矩形的坐标点
图片来自网络
获取旋转矩形的函数minAreaRect( )中,四个顶点中y值最大的顶点为p[0],p[0]围着center顺时针旋转,依次经过的顶点为p[1],p[2],p[3]。角度参数angle 是P[0]发出的平行于x轴的射线,逆时针旋转,与碰到的第一个边的夹角,取值范围[-90~0]。注:逆时针旋转角度为负。
在透视变换的4个顶点的顺序为左上,右上,右下,左下,所以根据上面的原理,我们要写一个4点的重新排序,把4个顶点的顺序按透视变换的需要修改过来。
//重新排序旋转矩形坐标点
void SortRotatedRectPoints(Point2f vetPoints[], RotatedRect rect)
{
rect.points(vetPoints);
cout << vetPoints[0] << vetPoints[1] << vetPoints[2] << vetPoints[3] << endl;
cout << rect.angle << endl;
Point2f curpoint;
//根据Rect的坐标点,Y轴最大的为P[0],p[0]围着center顺时针旋转,
//旋转角度为负的话即是P[0]在左下角,为正P[0]是右下角
//重新排序坐标点
if (rect.angle > 0) {
curpoint = vetPoints[0];
vetPoints[0] = vetPoints[2];
vetPoints[2] = curpoint;
curpoint = vetPoints[1];
vetPoints[1] = vetPoints[3];
vetPoints[3] = curpoint;
}
else if (rect.angle < 0) {
curpoint = vetPoints[0];
vetPoints[0] = vetPoints[1];
vetPoints[1] = vetPoints[2];
vetPoints[2] = vetPoints[3];
vetPoints[3] = curpoint;
}
}
02
计算多边形拟合需要透视变换的点
通过多边形拟合出来的点比较多,而使用透视变换也是只要4个点,如果使用最小旋转矩形的4个点没有什么效果,如上图中红色是多边形拟合的点,蓝色框为最小旋转矩形的点,如果用这个点无法实现透视变换的效果,所以通过遍历了多边形拟合的点,计算每个点到最小旋转矩形的距离最近的4个点,形成了上图中的白色框,虽然不完美,但是可以透视变换的效果。
距离的计算用的是欧几里德距离,然后对比找到最近的4个点。
//根据最小矩形点找最近的四边形点
//第一参数为输出的点,第二个参数为矩形的4个点,第三个为多边形拟合的点
void GetPointsFromRect(Point2f vetPoints[], Point2f rectPoints[], vector convex)
{
//定义最远的4个点,0--左上, 1--右上, 2--右下 3--左下
float ltdist = 99999999.9f; //左上的最大距离
float rtdist = 99999999.9f; //右上的最大距离
float rbdist = 99999999.9f; //右下的最大距离
float lbdist = 99999999.9f; //左下的最大距离
float curdist = 0.0f; //当前点的计算距离
for (auto curpoint : convex) {
//计算左上点的距离
curdist = CalcPointDistance(rectPoints[0], curpoint);
if (curdist < ltdist) {
ltdist = curdist;
vetPoints[0] = curpoint;
}
//计算右上角的点距离
curdist = CalcPointDistance(rectPoints[1], curpoint);
if (curdist < rtdist) {
rtdist = curdist;
vetPoints[1] = curpoint;
}
//计算右下角点的距离
curdist = CalcPointDistance(rectPoints[2], curpoint);
if (curdist < rbdist) {
rbdist = curdist;
vetPoints[2] = curpoint;
}
//计算左下角点的距离
curdist = CalcPointDistance(rectPoints[3], curpoint);
if (curdist < lbdist) {
lbdist = curdist;
vetPoints[3] = curpoint;
}
}
}
//计算两点间的距离
float CalcPointDistance(Point2f point1, Point2f point2)
{
//计算两个点的Point差值
Point2f tmppoint = point1 - point2;
//利用欧几里德距离计算H
return sqrt(pow(tmppoint.x, 2) + pow(tmppoint.y, 2));
}
TIPS
距离计算时一开始用的是旋转矩形的中心点离多边形拟合按左上,右上,右下,左下的方向找最远的4个,但是在某些斜的角度比较厉害的时候,这个计算问题不小,所以后来改为离最小旋转矩形的点最近的来找了。按中心点找最远距离的函数代码没删,一并贴上来。
#include
#include
using namespace std;
using namespace cv;
//根据中心点找四角最远的点
void GetRectPoints(Point2f vetPoints[], Point2f center, vector convex);
//根据最小矩形点找最近的四边形点
void GetPointsFromRect(Point2f vetPoints[], Point2f rectPoints[], vector convex);
//排序旋转矩形坐标点
void SortRotatedRectPoints(Point2f vetPoints[], RotatedRect rect);
//计算距离
float CalcPointDistance(Point2f point1, Point2f point2);
int main(int argc, char** argv) {
Mat src = imread("E:/DCIM/tsnew.jpg");
Mat gray, dst, dst2, result;
//图像缩放
resize(src, gray, Size(0, 0), 0.2, 0.2);
imshow("src", gray);
//灰度图
cvtColor(gray, dst, COLOR_BGRA2GRAY);
//高斯滤波
GaussianBlur(dst, dst, Size(3, 3), 0.5, 0.5);
imshow("gray", dst);
//二值化
threshold(dst, dst2, 0, 255, THRESH_BINARY | THRESH_OTSU);
imshow("thresh", dst2);
//形态学开操作
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
morphologyEx(dst2, dst2, MORPH_OPEN, kernel);
imshow("morph", dst2);
//Canny边缘检测
Canny(dst2, result, 127, 255, 7, true);
imshow("canny", result);
//查找轮廓
vector> contours;
findContours(result, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
cout << contours.size() << endl;
vector> contours_poly(contours.size());
Mat tmpgray;
gray.copyTo(tmpgray);
for (int i = 0; i < contours.size(); ++i) {
//计算轮廓周长,大于图像宽度的才算主体
double dlen = arcLength(contours[i], true);
if (dlen > gray.cols) {
//多边形拟合
approxPolyDP(Mat(contours[i]), contours_poly[i], 10, true);
cout << "当前:" << i << " 点数:" << contours_poly[i].size() << endl;
if (contours_poly[i].size() >= 4) {
//获取最小旋转矩形
RotatedRect rRect = minAreaRect(contours_poly[i]);
Point2f vertices[4];
//重新排序矩形坐标点,按左上,右上,右下,左下顺序
SortRotatedRectPoints(vertices, rRect);
cout << vertices[0] << vertices[1] << vertices[2] << vertices[3] << endl;
//根据获得的4个点画线
for (int k = 0; k < 4; ++k) {
line(gray, vertices[k], vertices[(k + 1) % 4], Scalar(255, 0, 0));
}
//多边形拟合的画出轮廓
drawContours(gray, contours_poly, i, Scalar(0, 0, 255));
//计算四边形的四点坐标
Point2f rPoints[4];
//GetRectPoints(rPoints, rRect.center, contours_poly[i]);
GetPointsFromRect(rPoints, vertices, contours_poly[i]);
for (int k = 0; k < 4; ++k) {
line(gray, rPoints[k], rPoints[(k + 1) % 4], Scalar(255, 255, 255));
}
//根据最小矩形和多边形拟合的最大四个点计算透视变换矩阵
//重新排序多边形拟合的4点
Rect rect = rRect.boundingRect();
rectangle(gray, rect, Scalar(0, 0, 0));
Point2f rectPoint[4];
rectPoint[0] = Point2f(rect.x, rect.y);
rectPoint[1] = Point2f(rect.x + rect.width, rect.y);
rectPoint[2] = Point2f(rect.x + rect.width, rect.y + rect.height);
rectPoint[3] = Point2f(rect.x, rect.y + rect.height);
//vector vecpt(vertices, vertices + 4);
//GetRectPoints(vertices, rRect.center, vecpt);
//计算透视变换矩阵
Mat warpmatrix = getPerspectiveTransform(rPoints, rectPoint);
Mat resultimg;
//透视变换
warpPerspective(tmpgray, resultimg, warpmatrix, resultimg.size(), INTER_LINEAR);
imshow("resultimg", resultimg);
}
}
}
imshow("src2", gray);
waitKey(0);
return 0;
}
//根据中心点找最远的四个点
void GetRectPoints(Point2f vetPoints[], Point2f center, vector convex)
{
//定义最远的4个点,0--左上, 1--右上, 2--右下 3--左下
float ltdist = 0.0f; //左上的最大距离
float rtdist = 0.0f; //右上的最大距离
float rbdist = 0.0f; //右下的最大距离
float lbdist = 0.0f; //左下的最大距离
for (auto curpoint : convex) {
//计算点的距离
float curdist = CalcPointDistance(center, curpoint);
if (curpoint.x < center.x && curpoint.y < center.y)
{
//判断是否在左上
if (curdist > ltdist) {
ltdist = curdist;
vetPoints[0] = curpoint;
}
}
else if (curpoint.x > center.x && curpoint.y < center.y) {
//判断在右上
if (curdist > rtdist) {
rtdist = curdist;
vetPoints[1] = curpoint;
}
}
else if (curpoint.x > center.x && curpoint.y > center.y) {
//判断在右下
if (curdist > rbdist) {
rbdist = curdist;
vetPoints[2] = curpoint;
}
}
else if (curpoint.x < center.x && curpoint.y > center.y) {
//判断在左下
if (curdist > lbdist) {
lbdist = curdist;
vetPoints[3] = curpoint;
}
}
}
}
//根据最小矩形点找最近的四边形点
//第一参数为输出的点,第二个参数为矩形的4个点,第三个为多边形拟合的点
void GetPointsFromRect(Point2f vetPoints[], Point2f rectPoints[], vector convex)
{
//定义最远的4个点,0--左上, 1--右上, 2--右下 3--左下
float ltdist = 99999999.9f; //左上的最大距离
float rtdist = 99999999.9f; //右上的最大距离
float rbdist = 99999999.9f; //右下的最大距离
float lbdist = 99999999.9f; //左下的最大距离
float curdist = 0.0f; //当前点的计算距离
for (auto curpoint : convex) {
//计算左上点的距离
curdist = CalcPointDistance(rectPoints[0], curpoint);
if (curdist < ltdist) {
ltdist = curdist;
vetPoints[0] = curpoint;
}
//计算右上角的点距离
curdist = CalcPointDistance(rectPoints[1], curpoint);
if (curdist < rtdist) {
rtdist = curdist;
vetPoints[1] = curpoint;
}
//计算右下角点的距离
curdist = CalcPointDistance(rectPoints[2], curpoint);
if (curdist < rbdist) {
rbdist = curdist;
vetPoints[2] = curpoint;
}
//计算左下角点的距离
curdist = CalcPointDistance(rectPoints[3], curpoint);
if (curdist < lbdist) {
lbdist = curdist;
vetPoints[3] = curpoint;
}
}
}
//重新排序旋转矩形坐标点
void SortRotatedRectPoints(Point2f vetPoints[], RotatedRect rect)
{
rect.points(vetPoints);
cout << vetPoints[0] << vetPoints[1] << vetPoints[2] << vetPoints[3] << endl;
cout << rect.angle << endl;
Point2f curpoint;
//根据Rect的坐标点,Y轴最大的为P[0],p[0]围着center顺时针旋转,
//旋转角度为负的话即是P[0]在左下角,为正P[0]是右下角
//重新排序坐标点
if (rect.angle > 0) {
curpoint = vetPoints[0];
vetPoints[0] = vetPoints[2];
vetPoints[2] = curpoint;
curpoint = vetPoints[1];
vetPoints[1] = vetPoints[3];
vetPoints[3] = curpoint;
}
else if (rect.angle < 0) {
curpoint = vetPoints[0];
vetPoints[0] = vetPoints[1];
vetPoints[1] = vetPoints[2];
vetPoints[2] = vetPoints[3];
vetPoints[3] = curpoint;
}
}
//计算两点间的距离
float CalcPointDistance(Point2f point1, Point2f point2)
{
//计算两个点的Point差值
Point2f tmppoint = point1 - point2;
//利用欧几里德距离计算H
return sqrt(pow(tmppoint.x, 2) + pow(tmppoint.y, 2));
}
完
扫描二维码
获取更多精彩
微卡智享
「 往期文章 」
.Net5 Windows Form App中Linq的分组查询使用
Android CameraX NDK OpenCV(四)-- 二维码检测与识别
.Net5下定时任务Quartz的使用