Python 如何找到已完成表单扫描图像的轮廓?
我想在此扫描中检测已完成表格的轮廓 理想情况下,我想找到涂有红色的桌子角落 我的最终目标是检测整个文档是否被扫描,并且四个角是否在扫描的边界内 我使用了python中的OpenCV,但它无法找到大容器的轮廓 有什么想法吗Python 如何找到已完成表单扫描图像的轮廓?,python,image,opencv,image-processing,computer-vision,Python,Image,Opencv,Image Processing,Computer Vision,我想在此扫描中检测已完成表格的轮廓 理想情况下,我想找到涂有红色的桌子角落 我的最终目标是检测整个文档是否被扫描,并且四个角是否在扫描的边界内 我使用了python中的OpenCV,但它无法找到大容器的轮廓 有什么想法吗 使用方向范围窄的Hough变换来查找垂直面和水平面怎么样?如果幸运的话,您需要的将是最长的,选择它们后,您可以重建矩形。观察到可以使用表格网格识别表单,下面是一个简单的方法: 获取二值图像。加载图像、灰度、高斯模糊,然后加载大津阈值以获取二值图像 找到水平部分。我们创建一个水
使用方向范围窄的Hough变换来查找垂直面和水平面怎么样?如果幸运的话,您需要的将是最长的,选择它们后,您可以重建矩形。观察到可以使用表格网格识别表单,下面是一个简单的方法:
结果如下: 输入图像 检测到要提取的轮廓以绿色突出显示 四点透视变换后的输出 代码
你能分享你尝试过的代码吗?当然,我尝试过轮廓检测的解决方案:,这里也有描述:。我还尝试了一些脚本来查找轮廓,如下所述:。运行第一个解决方案时,它返回图片的轮廓(我猜是因为扫描的页面和背景之间没有对比度)。第二,我找不到最大的多边形。看起来你正试图通过它的轮廓来检测桌子。在这里检查我的答案
import cv2
import numpy as np
from imutils.perspective import four_point_transform
# Load image, create mask, grayscale, and Otsu's threshold
image = cv2.imread('1.jpg')
mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,11,3)
# Find horizontal sections and draw on mask
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (80,1))
detect_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(mask, [c], -1, (255,255,255), -1)
# Find vertical sections and draw on mask
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
detect_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(mask, [c], -1, (255,255,255), -1)
# Fill text document body
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
close = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, close_kernel, iterations=3)
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(mask, [c], -1, 255, -1)
# Perform morph operations to remove noise
# Find contours and sort for largest contour
opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, close_kernel, iterations=5)
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
displayCnt = None
for c in cnts:
# Perform contour approximation
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
if len(approx) == 4:
displayCnt = approx
break
# Obtain birds' eye view of image
warped = four_point_transform(image, displayCnt.reshape(4, 2))
cv2.imwrite('mask.png', mask)
cv2.imwrite('thresh.png', thresh)
cv2.imwrite('warped.png', warped)
cv2.imwrite('opening.png', opening)