opencv实现图片中文字识别并切割

背景:

为了提升用户欣赏书法图片的体验,需要从高清TIF图片中把每个字都切割出来,手动切割太麻烦,所以利用opencv自动识别图片中的文字,并将每个文字切割保存。

实现代码:

import cv2
import os
import sys
import numpy as np


#自动切割单字
def split_image_by_auto(filePath, saveTo, threshold, margin):
    # img = cv2.imread(filePath)
    filename = filePath.split('\\')[-1].split(".")[0]
    print(filename)

    if os.path.exists(saveTo + filename) == False :
        os.mkdir(saveTo + filename)

    img = cv2.imdecode(np.fromfile(filePath, dtype=np.uint8), -1)
    sp = img.shape
    print("图像信息:", sp)

    h = sp[0]
    w = sp[1]
    c = sp[2]

    #save 一份完整的
    resize = cv2.resize(img, (int(w * 1000 / h), 1000))
    # cv2.imwrite(saveTo + filename+"_0.jpg", resize)
    cv2.imencode('.jpg', resize)[1].tofile(saveTo + filename+"/0.jpg") #支持中文

    gray_img

你可能感兴趣的:(python,opencv,opencv,计算机视觉,图像处理)