1. 程式人生 > >影象文字識別之影象分割(待改進)

影象文字識別之影象分割(待改進)

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, 127, 255, cv2.THRESH_BINARY_INV)
thresh = np.array(thresh)

#統計每行黑點個數,引數為二值圖陣列
def countPoint(img):
    re = []
    for th in img:
        re.append(sum(th)/255)
    return re

#根據每行黑點個數選取進行切割的位置,引數為二值圖陣列,以及切割方向
def findPoint(img,axis):
    if(axis == 'y'):
        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
        elif(countx[x] !=0):
            end = x
        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 == 'x'):
            re.append(img[x[0]: x[1]])
        elif(axis == 'y'):
            re.append(img[:, x[0]: x[1]])
    return re

#繪製切割後的影象
def cutImage(img):
    count = 1
    for r_x in Cut(img,'x'):
        for r_y in Cut(r_x, 'y'):
            cv2.imwrite("C:/Users/wang/Desktop/image/" + str(count) + ".jpg", r_y)
            count += 1

cutImage(thresh)

未完待續