opencv实现图像倾斜矫正(C++)

逻辑思路

  1. 首先借助霍夫变换来进行直线检测,从而画定图像中的直线。
  2. 根据霍夫变换中的直线,从而确定其中的theta值,然后将该值叠加求均值即可得到倾斜角度
  3. 利用该倾斜角度调用函数getRotationMatrix2D来获得旋转矩阵
  4. 采用映射变化从而达成矫正的效果。

实现效果

在这里插入图片描述
原图:
请添加图片描述

代码如下:

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2\imgproc\types_c.h>

#include <iostream>
#include <opencv2/opencv.hpp>
#include <math.h>//引用函数文件库
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
#define ERROR 65576
//度数转换
double DegreeTrans(double theta)
{
    double res = theta / CV_PI * 180;
    return res;
}
//逆时针旋转图像degree角度(原尺寸)
void rotateImage(Mat src, Mat& img_rotate, double degree)
{

    cv::Size dst_sz(src.cols, src.rows);        //保持图像大小一致
    cv::Point2f center(static_cast<float>(src.cols / 2.), static_cast<float>(src.rows / 2.));       //指定旋转中心
    cv::Mat rot_mat = cv::getRotationMatrix2D(center, degree, 1.0); //获取旋转矩阵(2x3矩阵)
    cv::warpAffine(src, img_rotate, rot_mat, dst_sz);

//通过霍夫变换计算角度
double CalcDegree(const Mat& srcImage)
{
    Mat midImage, dstImage;
    Canny(srcImage, midImage, 50, 200, 3);
    cvtColor(midImage, dstImage, COLOR_GRAY2BGR);
    //通过霍夫变换检测直线
    vector<Vec2f> lines;
    HoughLines(midImage, lines, 1, CV_PI / 180, 300, 0, 0);//第5个参数就是阈值,阈值越大,检测精度越高
    //由于图像不同,阈值不好设定,因为阈值设定过高导致无法检测直线,阈值过低直线太多,速度很慢
    //所以根据阈值由大到小设置了三个阈值,如果经过大量试验后,可以固定一个适合的阈值。
    if (!lines.size())
    {
        HoughLines(midImage, lines, 1, CV_PI / 180, 200, 0, 0);
    }
    //cout << lines.size() << endl;
    if (!lines.size())
    {
        HoughLines(midImage, lines, 1, CV_PI / 180, 150, 0, 0);
    }
    //cout << lines.size() << endl;
    if (!lines.size())
    {
        cout << "没有检测到直线!" << endl;
        return ERROR;
    }
    float sum = 0;
    //依次画出每条线段
    for (size_t i = 0; i < lines.size(); i++)
    {
        float rho = lines[i][0];
        float theta = lines[i][1];
        Point pt1, pt2;
        //cout << theta << endl;
        double a = cos(theta), b = sin(theta);
        double x0 = a * rho, y0 = b * rho;
        pt1.x = cvRound(x0 + 1000 * (-b));
        pt1.y = cvRound(y0 + 1000 * (a));
        pt2.x = cvRound(x0 - 1000 * (-b));
        pt2.y = cvRound(y0 - 1000 * (a));
        //只选角度最小的作为旋转角度
        sum += theta;
        line(dstImage, pt1, pt2, Scalar(55, 100, 195), 1, LINE_AA); //Scalar函数用于调节线段颜色
        imshow("直线探测效果图", dstImage);
    }
    float average = sum / lines.size(); //对所有角度求平均,这样做旋转效果会更好
    cout << "average theta:" << average << endl;
    double angle = DegreeTrans(average)-90;
    return angle;
}
void ImageRecify(const char* pInFileName)
{
    double degree;
    Mat src = imread(pInFileName);
    Mat src1 = imread("G:\\Image\\1(1).png");
//    imshow("原始图", src);
    Mat dst;
    //倾斜角度矫正
    degree = CalcDegree(src);
    if (degree == ERROR)
    {
        cout << "矫正失败!" << endl;
        return;
    }
    rotateImage(src1, dst, degree);
    cout << "angle:" << degree << endl;
    imshow("旋转调整后", dst);
}
int main()
{
    ImageRecify("G:\\Image\\test.png");
    waitKey();
    return 0;
}

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值