图像处理实战-文档扫描

2020-06-22  本文已影响0人  YvanYan

文档识别项目的主要工作是:识别出给定图片中的文档区域。.

完整代码:https://github.com/YvanYan/image_processing/tree/master/document


实现流程:
1.对输入图片进行边缘检测。
2.计算近似轮廓。
3.对文档区域进行透视变换。

1.对输入图片进行边缘检测。

image = cv2.imread('images/page.jpg')
image = resize(image, height=1000)
cv_show('image', image)

size_ratio = image.shape[0] / (TRANSFORM_HEIGHT * 1.0)
image_copy = image.copy()

image = resize(image, height=TRANSFORM_HEIGHT)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image_gray = cv2.GaussianBlur(image_gray, (5, 5), 0)
image_edge = cv2.Canny(image_gray, 75, 200)
cv_show('image_edge', image_edge)

2.计算近似轮廓。

cnts = cv2.findContours(image_edge, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]

for c in cnts:
    peri = cv2.arcLength(c, True)

    approx = cv2.approxPolyDP(c, 0.02 * peri, True)

    if len(approx) == 4:
        screenCnt = approx
        break

image_ = cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
cv_show('image_', image)

3.对文档区域进行透视变换。

def order_points(pts):
    # 一共4个坐标点
    rect = np.zeros((4, 2), dtype="float32")

    # 按顺序找到对应坐标0123分别是 左上,右上,右下,左下
    # 计算左上,右下
    s = pts.sum(axis=1)
    rect[0] = pts[np.argmin(s)]
    rect[2] = pts[np.argmax(s)]

    # 计算右上和左下
    diff = np.diff(pts, axis=1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]

    return rect


def four_point_transform(image, pts):
    # 获取输入坐标点
    rect = order_points(pts)
    (tl, tr, br, bl) = rect

    # 计算输入的w和h值
    widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
    widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
    maxWidth = max(int(widthA), int(widthB))

    heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
    heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
    maxHeight = max(int(heightA), int(heightB))

    # 变换后对应坐标位置
    dst = np.array([
        [0, 0],
        [maxWidth - 1, 0],
        [maxWidth - 1, maxHeight - 1],
        [0, maxHeight - 1]], dtype="float32")

    # 计算变换矩阵
    M = cv2.getPerspectiveTransform(rect, dst)
    warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))

    # 返回变换后结果
    return warped

# 透视变换
warped = four_point_transform(image_copy, screenCnt.reshape(4, 2) * size_ratio)
cv_show('warped', warped)

这样就得到了图片中的文档区域。

结果

image.png
上一篇下一篇

猜你喜欢

热点阅读