删除内部边框

时间:2017-05-05 07:25:47

标签: python opencv

我有大量来自桌面图像的裁剪图像。由于"剩饭和#34; OCR在文本检测方面存在一些问题。桌边框。实际上,我正在寻找删除它们的方法(我想只提取文本)。以下是一些例子:

first image example

second image example

谢谢!

1 个答案:

答案 0 :(得分:1)

此代码(基于opencv)解决了两个示例的问题。程序如下:

  • 阈值图片
  • 从二进制对象中删除行
    • 计算比率=(对象区域)/(边界框区域)
      • 如果比率太小,我们认为对象是行的组合
      • 如果比例很大,我们认为对象是单行

这里是python代码:

import cv2
import matplotlib.pylab as plt
import numpy as np

# load image
img = cv2.imread('om9gN.jpg',0)

# blur and apply otsu threshold
img = cv2.blur(img, (3,3))
_, img = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

# invert image
img = (img == 0).astype(np.uint8)


img_new = np.zeros_like(img)

# find contours
_,contours,_ = cv2.findContours(img, 1, 2)

for idx, cnt in enumerate(contours):

    # get area of contour
    temp = np.zeros_like(img)
    cv2.drawContours(temp, contours , idx, 1, -1)
    area_cnt = np.sum(temp)

    # get number of pixels of bounding box of contour
    x,y,w,h = cv2.boundingRect(cnt)
    area_box = w * h

    # get ratio of cnt-area and box-area
    ratio = float(area_cnt) / area_box

    # only draw contour if:
    #    - 1.) ratio is not too big (line fills whole bounding box)
    #    - 2.) ratio is not too small (combination of lines fill very 
    #                                  small ratio of bounding box)
    if 0.9 > ratio > 0.2:
        cv2.drawContours(img_new, contours , idx, 1, -1)

plt.figure()
plt.subplot(1,2,1)
plt.imshow(img_new)
plt.axis("off")
plt.show()

Isolated letters