1、预处理、轮廓检测
import cv2
import numpy as np
# 正确答案
ANSWER_KEY = {
0:1,1:4,2:0,3:3,4:1}
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey()
cv2.destroyAllWindows()
# 读图
img = cv2.imread("test_01.png")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
edge = cv2.Canny(blur,75,200)
# 轮廓检测
cnt = cv2.findContours(edge,cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[0]
cv2.drawContours(contour_img,cnt,-1,(0,255,0),2)
cv_show("contour_img",contour_img)
2、轮廓排序,透视变化
def order_points(pts):
# 一共四个坐标点
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)]
# 计算右上,左下
d = np.diff(pts,axis