删除内部边框
回答
此代码(基于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 –
- 1. 删除gtk.Button上的内部边框
- 2. 删除顶部边框
- 3. 删除部分边框
- 4. 删除边框底部离子头
- 5. 删除页面顶部的边框
- 6. 删除EmptyDataTemplate边框
- 7. 删除textareafield边框
- 8. 无法从标签内部的img中删除边框
- 9. CSS内部边框?
- 10. 删除顶部边框的TabBar而不会裁剪到边界
- 11. 如何删除一系列图像的内部边框并使外部边框在css中保持连续?
- 12. ios5:删除UIButton边框
- 13. 仅删除边框(CSS)
- 14. jQuery缩放删除边框
- 15. 删除CSS中的边框
- 16. 从Linklabel C删除边框#
- 17. 删除窗口边框?
- 18. 删除表格边框
- 19. 删除边框在mapkit
- 20. 删除AnchorTag IE边框
- 21. 删除按钮的边框
- 22. 从JComboBox中删除边框
- 23. 删除容器边框
- 24. ScrollView和删除边框
- 25. Flex 4.6 TabbedViewNavigator - 删除边框
- 26. 如何删除此边框?
- 27. 从HBITMAP中删除边框
- 28. 删除图像的边框
- 29. 删除gtk.button的边框
- 30. mPDF - 删除图像边框
为什么不干脆在每个边界裁剪_x_像素的图像(即5像素)? –
因为有些图像没有黑色边框,有时候它们也很小,如果我像你说的那样裁剪它们,我也会剪裁文字。 – sebbz