下面的脚本(从pyimagesearch.com修改)尝试更正扫描卡的透视图以旋转和裁剪图像。我有完成转换的最终代码,但在此之前绘制的边界矩形没有按预期运行。这是原始图片:
码:# import the necessary packages
from skimage.filters import threshold_local
import numpy as np
import cv2
import imutils
# load the image and compute the ratio of the old height
# to the new height, clone it, and resize it
image = cv2.imread('cards/red8.jpg')
ratio = image.shape[0] / 500.0
orig = image.copy()
image = imutils.resize(image, height = 500)
# convert the image to grayscale, blur it, and find edges
# in the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
blur = cv2.GaussianBlur(gray,(1,1),1000)
edged = cv2.Canny(gray, 75, 200)
# find the contours in the edged image, keeping only the
# largest ones, and initialize the screen contour
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]
# loop over the contours
for c in cnts:
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
# if our approximated contour has four points, then we
# can assume that we have found our screen
if len(approx) == 4:
screenCnt = approx
break
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
被绘制的盒子的输出似乎被圆角抛弃并抛出透视校正,但是偏离了卡片的直边的一小部分。
我能做些什么来得到一个适当的边界矩形来计算弯曲的边缘?
谢谢!