我试着把中世纪手稿中的单词分离出来。 我用cv2来检测区域ant-id给了我一个非常满意的结果。我用递增的数字标记每个矩形,我担心检测到的区域不是连续的: Here is a sample result of cv2 bounding box zones on a word
以下是我使用的代码:
import numpy as np
import cv2
import matplotlib.pyplot as plt
# This is font for labels
font = cv2.FONT_HERSHEY_SIMPLEX
# I load a picture of a page, gray and blur it
im = cv2.imread('test.png')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
image_blurred = cv2.GaussianBlur(imgray, (5, 5), 0)
image_blurred = cv2.dilate(image_blurred, None)
ret,thresh = cv2.threshold(image_blurred,0,255,0,cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# I try to retrieve contours and hierarchy on the sample
_, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
hierarchy = hierarchy[0]
# I read every contours and retrieve the bounding box
for i,component in enumerate(zip(contours, hierarchy)):
cnt = component[0]
currentHierarchy = component[1]
precision = 0.01
epsilon = precision*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)
# This is the best combination I found to isolate parents container
# It gives me the best result (even if I'm not sure what I'm doing)
# hierarchy[2/3] is "having child" / "having parent"
# I thought currentHierarchy[3] < 0 should be better
# but it gives no result
if currentHierarchy[2] > 0 and currentHierarchy[3] > 0:
x,y,w,h = cv2.boundingRect(approx)
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
cv2.putText(im,str(i),(x+2,y+2), font, 1,(0,255,0),2,cv2.LINE_AA)
plt.imshow(im)
plt.show()
我想把最近的区域连接在一起,以便得到一个单词标记化我的页面。在我的示例图片中,我想加入2835、2847、2864、2878、2870和2868。在
我该怎么办?我想我可以把每个盒子的每个坐标都存储在一个数组中,然后测试(start_x,start_y)和(end_x,end_y)--但这对我来说很糟糕。在
你能给个提示吗?在
谢谢
谢谢Jeru Luke,我们可以在一个完整的页面上实现这个尝试。给出的值对于模糊和腐蚀操作中的内核适应非常有帮助。The final result on the Bible Historiale Manuscript page仍然非常有趣。根据我的理解,在鉴定中我们可以看到一些“黑洞”,这是第一个正在进行的工作。我们必须管理大图片和大写字母。 下面是我们用来过滤框、在框上添加标签以及将每个片段保存在单独文件中的代码:
我继续我的方法来找出单个单词。虽然不完全准确,但请看下图:
伪代码:
做了几个形态学手术:
3.1腐蚀-试图去除图像左上方的细线。在
3.2将因上一次操作而分离的单个字母进行扩展。
找到某个区域上方的轮廓并标记它们
编辑
代码:
相关问题 更多 >
编程相关推荐