实战三、OpenCv答题卡识别判卷

import cv2
import numpy as np

ANSWER_KEY={0:1,1:4,2:0,3:3,4:1}

# 获取坐标点
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):#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))#根号下(x3-x4)^2+(y3-y4)^2
    widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))#根号下(x2-x1)^2+(y2-y1)^2
    maxWidth = max(int(widthA), int(widthB))#取得两个中的最大值,作为目标图像的宽

    heightA = np.s

你可能感兴趣的:(opencv,人工智能,计算机视觉)