删除包含 python 中文本的图像边框

Remove image borders containing text in python

我有一张图像是某些移动扫描仪的输出。当图像来自移动扫描仪应用程序时,图像被放置在一个矩形框内,边框具有扫描仪应用程序的 advt/信息。下面是图像: 在上图中,“Scanned by TapScanner”是广告信息。我从 Whosebug 获得了一个删除图像边框的代码,它能够从顶部删除边框,但由于下部的文本,它无法从下方删除边框。 以下是代码:

from PIL import Image, ImageChops
def trim(image_path):
    im = Image.open(image_path)
    bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
    diff = ImageChops.difference(im, bg)
    diff = ImageChops.add(diff, diff, 2.0, -100)
    bbox = diff.getbbox()
    if bbox:
        return im.crop(bbox)
crop_image = trim(image_path)

以下是当前输出: 是否可以只从整个图像中获取实际图像。请帮忙。我是图像处理的新手。 谢谢。

对图像进行阈值处理,并找到图像中最大的对象。

#!/usr/bin/env python
"""
Find and isolate the largest object in an image.
"""
import matplotlib.pyplot as plt
from skimage.io import imread
from skimage.measure import label, regionprops

figure, axes = plt.subplots(1, 3, sharex=True, sharey=True)

raw = imread('cheque.jpg')
axes[0].imshow(raw)

threshold = 250 # might need adjustment
binary = raw.mean(-1) < threshold
axes[1].imshow(binary, cmap='gray')

labeled = label(binary)
regions = regionprops(labeled)

largest = regions[0] # regions are ordered by area
minr, minc, maxr, maxc = largest.bbox
bx = (minc, maxc, maxc, minc, minc)
by = (minr, minr, maxr, maxr, minr)

axes[2].imshow(raw)
axes[2].plot(bx, by, '-b', linewidth=2.5)

for ax in axes:
    ax.axis([0, raw.shape[1], raw.shape[0], 0])

new = raw[minr:maxr, minc:maxc]
fig, ax = plt.subplots()
ax.imshow(new)

plt.show()

经过一番搜索,我找到了一个不错的解决方案,如下:

import opencv2
image = cv2.imread('cheque.jpg')

# convert to gray scale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# detect edges uing canny
edged = cv2.Canny(gray, 1, 1)

# find the countours
contours, hierarchy = cv2.findContours(edged, 
    cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

# take the maximum counter using "length" instead of "area"
contour = max(contours, key=len)

# get the edges of the counter
x, y, w, h = cv2.boundingRect(contour)

# crop the counter and save it
roi = image[y:y+h, x:x+w]
cv2.imwrite('cheque-crop.jpg', roi)