使用 Python 从 OpenCV 中的扫描中裁剪矩形照片

pol*_*m23 4 python opencv image-processing

我有一堆这样的照片:

浅草照片扫描

我想自动裁剪图像,以便只显示照片(可能还有标题)。

我尝试检测轮廓,但他们发现了照片中物体的边界,而不是照片本身。图像边缘和其他小边缘也存在虚假轮廓。

糟糕的轮廓

我该怎么做才能获得包含照片的矩形?

pol*_*m23 8

我设法想出了一个令人满意的解决方案。有几个步骤:

  1. 获取轮廓
  2. 去除面积过小或过大的轮廓
  3. 在所有剩余轮廓上找到最小/最大 x/y
  4. 使用这些值创建一个矩形以进行裁剪

这就是基本过程。

无论如何,这是核心部分的一些代码:

import cv2
from os.path import basename
from glob import glob

def get_contours(img):
    # First make the image 1-bit and get contours
    imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    ret, thresh = cv2.threshold(imgray, 150, 255, 0)

    cv2.imwrite('thresh.jpg', thresh)
    img2, contours, hierarchy = cv2.findContours(thresh, 1, 2)

    # filter contours that are too large or small
    size = get_size(img)
    contours = [cc for cc in contours if contourOK(cc, size)]
    return contours

def get_size(img):
    ih, iw = img.shape[:2]
    return iw * ih

def contourOK(cc, size=1000000):
    x, y, w, h = cv2.boundingRect(cc)
    if w < 50 or h < 50: return False # too narrow or wide is bad
    area = cv2.contourArea(cc)
    return area < (size * 0.5) and area > 200

def find_boundaries(img, contours):
    # margin is the minimum distance from the edges of the image, as a fraction
    ih, iw = img.shape[:2]
    minx = iw
    miny = ih
    maxx = 0
    maxy = 0

    for cc in contours:
        x, y, w, h = cv2.boundingRect(cc)
        if x < minx: minx = x
        if y < miny: miny = y
        if x + w > maxx: maxx = x + w
        if y + h > maxy: maxy = y + h

    return (minx, miny, maxx, maxy)

def crop(img, boundaries):
    minx, miny, maxx, maxy = boundaries
    return img[miny:maxy, minx:maxx]

def process_image(fname):
    img = cv2.imread(fname)
    contours = get_contours(img)
    #cv2.drawContours(img, contours, -1, (0,255,0)) # draws contours, good for debugging
    bounds = find_boundaries(img, contours)
    cropped = crop(img, bounds)
    if get_size(cropped) < 400: return # too small
    cv2.imwrite('cropped/' + basename(fname), cropped)

process_image('pic.jpg')
Run Code Online (Sandbox Code Playgroud)

这有重要的部分,但我使用了另外两个对我的数据集效果很好的技巧:

  1. 修改阈值,直到图像的特定百分比为黑色。对于我的大多数图像,即使照片最亮的部分也比下面的页面更暗,因此在某个魔术阈值级别,照片会变成黑色方块,因此更容易获得良好的轮廓。

  2. 完全忽略图像边缘附近的轮廓。有时书脊的一点点会导致在原始图像的边界处形成轮廓,这是不可取的。检查边缘的小像素数(如 20)内的轮廓并忽略它们解决了该问题。

一些结果图像,左边是原始图像,右边是自动裁剪的:

有电车的街道

公园里的人