删除内部边框
此代码(基于opencv)解决了这两个示例的问题。该过程如下:
- 阈值图像
- remove从二进制对象线
- 计算比=(物体的面积)/(边界框的面积)
- 如果比率太小,我们认为物体是线条组合
- 如果比例较大,我们认为该物体是单线
- 计算比=(物体的面积)/(边界框的面积)
这里的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()
好主意,应该在大多数情况下工作。但是,根据所使用的字体,它还会删除诸如** l **(L),** I **(i)和** i **之类的字母。 –
这是真的。避免这种情况的一个选择是也可以在边界框纵横比(box_width/box_lenght)的纵横比上设置阈值。如果纵横比太小,它必须是一行而不是I,l, - ,... –
,在这里我找到了另一种方法来从二进制图像中删除行:http://docs.opencv.org/trunk /d1/dee/tutorial_moprh_lines_detection.html –
为什么不干脆在每个边界裁剪_x_像素的图像(即5像素)? –
因为有些图像没有黑色边框,有时候它们也很小,如果我像你说的那样裁剪它们,我也会剪裁文字。 – sebbz