opencv - opencv python 减少差距canny
问题描述
我正在尝试为纸画轮廓。就像-> https://www.pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/
但是每次我运行此代码时,都会显示“NameError: name 'screenCnt' is not defined”错误。我发现我的图片有一点差距。这使得 len 的 approx 变为 3 。就像底部的图片一样。我怎样才能减少轮廓的间隙。
image = cv2.imread('example.jpg')
ratio = image.shape[0] / 500.0
orig = image.copy()
image = imutils.resize(image, height = 500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (3,3), 0)
edged = cv2.Canny(gray, 100, 200)
edge= auto_canny(gray)
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
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
# show the contour (outline) of the piece of paper
print("STEP 2: Find contours of paper")
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
cv2.imshow("Outline", image)
cv2.waitKey(0)
解决方案
推荐阅读
- c# - 倒计时到不包括周末和剩余假期的日期
- oracle-apex - 交互式报表 - 排序和过滤格式化列
- java - Jackson:用一个数组字段序列化/反序列化对象
- python - Web Scraping 遇到 XML 解析错误,如何修复?
- amazon-cloudformation - AWS CloudFormation 模板 - 如何开始
- scapy - 使用 scapy 阻止 DNS 请求包含单词“foo”的域名
- python - Pycharm:如何使用程序打开 Python 控制台
- jekyll - 站点或页面变量的 Jekyll 定义?
- linux - 在 Ubuntu 18.04 WSL 上安装 MariaDB - ISSUE 系统尚未使用 systemd 作为 init 系统 (PID 1) 启动。无法操作
- ios - 如何在 iOS Swift 中使用 Macaw 库获取用户点击的 SVG 图像视图层?