图像文字识别之图像分割(改进后)

import cv2
import numpy as np

#读入图片,将图片转化为2值图,最后转化为数组
image = cv2.imread('C:/Users/wang/Desktop/test.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)

thresh = np.array(thresh)
path = "C:/Users/wang/Desktop/image/"

#统计每行黑点个数,参数为二值图数组
def countPoint(img):
    re = []
    for th in img:
        re.append(sum(th)/255)
    return re

#根据每行黑点个数选取进行切割的位置,参数为二值图数组,以及切割方向
def findPoint(img,axis):
    if(axis == 1):
        img = img.T
    start = -1
    end = -1
    result = []
    countx = countPoint(img)
    for x in range(len(countx)):
        if(countx[x] != 0 and start<0):
            start = x-1
            end = x+1   
        elif(countx[x] !=0):
            end = x+1
        elif(countx[x] == 0 and start>0):
            result.append([start, end])
            start, end = -1, -1
    return result

#根据切割位置进行分割,参数为二值图数组,以及切割方向
def Cut(img,axis):
    point = findPoint(img, axis)
    re = []
    for x in point:
        if (axis == 0):
            re.append(img[x[0]: x[1]])
        elif(axis == 1):
            re.append(img[:, x[0]: x[1]])
    return re

#切割图像
def cutImage(img):
    count = 1
    for r_x in Cut(img,0):
        i = 1
        r_y = Cut(r_x,1)
        while(i<len(r_y)):
            c = len(r_y[i].T)/len(r_y[i-1].T)
            if( c>=1.5 and c<=2.0 ):
                r = np.concatenate([r_y[i-1],r_y[i]],axis=1)
                if(r.shape[1]>20 or r.shape[1]<13):
                    pass
                else:
                    cv2.imwrite(path + str(count) + '.jpg', r)
                    count += 1
            else:
                cv2.imwrite(path + str(count) + '.jpg', r_y[i - 1])
                count += 1
            i+=1
        cv2.imwrite(path + str(count) + '.jpg', r_y[i - 1])
        count += 1

cutImage(thresh)

待说明

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值