垂直投影法分割A3试卷,目前效果还不错~

原图:

分割实例:

 垂直投影法分割A3试卷,目前效果还不错~_第1张图片

 代码:(配合上一篇A3试卷的旋转矫正一起食用,效果更佳~)

# 试卷拆解为A4纸大小,使用paper_remake进行试卷重组
import copy
import base64
import cv2
import numpy as np
import matplotlib.pyplot as plt
from PaperRotate import paper_rotate


def cv2_base64(image):
    base64_str = cv2.imencode('.jpg',image)[1].tobytes()
    base64_str = base64.b64encode(base64_str)
    return base64_str


def base64_img(base64_str):
    img = base64.b64decode(base64_str)
    img = np.frombuffer(img, np.uint8)
    img = cv2.imdecode(img, cv2.COLOR_BGR2RGB)
    return img


# 扫描传入A3图像的分割设计


def paper_split(dou_page_img, min_value=0):
    images = []
    split_img = copy.copy(dou_page_img)
    split_img = cv2.resize(split_img, (dou_page_img.shape[1] // 10, dou_page_img.shape[0] // 10))
    gray = cv2.cvtColor(split_img, cv2.COLOR_BGR2GRAY)

    dst = cv2.adaptiveThreshold(src=gray, maxValue=255, adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                thresholdType=cv2.THRESH_BINARY, blockSize=59, C=25)

    # 投影法,垂直投影
    h, w = dst.shape
    log_w = [0 for i in range(w)]
    for x in range(w):
        for y in range(h):
            if dst[y, x] == 0:
                log_w[x] += 1

    empty_img = np.zeros((h, w, 3), np.uint8)
    for x in range(0, w):
        for y in range(0, log_w[x]):
            b = (255, 255, 255)
            empty_img[y, x] = b

    # 确定垂直投影图像分割点
    start_list = []
    start, end = 0, None

    for idx, val in enumerate(log_w):
        if val == min_value:
            end = idx
        else:
            if end is not None:
                start_list.append((start, end))
                end = None
            start = idx
    else:
        if end is not None:
            start_list.append((start, end))
            end = None

    splie_list = []
    for x, y in start_list:
        if y - x > 5:
            splie_list.append((x, y))

    split_point = splie_list[len(splie_list) // 2]
    split_point_idx = (split_point[0] + (split_point[1] - split_point[0]) // 2) * 10

    images.append(dou_page_img[:, 0:split_point_idx])
    images.append(dou_page_img[:, split_point_idx:dou_page_img.shape[1]])

    # lt = (split_point[0] * 10, 0)
    # rb = (split_point[1] * 10, dou_page_img.shape[0])
    # img = cv2.rectangle(dou_page_img, lt, rb, (0, 0, 0), -1)
    # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # plt.imshow(img)
    # plt.show()

    return images


def main(img_base64):
    pages = {}
    img = base64_img(img_base64)

    # 加入角度选择矫正
    img = paper_rotate(img)

    img_list = paper_split(img)
    pages['page_1'] = cv2_base64(img_list[0])
    pages['page_2'] = cv2_base64(img_list[1])
    return pages


if __name__ == '__main__':
    def show(img):
        cv2.namedWindow('img', 0)
        cv2.imshow('img', img)
        cv2.waitKey(0)


    def img_base64(img_path):
        with open(img_path, 'rb') as f:
            img_base64 = base64.b64encode(f.read())
        return img_base64

    path = '../ImgTest/4.jpg'
    base64img = img_base64(path)
    pages = main(base64img)

    img_1 = base64_img(pages['page_1'])
    img_2 = base64_img(pages['page_2'])

    show(img_1)
    show(img_2)

result:

垂直投影法分割A3试卷,目前效果还不错~_第2张图片

垂直投影法分割A3试卷,目前效果还不错~_第3张图片

 

 

你可能感兴趣的:(python,opencv,python,numpy,opencv)