【发布时间】:2019-01-12 07:42:36
【问题描述】:
下面的脚本(从 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)
绘制框的输出似乎被圆角甩掉了,并甩掉了透视校正,与卡片直边的轻微部分对接。
我可以做些什么来得到一个适当的边界矩形来解释弯曲的边缘吗?
谢谢!
【问题讨论】:
-
所有样本图像的背景是否都是灰色的?
-
是的,它们是一样的。每张图片有九张卡片。我试图让代码为一个人工作。
标签: python python-2.7 opencv