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

时间:2018-04-23 12:49:59

标签: python opencv scanning opencv-python

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

以下是我可以使用我的扫描仪获取的图片示例: sample scan

您可以想象,我的目标是从这张图片中创建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)

制作的图片是: resuling picture

此代码的问题是:

  • 它找不到太靠近边框的底部图像;
  • 它仅适用于我的测试图像,但似乎不是非常通用的。线" ret,th = cv2.threshold(灰色,220,235,1)"例如,会阻止事物处理我认为具有不同直方图的图像。

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

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

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

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

或某些部分几乎空白的卡片: lot of blank

1 个答案:

答案 0 :(得分: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)

这是输出: enter image description here

编辑: 我不知道这是否是正确的解决方案,可能还有其他的解决方案。我鼓励其他用户分享他们的方法。 @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)