先进行边缘检测:
假设
CANNY_THRESH_1 = 90
CANNY_THRESH_2 = 120
edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2)
edges = cv2.dilate(edges, None, iterations=3)
edges = cv2.erode(edges, None, iterations=1)
边缘特征比较明显,不同的图片系数差别不大。当前系数效果可以使用。更细的参数没有调整。
另外做了一些放大和腐蚀,把不需要的一些小的文字区域过滤掉了。方便过滤不需要的轮廓。
然后提取轮廓:
查找轮廓
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
显示所有轮廓
mask = np.zeros(img.shape)for c in contours:
过滤小面积
if (cv2.contourArea(c) < scale ** 2):
continue
cv2.drawContours(mask, [c], 0, (0, 0, 255))cv2.imshow('contours', mask)
关于函数findContours介绍可以参考:https://www.cnblogs.com/yiyi20120822/p/11506970.html
提取到的轮廓后用一个比较单间的最小包围矩形就能得到想要的结果。
最后提取矩形获取顶点:
rect = cv2.minAreaRect(cnt)box = cv2.boxPoints(rect)box = np.int0(box)
就可以得到顶点坐标