Calculating skew of text OpenCV

前端 未结 2 1560
余生分开走
余生分开走 2020-12-28 10:41

I am trying to calculate the skew of text in an image so I can correct it for the best OCR results.

Currently this is the function I am using:

doubl         


        
2条回答
  •  自闭症患者
    2020-12-28 11:34

    the approach you posted has its own "ideal binarization" assumption. the threshold value directly affects the process. utilize otsu threshold, or think about DFT for a generic solution.

    otsu trial:

    int main()
    {
        Mat input = imread("your text");
        cvtColor(input, input, CV_BGR2GRAY);
        Mat img;
        cv::threshold(input, img, 100, 255, cv::THRESH_OTSU);
    
        cv::bitwise_not(img, img);
        imshow("img ", img);
        waitKey(0);
    
        vector points;
        findNonZero(img, points);
        cv::RotatedRect box = cv::minAreaRect(points);
    
        double angle = box.angle;
        if (angle < -45.)
            angle += 90.;
    
        cv::Point2f vertices[4];
        box.points(vertices);
        for(int i = 0; i < 4; ++i)
            cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(255, 0, 0));
        imshow("img ", img);
        waitKey(0);
    
        return 0;
    }
    

    enter image description here

提交回复
热议问题