Text rotation correction

Text rotation correction idea
general idea: text generally divided into two parts and the return angle detection, rotating the image
text and the return angle detection: linear Hough transform on the detected vector In order to draw lines, each line calculated by the average angular
rotation of the image: using affine transformation (getRotationMatrix2D and warpAffine function)

#include<opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

//度数转换
double DegreeTrans(double theta)
{
    double res = theta / CV_PI * 180;
    return res;
}

//逆时针旋转图像degree角度(原尺寸)    
void rotateImage(Mat src, Mat& img_rotate, double degree)
{
    //旋转中心为图像中心    
    Point2f center;
    center.x = float(src.cols / 2.0);
    center.y = float(src.rows / 2.0);
    int length = 0;
    length = sqrt(src.cols*src.cols + src.rows*src.rows);
    //计算二维旋转的仿射变换矩阵  
    Mat M = getRotationMatrix2D(center, degree, 1);
    warpAffine(src, img_rotate, M, Size(length, length), 1, 0, Scalar(255, 255, 255));//仿射变换,背景色填充为白色  
}

//通过霍夫变换计算角度
double CalcDegree(const Mat &srcImage, Mat &dst)
{
    Mat midImage, dstImage;

    Canny(srcImage, midImage, 50, 200, 3);
    cvtColor(midImage, dstImage, CV_GRAY2BGR);

    //通过霍夫变换检测直线
    vector<Vec2f> lines;
    HoughLines(midImage, lines, 1, CV_PI / 180, 220);
    /*for (int i = 0; i < lines.size(); i++)
    {
        float rho = lines[i][0], theta = lines[i][1];
        Point pt1, pt2;
        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));
        line(midImage, pt1, pt2, Scalar(55, 100, 195), 1, LINE_AA);
    }
    imshow("zxcad",midImage);*/


    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;
        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);

        
    }
    float average = sum / lines.size(); //对所有角度求平均,这样做旋转效果会更好

    double angle = DegreeTrans(average) - 90;

    rotateImage(dstImage, dst, angle);
    imshow("霍夫线检测效果", dstImage);
    return angle;
}





int main()
{
    
    double degree;
    Mat src = imread("G:\\文字角度校正.png");
    imshow("src", src);
    Mat dst;
    //倾斜角度矫正
    degree = CalcDegree(src, dst);
    rotateImage(src, dst, degree);
    cout << "angle:" << degree+90 << endl;
    imshow("旋转调整", dst);

    Mat resultImage = dst(Rect(0, 0, dst.cols - 100, 400)); //根据先验知识,估计好文本的长宽,再裁剪下来
    imshow("最终效果图", resultImage);
    waitKey();
    return 0;
}

Guess you like

Origin www.cnblogs.com/xingkongcanghai/p/11420272.html