import numpy as np
import cv2
# read image
img = cv2.imread('receipt.png')
# thresholding
blur = cv2.GaussianBlur(img, (5,5), 1)
hls = cv2.cvtColor(blur, cv2.COLOR_BGR2HLS)
low = np.array([0, 70, 0])
high = np.array([255, 255, 85])
thresh = cv2.inRange(hls, low, high)
# morphological operations to get the paper
kclose = np.ones((3,3), dtype=np.uint8)
kopen = np.ones((5,5), dtype=np.uint8)
closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kclose, iterations=2)
opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kopen, iterations=6)
# corner detection
opening = cv2.GaussianBlur(opening, (3,3), 1)
opening = np.float32(opening)
dst = cv2.cornerHarris(opening, 2, 3, 0.04)
# drawing corners
dst = cv2.dilate(dst, None)
img[dst>0.01*dst.max()]=[0,0,255]
cv2.imshow('Corners', img)
cv2.waitKey(0)
这里是角落:
请注意,从Harris算法中你会得到多个像素点,如果想要在后续的图像处理中使用它们进行变形,你需要进行聚类以获取单个角点。关闭:
打开:
kmeans
聚类找到这些4个点的中心,并称之为您的点。 - alkasm