使用opencv从扫描的文档中提取明信片?

Syl*_*ain -4 python opencv scanning opencv-python

我有1000张旧明信片,我想扫描,我认为使用某种自动裁剪/旋转工具优化我的工作流程可能是一个好主意,所以我开始用Python调查openCV.

下面是我可以使用我的扫描仪获取的图片示例: 样品扫描

你可以想象,我的目标是从这张图片中创建3张图片,每张图片包含一张明信片.我尝试了很多opencv选项,到目前为止我能够获得的最佳代码是:

import cv2, sys, imutils

cv2.namedWindow('image', cv2.WINDOW_NORMAL)

image = cv2.imread("sample1600.jpg")
ratio = image.shape[0] / 300.0
image = imutils.resize(image, height = 800)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
ret, th = cv2.threshold(gray,220,235,1)
edged = cv2.Canny(th, 25, 200)

(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]

for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.05 * peri, True)

    if len(approx) == 4:
        cv2.drawContours(image, [approx], -1, (0, 255, 0), 3)

cv2.imshow("Image", image)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

制作的图像是: 结果图片

这段代码的问题是:

  • 它没有找到太靠近边界的底部图像;
  • 它只适用于我的测试图像,但似乎不是非常通用.例如,行"ret,th = cv2.threshold(gray,220,235,1)"将阻止事物处理具有不同直方图的图像.

有没有人知道如何使这个代码更好地工作,并更通用地满足我处理扫描图像的要求?

编辑:我最初没有提到的东西,可能有用的是个别明信片的宽度和高度之间的比例大约为√2.情况并非总是如此,但如果我的剧本能够有效地处理这种类型的明信片,我将非常高兴(他们代表我收藏的99%以上)

编辑24/04:感谢@Riccardo我现在有一个适用于我的第一个示例图像的脚本,因此添加一个新脚本以尝试找到更强大的解决方案: 对比度较低的样品

编辑24/04#2:由于@Riccardo非常有效地为两个第一个样本提供了解决方案,因此第一个样本的图像之间的空间有限,因此其他两个看起来有点复杂: 重叠图像

或某些部分几乎空白的卡片: 很多空白

el_*_*ldo 14

我建议通过计算轮廓的旋转边界框,而不是试图识别固定的形状.在我的尝试中,脚本识别一个类似于盒子的图形并计算其contourArea,然后它选择具有大区域的图形.

这应该可以解决您的问题,如果没有,请告诉我们.

cv2.namedWindow('image', cv2.WINDOW_NORMAL)

image = cv2.imread("sample1600.jpg")
ratio = image.shape[0] / 300.0
image = imutils.resize(image, height = 800)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)


ret, th = cv2.threshold(gray,220,235,1)
edged = cv2.Canny(th, 25, 200)

im2, cnts, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_TREE,   cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)

for c in cnts:
    box = cv2.minAreaRect(c)
    box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
    box = np.array(box, dtype="int")
    if cv2.contourArea(box) > 70000:
        cv2.drawContours(image, [box], -1, (0, 255, 0), 2)

cv2.imshow("Image", image)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

这是输出: 在此输入图像描述

编辑:我不知道这是否是正确的解决方案,可能还有其他一些.我鼓励其他用户分享他们的方法.@Sylvain,这是另一个尝试调整参数:

  • 将阈值降低到210;
  • 删除canny函数(它与一些图像的复杂模式混淆;
  • 计算图像区域并以要返回的轮廓极限进行游戏.在该特定示例中,我将轮廓施加为大于图像的1/10并且小于2/3.

    image = cv2.imread(img)
    
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    
    ret, th = cv2.threshold(gray,210,235,1)
    
    im2, cnts, hierarchy = cv2.findContours(th.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    cnts = sorted(cnts, key = cv2.contourArea, reverse = True)
    
    for c in cnts:
        box = cv2.minAreaRect(c)
        box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
        box = np.array(box, dtype="int")
        Area = image.shape[0]*image.shape[1]
        if Area/10 < cv2.contourArea(box) < Area*2/3:
            cv2.drawContours(image, [box], -1, (0, 255, 0), 2)
    
    cv2.imshow("Image", image)
    cv2.waitKey(0)
    
    Run Code Online (Sandbox Code Playgroud)

  • 我相信你可以自己解决这个问题,只需像我一样调整参数,结果就会到来.请享用 (9认同)