程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

文本倾斜校正的两种方法(Python-OpenCv)

发布于2019-08-17 20:53     阅读(1329)     评论(0)     点赞(3)     收藏(0)


一、通过minAreaRect方法获得斜率

参考资料
【1】有关角度的说明,可供参考
【2】https://blog.csdn.net/qq_24237837/article/details/77850496

主要步骤
1、输入图片
2、灰度化
3、图像取非
4、二值化
5、获得有文本区域的点集
6、求点集的最小外接矩形框,并返回旋转角度
7、仿射变换,将原图校正

参考代码

import numpy as np
import os
import cv2
import math

def rotate(image,angle,center=None,scale=1.0):
    (w,h) = image.shape[0:2]
    if center is None:
        center = (w//2,h//2)   
    wrapMat = cv2.getRotationMatrix2D(center,angle,scale)    
    return cv2.warpAffine(image,wrapMat,(h,w))
#使用矩形框
def getCorrect():
    #读取图片,灰度化
    src = cv2.imread("D:\\MyData\\tiltData\\neg.png")
    cv2.imshow("src",src)
    cv2.waitKey()
    gray = cv2.imread("D:\\MyData\\tiltData\\neg.png",cv2.IMREAD_GRAYSCALE)
    cv2.imshow("gray",gray)
    cv2.waitKey()
    #图像取非
    grayNot = cv2.bitwise_not(gray)
    cv2.imshow("grayNot",grayNot)
    cv2.waitKey()
    #二值化
    threImg = cv2.threshold(grayNot,100,255,cv2.THRESH_BINARY,)[1]
    cv2.imshow("threImg",threImg)
    cv2.waitKey()
    #获得有文本区域的点集,求点集的最小外接矩形框,并返回旋转角度
    coords = np.column_stack(np.where(threImg>0))
    angle = cv2.minAreaRect(coords)[-1]  
    if angle < -45:
        angle = -(angle + 90)
    else:
        angle = -angle

    #仿射变换,将原图校正
    dst = rotate(src,angle)
    cv2.imshow("dst",dst)
    cv2.waitKey()
    print(angle)
    
    
if __name__ == "__main__":              
    getCorrect()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

二、通过霍夫变换、反三角函数获得斜率

参考资料
【1】https://www.jianshu.com/p/34d6dc466e81
【2】该博主有倾斜图片数据可供下载,对我的测试有很大帮助,非常感谢

主要步骤
1、输入图片
2、灰度化
3、腐蚀、膨胀
4、边缘检测
5、霍夫变换得到线条并画出
6、计算角度
7、仿射变换,将原图校正

参考代码

import numpy as np
import os
import cv2
import math
from scipy import misc,ndimage

def rotate(image,angle,center=None,scale=1.0):
    (w,h) = image.shape[0:2]
    if center is None:
        center = (w//2,h//2)   
    wrapMat = cv2.getRotationMatrix2D(center,angle,scale)    
    return cv2.warpAffine(image,wrapMat,(h,w))
#使用霍夫变换
def getCorrect2():
    #读取图片,灰度化
    src = cv2.imread("D:\\MyData\\tiltData\\neg.png",cv2.IMREAD_COLOR)
    showAndWaitKey("src",src)
    gray = cv2.imread("D:\\MyData\\tiltData\\neg.png",cv2.IMREAD_GRAYSCALE)
    showAndWaitKey("gray",gray)
    #腐蚀、膨胀
    kernel = np.ones((5,5),np.uint8)
    erode_Img = cv2.erode(gray,kernel)
    eroDil = cv2.dilate(erode_Img,kernel)
    showAndWaitKey("eroDil",eroDil)
    #边缘检测
    canny = cv2.Canny(eroDil,50,150)
    showAndWaitKey("canny",canny)
    #霍夫变换得到线条
    lines = cv2.HoughLinesP(canny, 0.8, np.pi / 180, 90,minLineLength=100,maxLineGap=10)
    drawing = np.zeros(src.shape[:], dtype=np.uint8)
    #画出线条
    for line in lines:
        x1, y1, x2, y2 = line[0]
        cv2.line(drawing, (x1, y1), (x2, y2), (0, 255, 0), 1, lineType=cv2.LINE_AA)
    
    showAndWaitKey("houghP",drawing)
    """
    计算角度,因为x轴向右,y轴向下,所有计算的斜率是常规下斜率的相反数,我们就用这个斜率(旋转角度)进行旋转
    """
    k = float(y1-y2)/(x1-x2)
    thera = np.degrees(math.atan(k))

    """
    旋转角度大于0,则逆时针旋转,否则顺时针旋转
    """
    rotateImg = rotate(src,thera)
    cv2.imshow("rotateImg",rotateImg)
    cv2.waitKey()

def showAndWaitKey(winName,img):
    cv2.imshow(winName,img)
    cv2.waitKey()

if __name__ == "__main__":              
    getCorrect()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55


所属网站分类: 技术文章 > 博客

作者:黎明要到来了

链接:https://www.pythonheidong.com/blog/article/48438/0baaeacab3aa7fffa2e2/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

3 0
收藏该文
已收藏

评论内容:(最多支持255个字符)